fistinc-auth 0.1.0

Paging types for Fist Inc bank
Documentation
use actix_web::{HttpResponse, web};
use email_address::EmailAddress;
use fistinc_errors::{ApiError, ApiResult, CommonError};
use fistinc_paging::{Paging, QueryParamsImpl};
use phonenumber::PhoneNumber;
use serde::{Deserialize, Serialize};
use uuid::Uuid;

use crate::api::models::UserIdentity;
use crate::domain::entity::{Role, User};
use crate::domain::services::UserService;

#[derive(Serialize, Deserialize)]
pub struct CreateUserRequest {
    email: EmailAddress,
    phone: PhoneNumber,
    role: Role,
    password: String,
}

impl Into<User> for CreateUserRequest {
    fn into(self) -> User {
        User {
            id: Uuid::new_v4(),
            email: self.email,
            phone: self.phone,
            role: self.role,
            password: self.password,
        }
    }
}

pub async fn create_user(
    user_service: web::Data<dyn UserService>,
    params: web::Json<CreateUserRequest>,
    identity: UserIdentity,
) -> ApiResult<web::Json<User>> {
    if identity.role != Role::Administrator {
        return Err(ApiError::from(CommonError {
            message: "Have not permission".to_string(),
            code: 401,
        }));
    }
    let user = params.0.into();
    let created_user = user_service.create(user).await?;
    Ok(web::Json(created_user))
}

pub async fn get_users(
    user_service: web::Data<dyn UserService>,
    params: web::Data<QueryParamsImpl>,
    identity: UserIdentity,
) -> ApiResult<web::Json<Paging<User>>> {
    if identity.role != Role::Administrator {
        return Err(ApiError::from(CommonError {
            message: "Have not permission".to_string(),
            code: 401,
        }));
    }
    let users = user_service.users(params.get_ref()).await?;
    Ok(web::Json(users))
}

pub async fn get_user_by_id(
    user_service: web::Data<dyn UserService>,
    user_id: web::Path<String>,
    identity: UserIdentity,
) -> ApiResult<web::Json<Option<User>>> {
    if identity.role != Role::Administrator {
        return Err(ApiError::from(CommonError {
            message: "Have not permission".to_string(),
            code: 401,
        }));
    }
    match Uuid::parse_str(&*user_id) {
        Ok(user_id) => {
            Ok(web::Json(user_service.find_by_id(user_id).await?))
        }
        Err(e) => Err(ApiError::from(CommonError {
            message: e.to_string(),
            code: 400,
        }))
    }
}

pub async fn delete_user(
    user_service: web::Data<dyn UserService>,
    user_id: web::Path<String>,
    identity: UserIdentity,
) -> Result<web::HttpResponse, ApiError> {
    if identity.role != Role::Administrator {
        return Err(ApiError::from(CommonError {
            message: "Have not permission".to_string(),
            code: 401,
        }));
    }
    match Uuid::parse_str(&*user_id) {
        Ok(user_id) => {
            user_service.delete_by_id(user_id).await?;
            Ok(HttpResponse::NoContent().finish())
        }
        Err(e) => Err(ApiError::from(CommonError {
            message: e.to_string(),
            code: 400,
        }))
    }
}