@@ -11,50 +11,81 @@ fn crud_controller(name: &str, object: &str) -> String {
11
11
format ! ( "
12
12
use crate::{}::model::{}::*;
13
13
use crate::{}::database::Context;
14
+ use std::sync::{{Arc, Mutex}};
14
15
15
16
use actix_web::{{web, HttpResponse, Responder}};
16
17
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 {{
18
19
let id = uuid::Uuid::new_v4();
19
20
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
+ }}
21
32
}}
22
33
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)
25
38
}}
26
39
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 {{
28
41
let uuid = id.to_string();
29
42
30
43
if uuid::Uuid::parse_str(&uuid).is_err() {{
31
44
return HttpResponse::BadRequest().body(\" Id must be a Uuid::V4\" );
32
45
}}
33
46
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
+ }}
35
52
}}
36
53
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 {{
38
55
let uuid = id.to_string();
39
56
40
57
if uuid::Uuid::parse_str(&uuid).is_err() {{
41
58
return HttpResponse::BadRequest().body(\" Id must be a Uuid::V4\" );
42
59
}}
43
60
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
+ }}
45
66
}}
46
67
47
68
pub async fn update_{}(
48
69
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 {{
51
72
let uuid = id.to_string();
52
73
53
74
if uuid::Uuid::parse_str(&uuid).is_err() {{
54
75
return HttpResponse::BadRequest().body(\" Id must be a Uuid::V4\" );
55
76
}}
77
+
78
+ let bytes_str = match String::from_utf8(info.to_vec()) {{
79
+ Ok(text) => Ok(text),
80
+ Err(_) => Err(\" bytes parse error\" )
81
+ }};
56
82
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
+ }}
58
89
}}
59
90
" , name, name, name, object, object, object, object, object) . replace ( "@object" , & object. to_case ( Case :: Pascal ) )
60
91
}
0 commit comments