58 lines
2.1 KiB
Rust
58 lines
2.1 KiB
Rust
use std::sync::Arc;
|
|
use crate::lavina_clients::http_client::LavinaHttpClient;
|
|
pub use crate::models::models::User;
|
|
use serde_json::json;
|
|
use warp::Filter;
|
|
use crate::lavina_clients::errors::*;
|
|
|
|
pub struct CreateUserService {
|
|
lavina_client: LavinaHttpClient,
|
|
}
|
|
|
|
impl CreateUserService {
|
|
pub fn new(laina_http_client: LavinaHttpClient) -> Self {
|
|
CreateUserService { lavina_client: laina_http_client }
|
|
}
|
|
|
|
pub async fn create_user(&self, user: User) -> Result<impl warp::Reply, warp::Rejection> {
|
|
match self.lavina_client.new_player(&user.login, &user.password).await {
|
|
Ok(()) => {
|
|
let response = json!({"create_result": "User created"});
|
|
Ok::<_, warp::Rejection>(warp::reply::json(&response))
|
|
}
|
|
Err(err) => {
|
|
log::error!("Error creating user: {:?}", err);
|
|
let error_response = match err {
|
|
PlayerError::UserAlreadyExists(ref name) => {
|
|
json!({"error": format!("User '{}' already exists", name)})
|
|
}
|
|
PlayerError::RequestError(_) => {
|
|
json!({"error": "Error during request to lavina"})
|
|
}
|
|
PlayerError::Other(ref msg) => {
|
|
json!({"error": format!("Unknown error: {}", msg)})
|
|
}
|
|
};
|
|
Ok::<_, warp::Rejection>(warp::reply::json(&error_response))
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
pub fn create_user_route(self: Arc<Self>) -> impl Filter<Extract = (impl warp::Reply,), Error = warp::Rejection> + Clone {
|
|
warp::path("create-user")
|
|
.and(warp::post())
|
|
.and(warp::body::json())
|
|
.and_then({
|
|
let user_manager = Arc::clone(&self); // Клонируем Arc для использования в асинхронном блоке
|
|
move |user: User| {
|
|
let user_manager = Arc::clone(&user_manager);
|
|
async move {
|
|
user_manager.create_user(user).await
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|