chimes_rust/handler/
chimes_data_scope.rs

1use crate::entity::ChimesDataScope;
2use actix_web::{web, HttpResponse, Result};
3use chimes_auth::ApiResult;
4use chimes_utils::get_rbatis;
5/**
6 * Generate the file for chimes_dict_info.rs,
7 */
8use rbatis::Page;
9
10#[post("/api/v1/datascope/save")]
11pub async fn datascope_save(req: web::Json<ChimesDataScope>) -> Result<HttpResponse> {
12    let rb = get_rbatis();
13    let mut val = req.to_owned();
14    val.create_time = Some(rbatis::DateTimeNative::now());
15    val.update_time = Some(rbatis::DateTimeNative::now());
16    match val.save(rb).await {
17        Ok(_st) => {
18            let ret: web::Json<ApiResult<ChimesDataScope>> = web::Json(ApiResult::ok(val));
19            Ok(HttpResponse::Ok().json(ret))
20        }
21        Err(err) => {
22            let ret: web::Json<ApiResult<ChimesDataScope>> =
23                web::Json(ApiResult::error(5010, &err.to_string()));
24            Ok(HttpResponse::Ok().json(ret))
25        }
26    }
27}
28
29#[post("/api/v1/datascope/update")]
30async fn datascope_update(req: web::Json<ChimesDataScope>) -> Result<HttpResponse> {
31    let rb = get_rbatis();
32    let mut val = req.to_owned();
33    val.update_time = Some(rbatis::DateTimeNative::now());
34    match val.update_selective(rb).await {
35        Ok(_st) => {
36            let ret: web::Json<ApiResult<ChimesDataScope>> = web::Json(ApiResult::ok(val));
37            Ok(HttpResponse::Ok().json(ret))
38        }
39        Err(err) => {
40            let ret: web::Json<ApiResult<ChimesDataScope>> =
41                web::Json(ApiResult::error(5010, &err.to_string()));
42            Ok(HttpResponse::Ok().json(ret))
43        }
44    }
45}
46
47#[post("/api/v1/datascope/delete")]
48pub async fn datascope_delete(req: web::Json<ChimesDataScope>) -> Result<HttpResponse> {
49    let rb = get_rbatis();
50    let mut val = req.to_owned();
51    match val.remove(rb).await {
52        Ok(_st) => {
53            let ret: web::Json<ApiResult<ChimesDataScope>> = web::Json(ApiResult::ok(val));
54            Ok(HttpResponse::Ok().json(ret))
55        }
56        Err(err) => {
57            let ret: web::Json<ApiResult<ChimesDataScope>> =
58                web::Json(ApiResult::error(5010, &err.to_string()));
59            Ok(HttpResponse::Ok().json(ret))
60        }
61    }
62}
63
64#[post("/api/v1/datascope/delete_ids")]
65pub async fn datascope_delete_ids(req: web::Json<Vec<i64>>) -> Result<HttpResponse> {
66    let rb = get_rbatis();
67    let ids = req.as_slice();
68    match ChimesDataScope::remove_ids(rb, ids).await {
69        Ok(st) => {
70            let ret: web::Json<ApiResult<u64>> = web::Json(ApiResult::ok(st));
71            Ok(HttpResponse::Ok().json(ret))
72        }
73        Err(err) => {
74            let ret: web::Json<ApiResult<u64>> =
75                web::Json(ApiResult::error(5010, &err.to_string()));
76            Ok(HttpResponse::Ok().json(ret))
77        }
78    }
79}
80
81#[post("/api/v1/datascope/search")]
82pub async fn datascope_search(req: web::Json<ChimesDataScope>) -> Result<HttpResponse> {
83    let rb = get_rbatis();
84    let val = req.to_owned();
85    match val.query_list(rb).await {
86        Ok(st) => {
87            let ret: web::Json<ApiResult<Vec<ChimesDataScope>>> = web::Json(ApiResult::ok(st));
88            Ok(HttpResponse::Ok().json(ret))
89        }
90        Err(err) => {
91            let ret: web::Json<ApiResult<Vec<ChimesDataScope>>> =
92                web::Json(ApiResult::error(5010, &err.to_string()));
93            Ok(HttpResponse::Ok().json(ret))
94        }
95    }
96}
97
98#[post("/api/v1/datascope/paged/{current}/{size}")]
99pub async fn datascope_paged(
100    req: web::Json<ChimesDataScope>,
101    path_param: web::Path<(u64, u64)>,
102) -> Result<HttpResponse> {
103    let rb = get_rbatis();
104    let val = req.to_owned();
105    let (current, size) = path_param.into_inner();
106    match val.query_paged(rb, current, size).await {
107        Ok(st) => {
108            let ret: web::Json<ApiResult<Page<ChimesDataScope>>> = web::Json(ApiResult::ok(st));
109            Ok(HttpResponse::Ok().json(ret))
110        }
111        Err(err) => {
112            let ret: web::Json<ApiResult<Page<ChimesDataScope>>> =
113                web::Json(ApiResult::error(5010, &err.to_string()));
114            Ok(HttpResponse::Ok().json(ret))
115        }
116    }
117}
118
119#[get("/api/v1/datascope/get/{id}")]
120pub async fn datascope_get(dict_id_req: web::Path<i64>) -> Result<HttpResponse> {
121    let rb = get_rbatis();
122    let dict_id = dict_id_req.to_owned();
123    match ChimesDataScope::from_id(rb, &dict_id).await {
124        Ok(st) => match st {
125            Some(tv) => {
126                let ret: web::Json<ApiResult<ChimesDataScope>> = web::Json(ApiResult::ok(tv));
127                Ok(HttpResponse::Ok().json(ret))
128            }
129            None => {
130                let ret: web::Json<ApiResult<ChimesDataScope>> =
131                    web::Json(ApiResult::error(5040, &"Not-Found".to_string()));
132                Ok(HttpResponse::Ok().json(ret))
133            }
134        },
135        Err(err) => {
136            let ret: web::Json<ApiResult<ChimesDataScope>> =
137                web::Json(ApiResult::error(5010, &err.to_string()));
138            Ok(HttpResponse::Ok().json(ret))
139        }
140    }
141}