2022-04-07 15:57:10 +00:00
|
|
|
use actix::Addr;
|
|
|
|
use actix_web::{HttpResponse, Responder, web};
|
|
|
|
|
2022-04-08 15:54:51 +00:00
|
|
|
use crate::actors::users_actor::{DeleteUserRequest, FindUserByUsername, UsersActor};
|
|
|
|
use crate::data::current_user::CurrentUser;
|
|
|
|
use crate::data::user::UserID;
|
2022-04-07 15:57:10 +00:00
|
|
|
|
|
|
|
#[derive(serde::Deserialize)]
|
|
|
|
pub struct FindUserNameReq {
|
|
|
|
username: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(serde::Serialize)]
|
|
|
|
struct FindUserResult {
|
|
|
|
user_id: Option<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn find_username(req: web::Form<FindUserNameReq>, users: web::Data<Addr<UsersActor>>) -> impl Responder {
|
|
|
|
let res = users.send(FindUserByUsername(req.0.username)).await.unwrap();
|
|
|
|
HttpResponse::Ok().json(FindUserResult {
|
|
|
|
user_id: res.0.map(|r| r.uid)
|
|
|
|
})
|
2022-04-08 15:54:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(serde::Deserialize)]
|
|
|
|
pub struct DeleteUserReq {
|
|
|
|
user_id: UserID,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub async fn delete_user(user: CurrentUser, req: web::Form<DeleteUserReq>,
|
|
|
|
users: web::Data<Addr<UsersActor>>) -> impl Responder {
|
|
|
|
if user.uid == req.user_id {
|
|
|
|
return HttpResponse::BadRequest().body("You can not remove your own account!");
|
|
|
|
}
|
|
|
|
|
|
|
|
let res = users.send(DeleteUserRequest(req.0.user_id)).await.unwrap();
|
|
|
|
if res.0 {
|
|
|
|
HttpResponse::Ok().finish()
|
|
|
|
} else {
|
|
|
|
HttpResponse::InternalServerError().finish()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|