routes.rs
2.48 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
73
74
75
76
77
78
79
80
81
82
use crate::models::{self, Action};
use crate::Pool;
use actix_web::{Error, HttpResponse, web};
use anyhow::Result;
pub async fn root() -> Result<actix_files::NamedFile, Error> {
Ok(actix_files::NamedFile::open("static/index.html")?)
}
pub async fn p404() -> Result<actix_files::NamedFile, Error> {
Ok(actix_files::NamedFile::open("static/404.html")?)
}
pub async fn favicon() -> Result<actix_files::NamedFile, Error> {
Ok(actix_files::NamedFile::open("static/favicon.ico")?)
}
pub async fn create_user( pool: web::Data<Pool>
, item: web::Json<models::UserJson> )
-> Result<HttpResponse, Error>
{
let pool = pool.into_inner();
let item = item.into_inner();
Ok(web::block(move || models::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(pool: web::Data<Pool>)
-> Result<HttpResponse, Error>
{
Ok(web::block(move || models::get_users(pool.into_inner()))
. await
. map(|users| HttpResponse::Ok().json(users))
. map_err(|_| HttpResponse::InternalServerError())?)
}
pub async fn get_user(pool: web::Data<Pool>, id: web::Path<i32>)
-> Result<HttpResponse, Error>
{
let pool = pool.into_inner();
let id = id.into_inner();
Ok(web::block(move || models::get_user(pool, id))
. await
. map(|user| HttpResponse::Ok().json(user))
. map_err(|_| HttpResponse::InternalServerError())?)
}
pub async fn delete_user(pool: web::Data<Pool>, id: web::Path<i32>)
-> Result<HttpResponse, Error>
{
let pool = pool.into_inner();
let id = id.into_inner();
Ok(web::block(move || models::delete_user(pool, id))
. await
. map(|_| HttpResponse::NoContent().finish())
. map_err(|_| HttpResponse::InternalServerError())?)
}
pub async fn update_user( pool: web::Data<Pool>
, id: web::Path<i32>
, item: web::Json<models::UserJson> )
-> Result<HttpResponse, Error>
{
let pool = pool.into_inner();
let id = id.into_inner();
let item = item.into_inner();
Ok(web::block(move || models::update_user(pool, id, item))
. await
. map(|user| HttpResponse::Ok().json(user))
. map_err(|_| HttpResponse::InternalServerError())?)
}