@@ -11,50 +11,81 @@ fn crud_controller(name: &str, object: &str) -> String {
1111 format ! ( "
1212 use crate::{}::model::{}::*;
1313 use crate::{}::database::Context;
14+ use std::sync::{{Arc, Mutex}};
1415
1516 use actix_web::{{web, HttpResponse, Responder}};
1617
17- pub async fn create_{}(state: web::Data<Context>, info: web::Json<@object> ) -> impl Responder {{
18+ pub async fn create_{}(state: web::Data<Arc<Mutex< Context>>> , info: web::Bytes ) -> impl Responder {{
1819 let id = uuid::Uuid::new_v4();
1920
20- unimplemented!()
21+ let bytes_str = match String::from_utf8(info.to_vec()) {{
22+ Ok(text) => Ok(text),
23+ Err(_) => Err(\" bytes parse error\" )
24+ }};
25+
26+ let info = serde_json::from_str(&bytes_str.unwrap()).unwrap();
27+ let mut state = state.lock().unwrap();
28+ match state.create(id.to_string(), info) {{
29+ true => HttpResponse::Created().body(id.to_string()),
30+ false => HttpResponse::Conflict().finish(),
31+ }}
2132 }}
2233
23- pub async fn show_{}(state: web::Data<Context>) -> impl Responder {{
24- unimplemented!()
34+ pub async fn show_{}(state: web::Data<Arc<Mutex<Context>>>) -> impl Responder {{
35+ let mut state = state.lock().unwrap();
36+ let response = state.all();
37+ HttpResponse::Ok().json(response)
2538 }}
2639
27- pub async fn delete_{}(id: web::Path<String>, state: web::Data<Context>) -> impl Responder {{
40+ pub async fn delete_{}(id: web::Path<String>, state: web::Data<Arc<Mutex< Context>> >) -> impl Responder {{
2841 let uuid = id.to_string();
2942
3043 if uuid::Uuid::parse_str(&uuid).is_err() {{
3144 return HttpResponse::BadRequest().body(\" Id must be a Uuid::V4\" );
3245 }}
3346
34- unimplemented!()
47+ let mut state = state.lock().unwrap();
48+ match state.delete(id.to_string()) {{
49+ true => HttpResponse::Ok().finish(),
50+ false => HttpResponse::BadRequest().finish(),
51+ }}
3552 }}
3653
37- pub async fn get_{}(id: web::Path<String>, state: web::Data<Context>) -> impl Responder {{
54+ pub async fn get_{}(id: web::Path<String>, state: web::Data<Arc<Mutex< Context>> >) -> impl Responder {{
3855 let uuid = id.to_string();
3956
4057 if uuid::Uuid::parse_str(&uuid).is_err() {{
4158 return HttpResponse::BadRequest().body(\" Id must be a Uuid::V4\" );
4259 }}
4360
44- unimplemented!()
61+ let mut state = state.lock().unwrap();
62+ match state.get(uuid) {{
63+ Some(body) => HttpResponse::Ok().json(body),
64+ None => HttpResponse::BadRequest().finish(),
65+ }}
4566 }}
4667
4768 pub async fn update_{}(
4869 id: web::Path<String>,
49- info: web::Json<@objectUpdate> ,
50- state: web::Data<Context>) -> impl Responder {{
70+ info: web::Bytes ,
71+ state: web::Data<Arc<Mutex< Context>> >) -> impl Responder {{
5172 let uuid = id.to_string();
5273
5374 if uuid::Uuid::parse_str(&uuid).is_err() {{
5475 return HttpResponse::BadRequest().body(\" Id must be a Uuid::V4\" );
5576 }}
77+
78+ let bytes_str = match String::from_utf8(info.to_vec()) {{
79+ Ok(text) => Ok(text),
80+ Err(_) => Err(\" bytes parse error\" )
81+ }};
5682
57- unimplemented!()
83+ let info: ObjectUpdate = serde_json::from_str(&bytes_str.unwrap()).unwrap();
84+ let mut state = state.lock().unwrap();
85+ match state.update(id.to_string(), info) {{
86+ true => HttpResponse::Ok().finish(),
87+ false => HttpResponse::BadRequest().finish(),
88+ }}
5889 }}
5990" , name, name, name, object, object, object, object, object) . replace ( "@object" , & object. to_case ( Case :: Pascal ) )
6091}
0 commit comments