omni_orchestrator/schemas/v1/api/apps/
delete.rs

1use super::super::super::db::queries as db;
2use rocket::http::Status;
3use rocket::serde::json::{json, Json, Value};
4use rocket::{delete, State};
5use std::sync::Arc;
6
7use crate::DatabaseManager;
8
9/// Delete a specific application.
10///
11/// # Arguments
12///
13/// * `platform_id` - Platform identifier
14/// * `app_id` - The ID of the application to delete
15/// * `db_manager` - Database manager for accessing platform-specific pools
16///
17/// # Returns
18///
19/// A JSON response indicating success or an error message
20#[delete("/platform/<platform_id>/apps/<app_id>")]
21pub async fn delete_app(
22    platform_id: i64,
23    app_id: String,
24    db_manager: &State<Arc<DatabaseManager>>,
25) -> Result<Json<Value>, (Status, Json<Value>)> {
26    // Get platform information
27    let platform = match db::platforms::get_platform_by_id(db_manager.get_main_pool(), platform_id).await {
28        Ok(platform) => platform,
29        Err(_) => {
30            return Err((
31                Status::NotFound,
32                Json(json!({
33                    "error": "Platform not found",
34                    "message": format!("Platform with ID {} does not exist", platform_id)
35                }))
36            ));
37        }
38    };
39
40    // Get platform-specific database pool
41    let pool = match db_manager.get_platform_pool(&platform.name, platform_id).await {
42        Ok(pool) => pool,
43        Err(_) => {
44            return Err((
45                Status::InternalServerError,
46                Json(json!({
47                    "error": "Database error",
48                    "message": "Failed to connect to platform database"
49                }))
50            ));
51        }
52    };
53
54    match app_id.parse::<i64>() {
55        Ok(id) => {
56            match db::app::delete_app(&pool, id).await {
57                Ok(_) => Ok(Json(json!({ "status": "deleted" }))),
58                Err(_) => {
59                    Err((
60                        Status::InternalServerError,
61                        Json(json!({
62                            "error": "Database error",
63                            "message": "Failed to delete application"
64                        }))
65                    ))
66                }
67            }
68        }
69        Err(e) => {
70            Err((
71                Status::BadRequest,
72                Json(json!({
73                    "error": "Invalid ID format",
74                    "message": format!("The application ID must be a valid integer: {}", e)
75                }))
76            ))
77        }
78    }
79}