chimes_rust/handler/
chimes_job_info.rs1use crate::entity::{ChimesJobInfo, ChimesJobInfoQuery};
2use actix_web::{web, HttpResponse, Result};
3use chimes_auth::ApiResult;
4use chimes_utils::get_rbatis;
5use rbatis::Page;
9
10#[post("/api/v1/job/save")]
11pub async fn job_save(req: web::Json<ChimesJobInfo>) -> 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<ChimesJobInfo>> = web::Json(ApiResult::ok(val));
19 Ok(HttpResponse::Ok().json(ret))
20 }
21 Err(err) => {
22 let ret: web::Json<ApiResult<ChimesJobInfo>> =
23 web::Json(ApiResult::error(5010, &err.to_string()));
24 Ok(HttpResponse::Ok().json(ret))
25 }
26 }
27}
28
29#[post("/api/v1/job/update")]
30async fn job_update(req: web::Json<ChimesJobInfo>) -> 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<ChimesJobInfo>> = web::Json(ApiResult::ok(val));
37 Ok(HttpResponse::Ok().json(ret))
38 }
39 Err(err) => {
40 let ret: web::Json<ApiResult<ChimesJobInfo>> =
41 web::Json(ApiResult::error(5010, &err.to_string()));
42 Ok(HttpResponse::Ok().json(ret))
43 }
44 }
45}
46
47#[post("/api/v1/job/delete")]
48pub async fn job_delete(req: web::Json<ChimesJobInfo>) -> 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<ChimesJobInfo>> = web::Json(ApiResult::ok(val));
54 Ok(HttpResponse::Ok().json(ret))
55 }
56 Err(err) => {
57 let ret: web::Json<ApiResult<ChimesJobInfo>> =
58 web::Json(ApiResult::error(5010, &err.to_string()));
59 Ok(HttpResponse::Ok().json(ret))
60 }
61 }
62}
63
64#[post("/api/v1/job/delete_ids")]
65pub async fn job_delete_ids(req: web::Json<Vec<i64>>) -> Result<HttpResponse> {
66 let rb = get_rbatis();
67 let ids = req.as_slice();
68 match ChimesJobInfo::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/job/search")]
82pub async fn job_search(req: web::Json<ChimesJobInfoQuery>) -> 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<ChimesJobInfo>>> = web::Json(ApiResult::ok(st));
88 Ok(HttpResponse::Ok().json(ret))
89 }
90 Err(err) => {
91 let ret: web::Json<ApiResult<Vec<ChimesJobInfo>>> =
92 web::Json(ApiResult::error(5010, &err.to_string()));
93 Ok(HttpResponse::Ok().json(ret))
94 }
95 }
96}
97
98#[post("/api/v1/job/paged/{current}/{size}")]
99pub async fn job_paged(
100 req: web::Json<ChimesJobInfoQuery>,
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<ChimesJobInfo>>> = web::Json(ApiResult::ok(st));
109 Ok(HttpResponse::Ok().json(ret))
110 }
111 Err(err) => {
112 let ret: web::Json<ApiResult<Page<ChimesJobInfo>>> =
113 web::Json(ApiResult::error(5010, &err.to_string()));
114 Ok(HttpResponse::Ok().json(ret))
115 }
116 }
117}
118
119#[get("/api/v1/job/get/{id}")]
120pub async fn job_get(job_id_req: web::Path<i64>) -> Result<HttpResponse> {
121 let rb = get_rbatis();
122 let job_id = job_id_req.to_owned();
123 match ChimesJobInfo::from_id(rb, &job_id).await {
124 Ok(st) => match st {
125 Some(tv) => {
126 let ret: web::Json<ApiResult<ChimesJobInfo>> = web::Json(ApiResult::ok(tv));
127 Ok(HttpResponse::Ok().json(ret))
128 }
129 None => {
130 let ret: web::Json<ApiResult<ChimesJobInfo>> =
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<ChimesJobInfo>> =
137 web::Json(ApiResult::error(5010, &err.to_string()));
138 Ok(HttpResponse::Ok().json(ret))
139 }
140 }
141}