use actix::Addr; use actix_web::{HttpResponse, Responder, web}; use crate::actors::users_actor::{DeleteUserRequest, FindUserByUsername, UsersActor}; use crate::data::current_user::CurrentUser; use crate::data::user::UserID; #[derive(serde::Deserialize)] pub struct FindUserNameReq { username: String, } #[derive(serde::Serialize)] struct FindUserResult { user_id: Option, } pub async fn find_username(req: web::Form, users: web::Data>) -> impl Responder { let res = users.send(FindUserByUsername(req.0.username)).await.unwrap(); HttpResponse::Ok().json(FindUserResult { user_id: res.0.map(|r| r.uid) }) } #[derive(serde::Deserialize)] pub struct DeleteUserReq { user_id: UserID, } pub async fn delete_user(user: CurrentUser, req: web::Form, users: web::Data>) -> 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() } }