Skip to content

Commit c4a5a8f

Browse files
committed
[FEATURE] Major improvements:
- Document schema end-point implemented. - Update end-point implemented. - Insert end-point sign updated. - General refactorization.
1 parent 036ba8a commit c4a5a8f

File tree

7 files changed

+253
-29
lines changed

7 files changed

+253
-29
lines changed

src/infrastructure/controller_collection.rs

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,28 @@
1-
use axum::{extract::Path, http::StatusCode, middleware, response::IntoResponse, routing::{delete, get, post}, Json, Router};
2-
use rust_db_manager_core::{commons::configuration::configuration::Configuration, domain::{collection::generate_collection_query::GenerateCollectionQuery, filter::data_base_query::DataBaseQuery}};
1+
use axum::{
2+
extract::Path,
3+
http::StatusCode,
4+
middleware,
5+
response::IntoResponse,
6+
routing::{delete, get, post},
7+
Json, Router,
8+
};
9+
use rust_db_manager_core::{
10+
commons::configuration::configuration::Configuration,
11+
domain::{
12+
collection::generate_collection_query::GenerateCollectionQuery,
13+
filter::data_base_query::DataBaseQuery,
14+
},
15+
};
316

417
use crate::commons::exception::api_exception::ApiException;
518

6-
use super::{dto::{collection::dto_generate_collection_query::DTOGenerateCollectionQuery, table::dto_table_data_group::DTOTableDataGroup}, handler, utils};
19+
use super::{
20+
dto::{
21+
collection::dto_generate_collection_query::DTOGenerateCollectionQuery,
22+
table::dto_table_data_group::DTOTableDataGroup,
23+
},
24+
handler, utils,
25+
};
726

827
pub struct ControllerCollection {
928
}
@@ -33,7 +52,7 @@ impl ControllerCollection {
3352

3453
let query = DataBaseQuery::from_data_base(data_base);
3554

36-
let metadata = result.unwrap().data_base_collections_metadata(&query).await;
55+
let metadata = result.unwrap().data_base_metadata(&query).await;
3756
if let Err(error) = metadata {
3857
let exception = ApiException::from(StatusCode::INTERNAL_SERVER_ERROR.as_u16(), error);
3958
return Err(exception.into_response());
@@ -60,7 +79,7 @@ impl ControllerCollection {
6079

6180
let query = DataBaseQuery::from_data_base(data_base);
6281

63-
let collections = result.unwrap().list_collections(&query).await;
82+
let collections = result.unwrap().collection_find_all(&query).await;
6483
if let Err(error) = collections {
6584
let exception = ApiException::from(StatusCode::INTERNAL_SERVER_ERROR.as_u16(), error);
6685
return Err(exception.into_response());
@@ -86,7 +105,7 @@ impl ControllerCollection {
86105
return Err(exception.into_response());
87106
}
88107

89-
let collection = result.unwrap().create_collection(&query.unwrap()).await;
108+
let collection = result.unwrap().collection_create(&query.unwrap()).await;
90109
if let Err(error) = collection {
91110
let exception = ApiException::from(StatusCode::INTERNAL_SERVER_ERROR.as_u16(), error);
92111
return Err(exception.into_response());
@@ -109,7 +128,7 @@ impl ControllerCollection {
109128

110129
let query = GenerateCollectionQuery::from_collection(data_base, collection);
111130

112-
let collection = result.unwrap().drop_collection(&query).await;
131+
let collection = result.unwrap().collection_drop(&query).await;
113132
if let Err(error) = collection {
114133
let exception = ApiException::from(StatusCode::INTERNAL_SERVER_ERROR.as_u16(), error);
115134
return Err(exception.into_response());

src/infrastructure/controller_database.rs

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,26 @@
1-
use axum::{extract::Path, http::StatusCode, middleware, response::IntoResponse, routing::{delete, get, post}, Json, Router};
2-
use rust_db_manager_core::{commons::configuration::configuration::Configuration, domain::data_base::generate_database_query::GenerateDatabaseQuery};
1+
use axum::{
2+
extract::Path,
3+
http::StatusCode,
4+
middleware,
5+
response::IntoResponse,
6+
routing::{delete, get, post},
7+
Json, Router,
8+
};
9+
use rust_db_manager_core::{
10+
commons::configuration::configuration::Configuration,
11+
domain::data_base::generate_database_query::GenerateDatabaseQuery,
12+
};
313

414
use crate::commons::exception::api_exception::ApiException;
515

6-
use super::{dto::{collection::dto_collection_definition::DTOCollectionDefinition, data_base::dto_generate_data_base_query::DTOGenerateDatabaseQuery, table::dto_table_data_group::DTOTableDataGroup}, handler, utils};
16+
use super::{
17+
dto::{
18+
collection::dto_collection_definition::DTOCollectionDefinition,
19+
data_base::dto_generate_data_base_query::DTOGenerateDatabaseQuery,
20+
table::dto_table_data_group::DTOTableDataGroup,
21+
},
22+
handler, utils,
23+
};
724

825
pub struct ControllerDataBase {
926
}
@@ -14,7 +31,7 @@ impl ControllerDataBase {
1431
router
1532
.route("/:service/status", get(Self::status))
1633
.route("/:service/metadata", get(Self::metadata))
17-
.route("/:service/definition", get(Self::definition))
34+
.route("/:service/schema", get(Self::schema))
1835
.route("/:service/data-base", get(Self::find_all))
1936
.route("/:service/data-base", post(Self::insert))
2037
.route("/:service/data-base/:data_base", delete(Self::delete))
@@ -54,7 +71,7 @@ impl ControllerDataBase {
5471
return Err(exception.into_response());
5572
}
5673

57-
let metadata = result.unwrap().data_base_metadata().await;
74+
let metadata = result.unwrap().metadata().await;
5875
if let Err(error) = metadata {
5976
let exception = ApiException::from(StatusCode::INTERNAL_SERVER_ERROR.as_u16(), error);
6077
return Err(exception.into_response());
@@ -67,7 +84,7 @@ impl ControllerDataBase {
6784
Ok(Json(dto))
6885
}
6986

70-
async fn definition(Path(service): Path<String>) -> Result<Json<DTOCollectionDefinition>, impl IntoResponse> {
87+
async fn schema(Path(service): Path<String>) -> Result<Json<DTOCollectionDefinition>, impl IntoResponse> {
7188
let o_db_service = Configuration::find_service(&service);
7289
if o_db_service.is_none() {
7390
return Err(utils::not_found());
@@ -79,7 +96,7 @@ impl ControllerDataBase {
7996
return Err(exception.into_response());
8097
}
8198

82-
let definition = result.unwrap().collection_accept_definition().await;
99+
let definition = result.unwrap().collection_accept_schema().await;
83100
if let Err(error) = definition {
84101
let exception = ApiException::from(StatusCode::INTERNAL_SERVER_ERROR.as_u16(), error);
85102
return Err(exception.into_response());
@@ -100,7 +117,7 @@ impl ControllerDataBase {
100117
return Err(exception.into_response());
101118
}
102119

103-
let collection = result.unwrap().list_data_bases().await;
120+
let collection = result.unwrap().data_base_find_all().await;
104121
if let Err(error) = collection {
105122
let exception = ApiException::from(StatusCode::INTERNAL_SERVER_ERROR.as_u16(), error);
106123
return Err(exception.into_response());
@@ -123,7 +140,7 @@ impl ControllerDataBase {
123140

124141
let query = GenerateDatabaseQuery::new(dto.data_base);
125142

126-
let collection = result.unwrap().create_data_base(&query).await;
143+
let collection = result.unwrap().data_base_create(&query).await;
127144
if let Err(error) = collection {
128145
let exception = ApiException::from(StatusCode::INTERNAL_SERVER_ERROR.as_u16(), error);
129146
return Err(exception.into_response());
@@ -146,7 +163,7 @@ impl ControllerDataBase {
146163

147164
let query = GenerateDatabaseQuery::new(data_base);
148165

149-
let collection = result.unwrap().drop_data_base(&query).await;
166+
let collection = result.unwrap().data_base_drop(&query).await;
150167
if let Err(error) = collection {
151168
let exception = ApiException::from(StatusCode::INTERNAL_SERVER_ERROR.as_u16(), error);
152169
return Err(exception.into_response());

src/infrastructure/controller_document.rs

Lines changed: 157 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,32 @@
1-
use axum::{extract::Path, http::StatusCode, middleware, response::IntoResponse, routing::{get, post}, Json, Router};
2-
use rust_db_manager_core::{commons::{configuration::configuration::Configuration, utils::document_keys_to_filter_element}, domain::filter::data_base_query::DataBaseQuery};
1+
use axum::{
2+
extract::Path,
3+
http::StatusCode,
4+
middleware,
5+
response::IntoResponse,
6+
routing::{delete, get, post, put},
7+
Json, Router,
8+
};
9+
use rust_db_manager_core::{
10+
commons::{
11+
configuration::configuration::Configuration, utils::document_keys_to_filter_element,
12+
},
13+
domain::filter::data_base_query::DataBaseQuery,
14+
};
315

416
use crate::commons::exception::api_exception::ApiException;
517

6-
use super::{dto::{document::{dto_document_data::DTODocumentData, dto_document_key::DTODocumentKey}, table::dto_table_data_group::DTOTableDataGroup}, handler, utils};
18+
use super::{
19+
dto::{
20+
document::{
21+
dto_document_data::DTODocumentData, dto_document_key::DTODocumentKey,
22+
dto_document_schema::DTODocumentSchema,
23+
},
24+
dto_create_document::DTOCreateDocument,
25+
dto_update_document::DTOUpdateDocument,
26+
table::dto_table_data_group::DTOTableDataGroup,
27+
},
28+
handler, utils,
29+
};
730

831
pub struct ControllerDocument {
932
}
@@ -13,8 +36,12 @@ impl ControllerDocument {
1336
pub fn route(router: Router) -> Router {
1437
router
1538
.route("/:service/data-base/:data_base/collection/:collection/metadata", get(Self::metadata))
16-
.route("/:service/data-base/:data_base/collection/:collection", get(Self::find_all))
17-
.route("/:service/data-base/:data_base/collection/:collection/document", post(Self::find))
39+
.route("/:service/data-base/:data_base/collection/:collection/schema", get(Self::schema))
40+
.route("/:service/data-base/:data_base/collection/:collection/document/find", get(Self::find_all))
41+
.route("/:service/data-base/:data_base/collection/:collection/document/find", post(Self::find))
42+
.route("/:service/data-base/:data_base/collection/:collection/document/query", post(Self::insert))
43+
.route("/:service/data-base/:data_base/collection/:collection/document/query", put(Self::update))
44+
.route("/:service/data-base/:data_base/collection/:collection/document/query", delete(Self::delete))
1845
.route_layer(middleware::from_fn(handler::autentication_handler))
1946
}
2047

@@ -45,7 +72,7 @@ impl ControllerDocument {
4572
Ok(Json(dto))
4673
}
4774

48-
async fn find_all(Path((service, data_base, collection)): Path<(String, String, String)>) -> Result<Json<Vec<DTODocumentData>>, impl IntoResponse> {
75+
async fn schema(Path((service, data_base, collection)): Path<(String, String, String)>) -> Result<Json<DTODocumentSchema>, impl IntoResponse> {
4976
let o_db_service = Configuration::find_service(&service);
5077
if o_db_service.is_none() {
5178
return Err(utils::not_found());
@@ -59,16 +86,13 @@ impl ControllerDocument {
5986

6087
let query = DataBaseQuery::from(data_base, collection);
6188

62-
let documents = result.unwrap().find_all(&query).await;
63-
if let Err(error) = documents {
89+
let schema = result.unwrap().schema(&query).await;
90+
if let Err(error) = schema {
6491
let exception = ApiException::from(StatusCode::INTERNAL_SERVER_ERROR.as_u16(), error);
6592
return Err(exception.into_response());
6693
}
6794

68-
Ok(Json(documents.unwrap().iter()
69-
.map(|d| DTODocumentData::from(d))
70-
.collect())
71-
)
95+
Ok(Json(DTODocumentSchema::from(&schema.unwrap())))
7296
}
7397

7498
async fn find(Path((service, data_base, collection)): Path<(String, String, String)>, Json(dto): Json<Vec<DTODocumentKey>>) -> Result<Json<DTODocumentData>, impl IntoResponse> {
@@ -111,4 +135,125 @@ impl ControllerDocument {
111135
Ok(Json(DTODocumentData::from(&document.unwrap())))
112136
}
113137

138+
async fn find_all(Path((service, data_base, collection)): Path<(String, String, String)>) -> Result<Json<Vec<DTODocumentData>>, impl IntoResponse> {
139+
let o_db_service = Configuration::find_service(&service);
140+
if o_db_service.is_none() {
141+
return Err(utils::not_found());
142+
}
143+
144+
let result = o_db_service.unwrap().instance().await;
145+
if let Err(error) = result {
146+
let exception = ApiException::from(StatusCode::INTERNAL_SERVER_ERROR.as_u16(), error);
147+
return Err(exception.into_response());
148+
}
149+
150+
let query = DataBaseQuery::from(data_base, collection);
151+
152+
let documents = result.unwrap().find_all(&query).await;
153+
if let Err(error) = documents {
154+
let exception = ApiException::from(StatusCode::INTERNAL_SERVER_ERROR.as_u16(), error);
155+
return Err(exception.into_response());
156+
}
157+
158+
Ok(Json(documents.unwrap().iter()
159+
.map(|d| DTODocumentData::from(d))
160+
.collect())
161+
)
162+
}
163+
164+
async fn insert(Path((service, data_base, collection)): Path<(String, String, String)>, Json(dto): Json<DTOCreateDocument>) -> Result<Json<DTODocumentData>, impl IntoResponse> {
165+
let o_db_service = Configuration::find_service(&service);
166+
if o_db_service.is_none() {
167+
return Err(utils::not_found());
168+
}
169+
170+
let result = o_db_service.unwrap().instance().await;
171+
if let Err(error) = result {
172+
let exception = ApiException::from(StatusCode::INTERNAL_SERVER_ERROR.as_u16(), error);
173+
return Err(exception.into_response());
174+
}
175+
176+
let query = DataBaseQuery::from(data_base, collection);
177+
178+
let document = result.unwrap().insert(&query, &dto.document).await;
179+
if let Err(error) = document {
180+
let exception = ApiException::from(StatusCode::INTERNAL_SERVER_ERROR.as_u16(), error);
181+
return Err(exception.into_response());
182+
}
183+
184+
Ok(Json(DTODocumentData::from(&document.unwrap())))
185+
}
186+
187+
async fn update(Path((service, data_base, collection)): Path<(String, String, String)>, Json(dto): Json<DTOUpdateDocument>) -> Result<Json<Vec<DTODocumentData>>, impl IntoResponse> {
188+
let o_db_service = Configuration::find_service(&service);
189+
if o_db_service.is_none() {
190+
return Err(utils::not_found());
191+
}
192+
193+
let result = o_db_service.unwrap().instance().await;
194+
if let Err(error) = result {
195+
let exception = ApiException::from(StatusCode::INTERNAL_SERVER_ERROR.as_u16(), error);
196+
return Err(exception.into_response());
197+
}
198+
199+
let mut keys = Vec::new();
200+
for dto_key in dto.keys {
201+
let key = dto_key.from_dto();
202+
if let Err(exception) = key {
203+
return Err(exception.into_response());
204+
}
205+
keys.push(key.unwrap());
206+
}
207+
208+
let filter = document_keys_to_filter_element(keys);
209+
let query = DataBaseQuery::from_filter(data_base, collection, filter);
210+
211+
let documents = result.unwrap().update(&query, &dto.document).await;
212+
if let Err(error) = documents {
213+
let exception = ApiException::from(StatusCode::INTERNAL_SERVER_ERROR.as_u16(), error);
214+
return Err(exception.into_response());
215+
}
216+
217+
Ok(Json(documents.unwrap().iter()
218+
.map(|d| DTODocumentData::from(d))
219+
.collect())
220+
)
221+
}
222+
223+
async fn delete(Path((service, data_base, collection)): Path<(String, String, String)>, Json(dto): Json<Vec<DTODocumentKey>>) -> Result<Json<Vec<DTODocumentData>>, impl IntoResponse> {
224+
let o_db_service = Configuration::find_service(&service);
225+
if o_db_service.is_none() {
226+
return Err(utils::not_found());
227+
}
228+
229+
let result = o_db_service.unwrap().instance().await;
230+
if let Err(error) = result {
231+
let exception = ApiException::from(StatusCode::INTERNAL_SERVER_ERROR.as_u16(), error);
232+
return Err(exception.into_response());
233+
}
234+
235+
let mut keys = Vec::new();
236+
for dto_key in dto {
237+
let key = dto_key.from_dto();
238+
if let Err(exception) = key {
239+
return Err(exception.into_response());
240+
}
241+
keys.push(key.unwrap());
242+
}
243+
244+
let filter = document_keys_to_filter_element(keys);
245+
let query = DataBaseQuery::from_filter(data_base, collection, filter);
246+
247+
let documents = result.unwrap().delete(&query).await;
248+
if let Err(error) = documents {
249+
let exception = ApiException::from(StatusCode::INTERNAL_SERVER_ERROR.as_u16(), error);
250+
return Err(exception.into_response());
251+
}
252+
253+
Ok(Json(documents.unwrap().iter()
254+
.map(|d| DTODocumentData::from(d))
255+
.collect())
256+
)
257+
}
258+
114259
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
use rust_db_manager_core::domain::document::document_schema::DocumentSchema;
2+
use serde::Serialize;
3+
4+
use crate::infrastructure::dto::field::generate::dto_field_data::DTOFieldData;
5+
6+
#[derive(Clone, Serialize)]
7+
pub struct DTODocumentSchema {
8+
comments: Vec<String>,
9+
fields: Vec<DTOFieldData>
10+
}
11+
12+
impl DTODocumentSchema {
13+
14+
pub fn from(schema: &DocumentSchema) -> Self {
15+
Self {
16+
comments: schema.comments(),
17+
fields: schema.fields().iter()
18+
.map(|f| DTOFieldData::from(f))
19+
.collect()
20+
}
21+
}
22+
23+
}

0 commit comments

Comments
 (0)