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

1use super::super::super::db::queries as db;
2use super::types::CreateAppRequest;
3use rocket::http::Status;
4use rocket::serde::json::{json, Json, Value};
5use rocket::{post, State};
6use std::sync::Arc;
7
8use crate::DatabaseManager;
9
10use libomni::types::db::v1 as types;
11use types::app::App;
12
13/// Create a new application.
14///
15/// # Arguments
16///
17/// * `platform_id` - Platform identifier
18/// * `app_request` - JSON data containing application details
19/// * `db_manager` - Database manager for accessing platform-specific pools
20///
21/// # Returns
22///
23/// The newly created application
24#[post("/platform/<platform_id>/apps", format = "json", data = "<app_request>")]
25pub async fn create_app(
26    platform_id: i64,
27    app_request: Json<CreateAppRequest>,
28    db_manager: &State<Arc<DatabaseManager>>,
29) -> Result<Json<App>, (Status, Json<Value>)> {
30    // Get platform information
31    let platform = match db::platforms::get_platform_by_id(db_manager.get_main_pool(), platform_id).await {
32        Ok(platform) => platform,
33        Err(_) => {
34            return Err((
35                Status::NotFound,
36                Json(json!({
37                    "error": "Platform not found",
38                    "message": format!("Platform with ID {} does not exist", platform_id)
39                }))
40            ));
41        }
42    };
43
44    // Get platform-specific database pool
45    let pool = match db_manager.get_platform_pool(&platform.name, platform_id).await {
46        Ok(pool) => pool,
47        Err(_) => {
48            return Err((
49                Status::InternalServerError,
50                Json(json!({
51                    "error": "Database error",
52                    "message": "Failed to connect to platform database"
53                }))
54            ));
55        }
56    };
57
58    match db::app::create_app(
59        &pool,
60        &app_request.name,
61        app_request.org_id,
62        None,
63        None,
64        None,
65        None,
66    ).await {
67        Ok(app) => Ok(Json(app)),
68        Err(_) => {
69            Err((
70                Status::InternalServerError,
71                Json(json!({
72                    "error": "Database error",
73                    "message": "Failed to create application"
74                }))
75            ))
76        }
77    }
78}