2025-03-20 12:32:06 +08:00
---
title: Add Dashboards to Collection REST Endpoint
author: Cascade
date: 2025-03-19
2025-03-20 13:26:31 +08:00
status: Implemented
2025-03-20 12:32:06 +08:00
---
# Add Dashboards to Collection REST Endpoint
## Problem Statement
Users need the ability to programmatically add dashboards to collections via a REST API. Currently, this functionality is not available, limiting the ability to manage collections through the API.
## Goals
2025-03-20 13:26:31 +08:00
1. ✅ Create a REST endpoint to add dashboards to a collection
2. ✅ Implement proper permission validation
3. ✅ Ensure data integrity with proper error handling
4. ✅ Follow established patterns for REST endpoints and handlers
2025-03-20 12:32:06 +08:00
## Non-Goals
1. Modifying the existing collections functionality
2. Creating UI components for this endpoint
## Technical Design
### REST Endpoint
**Endpoint:** `POST /collections/:id/dashboards`
**Request Body:**
```json
{
"dashboard_ids": ["uuid1", "uuid2", "uuid3"]
}
```
**Response:**
- `200 OK` - Success
```json
{
"message": "Dashboards added to collection successfully"
}
```
- `400 Bad Request` - Invalid input
- `403 Forbidden` - Insufficient permissions
- `404 Not Found` - Collection not found
- `500 Internal Server Error` - Server error
### Handler Implementation
The handler will:
2025-03-20 13:26:31 +08:00
1. ✅ Validate that the collection exists
2. ✅ Check if the user has appropriate permissions (Owner, FullAccess, or CanEdit)
3. ✅ Validate that the dashboards exist and the user has access to them
4. ✅ Add the dashboards to the collection by creating records in the `collections_to_assets` table
5. ✅ Handle the case where a dashboard was previously in the collection but was deleted (upsert)
2025-03-20 12:32:06 +08:00
### File Changes
#### New Files
2025-03-20 13:26:31 +08:00
1. ✅ `libs/handlers/src/collections/add_dashboards_to_collection_handler.rs`
2. ✅ `src/routes/rest/routes/collections/add_dashboards_to_collection.rs`
3. ✅ Update `libs/handlers/src/collections/mod.rs` to include the new handler
4. ✅ Update `src/routes/rest/routes/collections/mod.rs` to include the new endpoint
2025-03-20 12:32:06 +08:00
### Database Operations
2025-03-20 13:26:31 +08:00
The implementation uses the `collections_to_assets` table with the following operations:
1. ✅ SELECT to check if records exist
2. ✅ INSERT for new records
3. ✅ UPDATE for records that were previously deleted
2025-03-20 12:32:06 +08:00
## Testing Strategy
### Unit Tests
2025-03-20 13:26:31 +08:00
1. ✅ Test the handler with mocked database connections
- ✅ Test adding dashboards to a collection
- ✅ Test error cases (collection not found, dashboard not found, insufficient permissions)
- ✅ Test adding dashboards that were previously in the collection but deleted
2025-03-20 12:32:06 +08:00
2025-03-20 13:26:31 +08:00
2. ✅ Test the REST endpoint
- ✅ Test successful request
- ✅ Test error responses for various scenarios
2025-03-20 12:32:06 +08:00
### Integration Tests
2025-03-20 13:26:31 +08:00
1. ✅ Test skeleton created for the endpoint with a test database
2025-03-20 12:32:06 +08:00
## Security Considerations
2025-03-20 13:26:31 +08:00
- ✅ The endpoint requires authentication
- ✅ Permission checks ensure users can only modify collections they have access to
- ✅ Input validation prevents malicious data
2025-03-20 12:32:06 +08:00
## Monitoring and Logging
2025-03-20 13:26:31 +08:00
- ✅ All operations are logged with appropriate context
- ✅ Errors are logged with detailed information
2025-03-20 12:32:06 +08:00
## Dependencies
- `libs/sharing` - For permission checking
- `libs/database` - For database operations
## Rollout Plan
2025-03-20 13:26:31 +08:00
1. ✅ Implement the handler and endpoint
2. ✅ Write tests
3. ✅ Code review
2025-03-20 12:32:06 +08:00
4. Deploy to staging
5. Test in staging
6. Deploy to production
7. Monitor for issues