@@ -18,8 +18,7 @@ use crate::commons::exception::api_exception::ApiException;
1818
1919use super :: {
2020 dto:: {
21- collection:: dto_generate_collection_query:: DTOGenerateCollectionQuery ,
22- table:: dto_table_data_group:: DTOTableDataGroup ,
21+ collection:: { dto_generate_collection_query:: DTOGenerateCollectionQuery , dto_rename_collection_query:: DTORenameCollectionQuery } , document:: { dto_document_data:: DTODocumentData , dto_document_schema:: DTODocumentSchema } , table:: dto_table_data_group:: DTOTableDataGroup
2322 } ,
2423 handler, utils,
2524} ;
@@ -31,19 +30,23 @@ impl ControllerCollection {
3130
3231 pub fn route ( router : Router ) -> Router {
3332 router
34- . route ( "/:service/data-base/:data_base/metadata" , get ( Self :: metadata) )
35- . route ( "/:service/data-base/:data_base/collection" , get ( Self :: find_all) )
36- . route ( "/:service/data-base/:data_base/collection" , post ( Self :: insert) )
37- . route ( "/:service/data-base/:data_base/collection/:collection" , delete ( Self :: delete) )
33+ . route ( "/api/v1/service/:service/data-base/:data_base/collection" , get ( Self :: find_all) )
34+ . route ( "/api/v1/service/:service/data-base/:data_base/collection" , post ( Self :: insert) )
35+ . route ( "/api/v1/service/:service/data-base/:data_base/collection/:collection" , delete ( Self :: delete) )
36+ . route ( "/api/v1/service/:service/data-base/:data_base/collection/:collection/metadata" , get ( Self :: metadata) )
37+ . route ( "/api/v1/service/:service/data-base/:data_base/collection/:collection/schema" , get ( Self :: schema) )
38+ . route ( "/api/v1/service/:service/data-base/:data_base/collection/:collection/rename" , post ( Self :: rename) )
39+ . route ( "/api/v1/service/:service/data-base/:data_base/collection/:collection/export" , get ( Self :: export) )
40+ . route ( "/api/v1/service/:service/data-base/:data_base/collection/:collection/import" , post ( Self :: import) )
3841 . route_layer ( middleware:: from_fn ( handler:: autentication_handler) )
3942 }
4043
41- async fn metadata ( Path ( ( service, data_base) ) : Path < ( String , String ) > ) -> Result < Json < Vec < DTOTableDataGroup > > , impl IntoResponse > {
44+ async fn find_all ( Path ( ( service, data_base) ) : Path < ( String , String ) > ) -> Result < Json < Vec < String > > , impl IntoResponse > {
4245 let o_db_service = Configuration :: find_service ( & service) ;
4346 if o_db_service. is_none ( ) {
4447 return Err ( utils:: not_found ( ) ) ;
4548 }
46-
49+
4750 let result = o_db_service. unwrap ( ) . instance ( ) . await ;
4851 if let Err ( error) = result {
4952 let exception = ApiException :: from ( StatusCode :: INTERNAL_SERVER_ERROR . as_u16 ( ) , error) ;
@@ -52,7 +55,79 @@ impl ControllerCollection {
5255
5356 let query = DataBaseQuery :: from_data_base ( data_base) ;
5457
55- let metadata = result. unwrap ( ) . data_base_metadata ( & query) . await ;
58+ let collections = result. unwrap ( ) . collection_find_all ( & query) . await ;
59+ if let Err ( error) = collections {
60+ let exception = ApiException :: from ( StatusCode :: INTERNAL_SERVER_ERROR . as_u16 ( ) , error) ;
61+ return Err ( exception. into_response ( ) ) ;
62+ }
63+
64+ Ok ( Json ( collections. unwrap ( ) ) )
65+ }
66+
67+ async fn insert ( Path ( ( service, _) ) : Path < ( String , String ) > , Json ( dto) : Json < DTOGenerateCollectionQuery > ) -> Result < StatusCode , impl IntoResponse > {
68+ let o_db_service = Configuration :: find_service ( & service) ;
69+ if o_db_service. is_none ( ) {
70+ return Err ( utils:: not_found ( ) ) ;
71+ }
72+
73+ let result = o_db_service. unwrap ( ) . instance ( ) . await ;
74+ if let Err ( error) = result {
75+ let exception = ApiException :: from ( StatusCode :: INTERNAL_SERVER_ERROR . as_u16 ( ) , error) ;
76+ return Err ( exception. into_response ( ) ) ;
77+ }
78+
79+ let query = dto. from_dto ( ) ;
80+ if let Err ( exception) = query {
81+ return Err ( exception. into_response ( ) ) ;
82+ }
83+
84+ let collection = result. unwrap ( ) . collection_create ( & query. unwrap ( ) ) . await ;
85+ if let Err ( error) = collection {
86+ let exception = ApiException :: from ( StatusCode :: INTERNAL_SERVER_ERROR . as_u16 ( ) , error) ;
87+ return Err ( exception. into_response ( ) ) ;
88+ }
89+
90+ Ok ( StatusCode :: ACCEPTED )
91+ }
92+
93+ async fn delete ( Path ( ( service, data_base, collection) ) : Path < ( String , String , String ) > ) -> Result < StatusCode , impl IntoResponse > {
94+ let o_db_service = Configuration :: find_service ( & service) ;
95+ if o_db_service. is_none ( ) {
96+ return Err ( utils:: not_found ( ) ) ;
97+ }
98+
99+ let result = o_db_service. unwrap ( ) . instance ( ) . await ;
100+ if let Err ( error) = result {
101+ let exception = ApiException :: from ( StatusCode :: INTERNAL_SERVER_ERROR . as_u16 ( ) , error) ;
102+ return Err ( exception. into_response ( ) ) ;
103+ }
104+
105+ let query = GenerateCollectionQuery :: from_collection ( data_base, collection) ;
106+
107+ let collection = result. unwrap ( ) . collection_drop ( & query) . await ;
108+ if let Err ( error) = collection {
109+ let exception = ApiException :: from ( StatusCode :: INTERNAL_SERVER_ERROR . as_u16 ( ) , error) ;
110+ return Err ( exception. into_response ( ) ) ;
111+ }
112+
113+ Ok ( StatusCode :: ACCEPTED )
114+ }
115+
116+ async fn metadata ( Path ( ( service, data_base, collection) ) : Path < ( String , String , String ) > ) -> Result < Json < Vec < DTOTableDataGroup > > , impl IntoResponse > {
117+ let o_db_service = Configuration :: find_service ( & service) ;
118+ if o_db_service. is_none ( ) {
119+ return Err ( utils:: not_found ( ) ) ;
120+ }
121+
122+ let result = o_db_service. unwrap ( ) . instance ( ) . await ;
123+ if let Err ( error) = result {
124+ let exception = ApiException :: from ( StatusCode :: INTERNAL_SERVER_ERROR . as_u16 ( ) , error) ;
125+ return Err ( exception. into_response ( ) ) ;
126+ }
127+
128+ let query = DataBaseQuery :: from ( data_base, collection) ;
129+
130+ let metadata = result. unwrap ( ) . collection_metadata ( & query) . await ;
56131 if let Err ( error) = metadata {
57132 let exception = ApiException :: from ( StatusCode :: INTERNAL_SERVER_ERROR . as_u16 ( ) , error) ;
58133 return Err ( exception. into_response ( ) ) ;
@@ -65,7 +140,7 @@ impl ControllerCollection {
65140 Ok ( Json ( dto) )
66141 }
67142
68- async fn find_all ( Path ( ( service, data_base) ) : Path < ( String , String ) > ) -> Result < Json < Vec < String > > , impl IntoResponse > {
143+ async fn schema ( Path ( ( service, data_base, collection ) ) : Path < ( String , String , String ) > ) -> Result < Json < DTODocumentSchema > , impl IntoResponse > {
69144 let o_db_service = Configuration :: find_service ( & service) ;
70145 if o_db_service. is_none ( ) {
71146 return Err ( utils:: not_found ( ) ) ;
@@ -77,18 +152,18 @@ impl ControllerCollection {
77152 return Err ( exception. into_response ( ) ) ;
78153 }
79154
80- let query = DataBaseQuery :: from_data_base ( data_base) ;
155+ let query = DataBaseQuery :: from ( data_base, collection ) ;
81156
82- let collections = result. unwrap ( ) . collection_find_all ( & query) . await ;
83- if let Err ( error) = collections {
157+ let schema = result. unwrap ( ) . schema ( & query) . await ;
158+ if let Err ( error) = schema {
84159 let exception = ApiException :: from ( StatusCode :: INTERNAL_SERVER_ERROR . as_u16 ( ) , error) ;
85160 return Err ( exception. into_response ( ) ) ;
86161 }
87162
88- Ok ( Json ( collections . unwrap ( ) ) )
163+ Ok ( Json ( DTODocumentSchema :: from ( & schema . unwrap ( ) ) ) )
89164 }
90165
91- async fn insert ( Path ( ( service, _ ) ) : Path < ( String , String ) > , Json ( dto) : Json < DTOGenerateCollectionQuery > ) -> Result < StatusCode , impl IntoResponse > {
166+ async fn rename ( Path ( ( service, data_base , collection ) ) : Path < ( String , String , String ) > , Json ( dto) : Json < DTORenameCollectionQuery > ) -> Result < StatusCode , impl IntoResponse > {
92167 let o_db_service = Configuration :: find_service ( & service) ;
93168 if o_db_service. is_none ( ) {
94169 return Err ( utils:: not_found ( ) ) ;
@@ -100,21 +175,44 @@ impl ControllerCollection {
100175 return Err ( exception. into_response ( ) ) ;
101176 }
102177
103- let query = dto. from_dto ( ) ;
104- if let Err ( exception) = query {
178+ let query = DataBaseQuery :: from ( data_base, collection) ;
179+
180+ let documents = result. unwrap ( ) . collection_rename ( & query, & dto. collection ) . await ;
181+ if let Err ( error) = documents {
182+ let exception = ApiException :: from ( StatusCode :: INTERNAL_SERVER_ERROR . as_u16 ( ) , error) ;
105183 return Err ( exception. into_response ( ) ) ;
106184 }
185+
186+ Ok ( StatusCode :: OK )
187+ }
107188
108- let collection = result. unwrap ( ) . collection_create ( & query. unwrap ( ) ) . await ;
109- if let Err ( error) = collection {
189+ async fn export ( Path ( ( service, data_base, collection) ) : Path < ( String , String , String ) > ) -> Result < Json < Vec < DTODocumentData > > , impl IntoResponse > {
190+ let o_db_service = Configuration :: find_service ( & service) ;
191+ if o_db_service. is_none ( ) {
192+ return Err ( utils:: not_found ( ) ) ;
193+ }
194+
195+ let result = o_db_service. unwrap ( ) . instance ( ) . await ;
196+ if let Err ( error) = result {
197+ let exception = ApiException :: from ( StatusCode :: INTERNAL_SERVER_ERROR . as_u16 ( ) , error) ;
198+ return Err ( exception. into_response ( ) ) ;
199+ }
200+
201+ let query = DataBaseQuery :: from ( data_base, collection) ;
202+
203+ let documents = result. unwrap ( ) . collection_export ( & query) . await ;
204+ if let Err ( error) = documents {
110205 let exception = ApiException :: from ( StatusCode :: INTERNAL_SERVER_ERROR . as_u16 ( ) , error) ;
111206 return Err ( exception. into_response ( ) ) ;
112207 }
113208
114- Ok ( StatusCode :: ACCEPTED )
209+ Ok ( Json ( documents. unwrap ( ) . iter ( )
210+ . map ( |d| DTODocumentData :: from ( d) )
211+ . collect ( ) )
212+ )
115213 }
116214
117- async fn delete ( Path ( ( service, data_base, collection) ) : Path < ( String , String , String ) > ) -> Result < StatusCode , impl IntoResponse > {
215+ async fn import ( Path ( ( service, data_base, collection) ) : Path < ( String , String , String ) > , documents : Json < Vec < String > > ) -> Result < StatusCode , impl IntoResponse > {
118216 let o_db_service = Configuration :: find_service ( & service) ;
119217 if o_db_service. is_none ( ) {
120218 return Err ( utils:: not_found ( ) ) ;
@@ -126,15 +224,15 @@ impl ControllerCollection {
126224 return Err ( exception. into_response ( ) ) ;
127225 }
128226
129- let query = GenerateCollectionQuery :: from_collection ( data_base, collection) ;
227+ let query = DataBaseQuery :: from ( data_base, collection) ;
130228
131- let collection = result. unwrap ( ) . collection_drop ( & query) . await ;
132- if let Err ( error) = collection {
229+ let result = result. unwrap ( ) . collection_import ( & query, documents . to_vec ( ) ) . await ;
230+ if let Err ( error) = result {
133231 let exception = ApiException :: from ( StatusCode :: INTERNAL_SERVER_ERROR . as_u16 ( ) , error) ;
134232 return Err ( exception. into_response ( ) ) ;
135233 }
136234
137- Ok ( StatusCode :: ACCEPTED )
235+ Ok ( StatusCode :: OK )
138236 }
139237
140238}
0 commit comments