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

1use super::super::super::db::queries as db;
2use rocket::http::Status;
3use rocket::serde::json::{json, Json, Value};
4use rocket::{get, State};
5use std::sync::Arc;
6
7use crate::DatabaseManager;
8
9// List all instances for an application with pagination
10#[get("/platform/<platform_id>/apps/<app_id>/instances?<page>&<per_page>")]
11pub async fn list_instances(
12    platform_id: i64,
13    app_id: i64,
14    page: Option<i64>,
15    per_page: Option<i64>,
16    db_manager: &State<Arc<DatabaseManager>>,
17) -> Result<Json<Value>, (Status, Json<Value>)> {
18    // Get platform information
19    let platform = match db::platforms::get_platform_by_id(db_manager.get_main_pool(), platform_id).await {
20        Ok(platform) => platform,
21        Err(_) => {
22            return Err((
23                Status::NotFound,
24                Json(json!({
25                    "error": "Platform not found",
26                    "message": format!("Platform with ID {} does not exist", platform_id)
27                }))
28            ));
29        }
30    };
31
32    // Get platform-specific database pool
33    let pool = match db_manager.get_platform_pool(&platform.name, platform_id).await {
34        Ok(pool) => pool,
35        Err(_) => {
36            return Err((
37                Status::InternalServerError,
38                Json(json!({
39                    "error": "Database error",
40                    "message": "Failed to connect to platform database"
41                }))
42            ));
43        }
44    };
45
46    match (page, per_page) {
47        (Some(p), Some(pp)) => {
48            let instances = match db::app::list_instances(&pool, app_id, p, pp).await {
49                Ok(instances) => instances,
50                Err(_) => {
51                    return Err((
52                        Status::InternalServerError,
53                        Json(json!({
54                            "error": "Database error",
55                            "message": "Failed to retrieve instances"
56                        }))
57                    ));
58                }
59            };
60            
61            let total_count = match db::app::count_instances_by_app(&pool, app_id).await {
62                Ok(count) => count,
63                Err(_) => {
64                    return Err((
65                        Status::InternalServerError,
66                        Json(json!({
67                            "error": "Database error",
68                            "message": "Failed to count instances"
69                        }))
70                    ));
71                }
72            };
73            
74            let total_pages = (total_count as f64 / pp as f64).ceil() as i64;
75
76            let response = json!({
77                "instances": instances,
78                "pagination": {
79                    "page": p,
80                    "per_page": pp,
81                    "total_count": total_count,
82                    "total_pages": total_pages
83                }
84            });
85
86            Ok(Json(response))
87        }
88        _ => Err((
89            Status::BadRequest,
90            Json(json!({
91                "error": "Missing pagination parameters",
92                "message": "Please provide both 'page' and 'per_page' parameters"
93            }))
94        ))
95    }
96}