Skip to main content

fistinc_auth/api/controllers/
user.rs

1use actix_web::{HttpResponse, web};
2use email_address::EmailAddress;
3use fistinc_errors::{ApiError, ApiResult, CommonError};
4use fistinc_paging::{Paging, QueryParamsImpl};
5use phonenumber::PhoneNumber;
6use serde::{Deserialize, Serialize};
7use uuid::Uuid;
8
9use crate::api::models::UserIdentity;
10use crate::domain::entity::{Role, User};
11use crate::domain::services::UserService;
12
13#[derive(Serialize, Deserialize)]
14pub struct CreateUserRequest {
15    email: EmailAddress,
16    phone: PhoneNumber,
17    role: Role,
18    password: String,
19}
20
21impl Into<User> for CreateUserRequest {
22    fn into(self) -> User {
23        User {
24            id: Uuid::new_v4(),
25            email: self.email,
26            phone: self.phone,
27            role: self.role,
28            password: self.password,
29        }
30    }
31}
32
33pub async fn create_user(
34    user_service: web::Data<dyn UserService>,
35    params: web::Json<CreateUserRequest>,
36    identity: UserIdentity,
37) -> ApiResult<web::Json<User>> {
38    if identity.role != Role::Administrator {
39        return Err(ApiError::from(CommonError {
40            message: "Have not permission".to_string(),
41            code: 401,
42        }));
43    }
44    let user = params.0.into();
45    let created_user = user_service.create(user).await?;
46    Ok(web::Json(created_user))
47}
48
49pub async fn get_users(
50    user_service: web::Data<dyn UserService>,
51    params: web::Data<QueryParamsImpl>,
52    identity: UserIdentity,
53) -> ApiResult<web::Json<Paging<User>>> {
54    if identity.role != Role::Administrator {
55        return Err(ApiError::from(CommonError {
56            message: "Have not permission".to_string(),
57            code: 401,
58        }));
59    }
60    let users = user_service.users(params.get_ref()).await?;
61    Ok(web::Json(users))
62}
63
64pub async fn get_user_by_id(
65    user_service: web::Data<dyn UserService>,
66    user_id: web::Path<String>,
67    identity: UserIdentity,
68) -> ApiResult<web::Json<Option<User>>> {
69    if identity.role != Role::Administrator {
70        return Err(ApiError::from(CommonError {
71            message: "Have not permission".to_string(),
72            code: 401,
73        }));
74    }
75    match Uuid::parse_str(&*user_id) {
76        Ok(user_id) => {
77            Ok(web::Json(user_service.find_by_id(user_id).await?))
78        }
79        Err(e) => Err(ApiError::from(CommonError {
80            message: e.to_string(),
81            code: 400,
82        }))
83    }
84}
85
86pub async fn delete_user(
87    user_service: web::Data<dyn UserService>,
88    user_id: web::Path<String>,
89    identity: UserIdentity,
90) -> Result<web::HttpResponse, ApiError> {
91    if identity.role != Role::Administrator {
92        return Err(ApiError::from(CommonError {
93            message: "Have not permission".to_string(),
94            code: 401,
95        }));
96    }
97    match Uuid::parse_str(&*user_id) {
98        Ok(user_id) => {
99            user_service.delete_by_id(user_id).await?;
100            Ok(HttpResponse::NoContent().finish())
101        }
102        Err(e) => Err(ApiError::from(CommonError {
103            message: e.to_string(),
104            code: 400,
105        }))
106    }
107}