omni_orchestrator/schemas/v1/api/deployments/
list.rs1use std::sync::Arc;
2use crate::DatabaseManager;
3use super::super::super::db::queries as db;
4use rocket::http::Status;
5use rocket::serde::json::{json, Json, Value};
6use rocket::{get, State};
7
8#[get("/platform/<platform_id>/deployments?<page>&<per_page>")]
10pub async fn list_deployments(
11 platform_id: i64,
12 page: Option<i64>,
13 per_page: Option<i64>,
14 db_manager: &State<Arc<DatabaseManager>>,
15) -> Result<Json<Value>, (Status, Json<Value>)> {
16 let platform = match db::platforms::get_platform_by_id(db_manager.get_main_pool(), platform_id).await {
18 Ok(platform) => platform,
19 Err(_) => {
20 return Err((
21 Status::NotFound,
22 Json(json!({
23 "error": "Platform not found",
24 "message": format!("Platform with ID {} does not exist", platform_id)
25 }))
26 ));
27 }
28 };
29
30 let pool = match db_manager.get_platform_pool(&platform.name, platform_id).await {
32 Ok(pool) => pool,
33 Err(_) => {
34 return Err((
35 Status::InternalServerError,
36 Json(json!({
37 "error": "Database error",
38 "message": "Failed to connect to platform database"
39 }))
40 ));
41 }
42 };
43
44 match (page, per_page) {
45 (Some(p), Some(pp)) => {
46 let deployments = match db::deployment::list_deployments(&pool, p, pp).await {
47 Ok(deployments) => deployments,
48 Err(_) => {
49 return Err((
50 Status::InternalServerError,
51 Json(json!({
52 "error": "Database error",
53 "message": "Failed to retrieve deployments"
54 }))
55 ));
56 }
57 };
58
59 let total_count = match db::deployment::count_deployments(&pool).await {
60 Ok(count) => count,
61 Err(_) => {
62 return Err((
63 Status::InternalServerError,
64 Json(json!({
65 "error": "Database error",
66 "message": "Failed to count deployments"
67 }))
68 ));
69 }
70 };
71
72 let total_pages = (total_count as f64 / pp as f64).ceil() as i64;
73
74 let response = json!({
75 "deployments": deployments,
76 "pagination": {
77 "page": p,
78 "per_page": pp,
79 "total_count": total_count,
80 "total_pages": total_pages
81 }
82 });
83
84 Ok(Json(response))
85 }
86 _ => Err((
87 Status::BadRequest,
88 Json(json!({
89 "error": "Missing pagination parameters",
90 "message": "Please provide both 'page' and 'per_page' parameters"
91 }))
92 ))
93 }
94}
95
96#[get("/platform/<platform_id>/count/deployments")]
98pub async fn count_deployments(
99 platform_id: i64,
100 db_manager: &State<Arc<DatabaseManager>>
101) -> Result<Json<i64>, (Status, Json<Value>)> {
102 let platform = match db::platforms::get_platform_by_id(db_manager.get_main_pool(), platform_id).await {
104 Ok(platform) => platform,
105 Err(_) => {
106 return Err((
107 Status::NotFound,
108 Json(json!({
109 "error": "Platform not found",
110 "message": format!("Platform with ID {} does not exist", platform_id)
111 }))
112 ));
113 }
114 };
115
116 let pool = match db_manager.get_platform_pool(&platform.name, platform_id).await {
118 Ok(pool) => pool,
119 Err(_) => {
120 return Err((
121 Status::InternalServerError,
122 Json(json!({
123 "error": "Database error",
124 "message": "Failed to connect to platform database"
125 }))
126 ));
127 }
128 };
129
130 match db::deployment::count_deployments(&pool).await {
131 Ok(count) => Ok(Json(count)),
132 Err(_) => Err((
133 Status::InternalServerError,
134 Json(json!({
135 "error": "Database error",
136 "message": "Failed to count deployments"
137 }))
138 )),
139 }
140}
141
142#[get("/platform/<platform_id>/apps/<app_id>/deployments?<page>&<per_page>")]
144pub async fn list_app_deployments(
145 platform_id: i64,
146 app_id: i64,
147 page: Option<i64>,
148 per_page: Option<i64>,
149 db_manager: &State<Arc<DatabaseManager>>,
150) -> Result<Json<Value>, (Status, Json<Value>)> {
151 let platform = match db::platforms::get_platform_by_id(db_manager.get_main_pool(), platform_id).await {
153 Ok(platform) => platform,
154 Err(_) => {
155 return Err((
156 Status::NotFound,
157 Json(json!({
158 "error": "Platform not found",
159 "message": format!("Platform with ID {} does not exist", platform_id)
160 }))
161 ));
162 }
163 };
164
165 let pool = match db_manager.get_platform_pool(&platform.name, platform_id).await {
167 Ok(pool) => pool,
168 Err(_) => {
169 return Err((
170 Status::InternalServerError,
171 Json(json!({
172 "error": "Database error",
173 "message": "Failed to connect to platform database"
174 }))
175 ));
176 }
177 };
178
179 match (page, per_page) {
180 (Some(p), Some(pp)) => {
181 let deployments = match db::deployment::list_deployments_by_app(&pool, app_id, p, pp).await {
182 Ok(deployments) => deployments,
183 Err(_) => {
184 return Err((
185 Status::InternalServerError,
186 Json(json!({
187 "error": "Database error",
188 "message": "Failed to retrieve deployments"
189 }))
190 ));
191 }
192 };
193
194 let total_count = match db::deployment::count_deployments_by_app(&pool, app_id).await {
195 Ok(count) => count,
196 Err(_) => {
197 return Err((
198 Status::InternalServerError,
199 Json(json!({
200 "error": "Database error",
201 "message": "Failed to count deployments"
202 }))
203 ));
204 }
205 };
206
207 let total_pages = (total_count as f64 / pp as f64).ceil() as i64;
208
209 let response = json!({
210 "deployments": deployments,
211 "pagination": {
212 "page": p,
213 "per_page": pp,
214 "total_count": total_count,
215 "total_pages": total_pages
216 }
217 });
218
219 Ok(Json(response))
220 }
221 _ => Err((
222 Status::BadRequest,
223 Json(json!({
224 "error": "Missing pagination parameters",
225 "message": "Please provide both 'page' and 'per_page' parameters"
226 }))
227 ))
228 }
229}