fix collections delete

This commit is contained in:
dal 2025-03-20 15:08:35 -06:00
parent 1e5b63a3d0
commit b9a0dcfb7a
No known key found for this signature in database
GPG Key ID: 16F4B0E1E9F61122
4 changed files with 106 additions and 23 deletions

View File

@ -1,14 +1,33 @@
use axum::{http::StatusCode, Extension, Json}; use axum::{extract::Path, http::StatusCode, Extension, Json};
use handlers::collections::{ use handlers::collections::{
delete_collection_handler, DeleteCollectionRequest, DeleteCollectionResponse, delete_collection_handler, DeleteCollectionRequest, DeleteCollectionResponse,
}; };
use middleware::AuthenticatedUser; use middleware::AuthenticatedUser;
use uuid::Uuid; use uuid::Uuid;
/// Delete a collection /// Delete a collection by ID
/// ///
/// This endpoint deletes one or more collections by their IDs. /// This endpoint deletes a single collection by its ID.
pub async fn delete_collection( pub async fn delete_collection_by_id(
Path(id): Path<Uuid>,
Extension(user): Extension<AuthenticatedUser>,
) -> Result<Json<DeleteCollectionResponse>, (StatusCode, String)> {
let user_organization = match user.organizations.first() {
Some(org) => org,
None => return Err((StatusCode::NOT_FOUND, "User not found".to_string())),
};
// Call the handler with a single ID in a vector
match delete_collection_handler(&user.id, &user_organization.id, vec![id]).await {
Ok(response) => Ok(Json(response)),
Err(e) => handle_error(e)
}
}
/// Delete multiple collections
///
/// This endpoint deletes one or more collections by their IDs provided in the request body.
pub async fn delete_collections(
Extension(user): Extension<AuthenticatedUser>, Extension(user): Extension<AuthenticatedUser>,
Json(req): Json<DeleteCollectionRequest>, Json(req): Json<DeleteCollectionRequest>,
) -> Result<Json<DeleteCollectionResponse>, (StatusCode, String)> { ) -> Result<Json<DeleteCollectionResponse>, (StatusCode, String)> {
@ -20,23 +39,26 @@ pub async fn delete_collection(
// Call the handler // Call the handler
match delete_collection_handler(&user.id, &user_organization.id, req.ids).await { match delete_collection_handler(&user.id, &user_organization.id, req.ids).await {
Ok(response) => Ok(Json(response)), Ok(response) => Ok(Json(response)),
Err(e) => { Err(e) => handle_error(e)
tracing::error!("Error deleting collection: {}", e); }
}
// Return appropriate error response based on the error
if e.to_string().contains("not found") { // Helper function to handle errors
Err(( fn handle_error(e: anyhow::Error) -> Result<Json<DeleteCollectionResponse>, (StatusCode, String)> {
StatusCode::NOT_FOUND, tracing::error!("Error deleting collection: {}", e);
format!("Collection not found: {}", e),
)) // Return appropriate error response based on the error
} else if e.to_string().contains("permission") { if e.to_string().contains("not found") {
Err((StatusCode::FORBIDDEN, format!("Permission denied: {}", e))) Err((
} else { StatusCode::NOT_FOUND,
Err(( format!("Collection not found: {}", e),
StatusCode::INTERNAL_SERVER_ERROR, ))
format!("Error deleting collection: {}", e), } else if e.to_string().contains("permission") {
)) Err((StatusCode::FORBIDDEN, format!("Permission denied: {}", e)))
} } else {
} Err((
StatusCode::INTERNAL_SERVER_ERROR,
format!("Error deleting collection: {}", e),
))
} }
} }

View File

@ -18,9 +18,10 @@ pub fn router() -> Router {
Router::new() Router::new()
.route("/", get(list_collections::list_collections)) .route("/", get(list_collections::list_collections))
.route("/", post(create_collection::create_collection)) .route("/", post(create_collection::create_collection))
.route("/", delete(delete_collection::delete_collections))
.route("/:id", get(get_collection::get_collection)) .route("/:id", get(get_collection::get_collection))
.route("/:id", put(update_collection::update_collection)) .route("/:id", put(update_collection::update_collection))
.route("/:id", delete(delete_collection::delete_collection)) .route("/:id", delete(delete_collection::delete_collection_by_id))
.route("/:id/assets", post(add_assets_to_collection::add_assets_to_collection)) .route("/:id/assets", post(add_assets_to_collection::add_assets_to_collection))
.route("/:id/dashboards", post(add_dashboards_to_collection::add_dashboards_to_collection)) .route("/:id/dashboards", post(add_dashboards_to_collection::add_dashboards_to_collection))
.route("/:id/assets", delete(remove_assets_from_collection::remove_assets_from_collection)) .route("/:id/assets", delete(remove_assets_from_collection::remove_assets_from_collection))

View File

@ -0,0 +1,59 @@
use uuid::Uuid;
use serde_json::json;
use crate::common::{
fixtures::users::create_test_user,
http::test_app::TestApp,
};
#[tokio::test]
async fn test_delete_collections_bulk() {
// Set up test app
let app = TestApp::new().await;
// Create test user
let user = create_test_user();
// Test IDs
let id1 = Uuid::new_v4();
let id2 = Uuid::new_v4();
// Call the API to delete collections in bulk
let response = app
.delete("/api/v1/collections")
.with_auth(&user)
.json(&json!({
"ids": [id1, id2]
}))
.send()
.await;
// Verify response status (we're not actually deleting real collections here)
// Since we're not creating test collections, it might fail with 404 or succeed with 200
// We're mainly testing that the endpoint accepts the request format correctly
assert!(response.status().is_client_error() || response.status().is_success());
}
#[tokio::test]
async fn test_delete_collection_by_id() {
// Set up test app
let app = TestApp::new().await;
// Create test user
let user = create_test_user();
// Test ID
let id = Uuid::new_v4();
// Call the API to delete a collection by ID
let response = app
.delete(&format!("/api/v1/collections/{}", id))
.with_auth(&user)
.send()
.await;
// Verify response status (we're not actually deleting a real collection here)
// Since we're not creating a test collection, it might fail with 404 or succeed with 200
// We're mainly testing that the endpoint accepts the request format correctly
assert!(response.status().is_client_error() || response.status().is_success());
}

View File

@ -1,6 +1,7 @@
pub mod sharing; pub mod sharing;
pub mod add_assets_to_collection_test; pub mod add_assets_to_collection_test;
pub mod add_dashboards_to_collection_test; pub mod add_dashboards_to_collection_test;
pub mod delete_collection_test;
pub mod get_collection_test; pub mod get_collection_test;
pub mod remove_assets_from_collection_test; pub mod remove_assets_from_collection_test;
pub mod remove_metrics_from_collection_test; pub mod remove_metrics_from_collection_test;