user.rs
2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
use crate::models::user::{self, Action};
use crate::AppData;
use actix_web::{Error, HttpResponse, web};
use anyhow::Result;
pub async fn create_user( app_data: web::Data<AppData>
, item: web::Json<user::UserJson> )
-> Result<HttpResponse, Error>
{
let pool = app_data.database_pool.clone();
let item = item.into_inner();
Ok(web::block(move || user::create_user(pool, item))
. await
. map(|action| {
match action {
Action::Created(user) => HttpResponse::Created().json(user),
Action::Found(user) => HttpResponse::Ok().json(user),
}})
. map_err(|_| HttpResponse::InternalServerError())?)
}
pub async fn get_users(app_data: web::Data<AppData>)
-> Result<HttpResponse, Error>
{
let pool = app_data.database_pool.clone();
Ok(web::block(move || user::get_users(pool))
. await
. map(|users| HttpResponse::Ok().json(users))
. map_err(|_| HttpResponse::InternalServerError())?)
}
pub async fn get_user(app_data: web::Data<AppData>, id: web::Path<i32>)
-> Result<HttpResponse, Error>
{
let pool = app_data.database_pool.clone();
let id = id.into_inner();
Ok(web::block(move || user::get_user(pool, id))
. await
. map(|user| HttpResponse::Ok().json(user))
. map_err(|_| HttpResponse::InternalServerError())?)
}
pub async fn delete_user(app_data: web::Data<AppData>, id: web::Path<i32>)
-> Result<HttpResponse, Error>
{
let pool = app_data.database_pool.clone();
let id = id.into_inner();
Ok(web::block(move || user::delete_user(pool, id))
. await
. map(|_| HttpResponse::NoContent().finish())
. map_err(|_| HttpResponse::InternalServerError())?)
}
pub async fn update_user( app_data: web::Data<AppData>
, id: web::Path<i32>
, item: web::Json<user::UserJson> )
-> Result<HttpResponse, Error>
{
let pool = app_data.database_pool.clone();
let id = id.into_inner();
let item = item.into_inner();
Ok(web::block(move || user::update_user(pool, id, item))
. await
. map(|user| HttpResponse::Ok().json(user))
. map_err(|_| HttpResponse::InternalServerError())?)
}