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
416use 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
831pub 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}
0 commit comments