From 03eb86113140056b35d8f0690ecb0fbe414431a7 Mon Sep 17 00:00:00 2001 From: Nikita Vilunov Date: Mon, 29 Apr 2024 02:21:21 +0200 Subject: [PATCH] fix --- src/http.rs | 46 ++++++++++++++++++---------------------------- 1 file changed, 18 insertions(+), 28 deletions(-) diff --git a/src/http.rs b/src/http.rs index 84dbfdc..7543ed8 100644 --- a/src/http.rs +++ b/src/http.rs @@ -86,9 +86,9 @@ async fn route( (&Method::GET, "/metrics") => endpoint_metrics(registry), (&Method::GET, "/rooms") => endpoint_rooms(core.rooms).await, (&Method::POST, paths::CREATE_PLAYER) => endpoint_create_player(request, storage).await.or5xx(), - (&Method::POST, paths::STOP_PLAYER) => endpoint_stop_player(request, storage).await.or5xx(), + (&Method::POST, paths::STOP_PLAYER) => endpoint_stop_player(request, core.players).await.or5xx(), (&Method::POST, paths::SET_PASSWORD) => endpoint_set_password(request, storage).await.or5xx(), - _ => not_found(), + _ => endpoint_not_found(), }; Ok(res) } @@ -131,27 +131,13 @@ async fn endpoint_stop_player( return Ok(malformed_request()); }; let Ok(player_id) = PlayerId::from(res.name) else { - let payload = ErrorResponse { - code: errors::PLAYER_NOT_FOUND, - message: "No such player exists", - } - .to_body(); - let mut response = Response::new(payload); - *response.status_mut() = StatusCode::UNPROCESSABLE_ENTITY; - return Ok(response); + return Ok(player_not_found()); }; let Some(()) = players.stop_player(&player_id).await? else { - let payload = ErrorResponse { - code: errors::PLAYER_NOT_FOUND, - message: "No such player exists", - } - .to_body(); - let mut response = Response::new(payload); - *response.status_mut() = StatusCode::UNPROCESSABLE_ENTITY; - return Ok(response); + return Ok(player_not_found()); }; let mut response = Response::new(Full::::default()); - *response.status_mut() = StatusCode::CREATED; + *response.status_mut() = StatusCode::NO_CONTENT; Ok(response) } @@ -167,14 +153,7 @@ async fn endpoint_set_password( match verdict { UpdatePasswordResult::PasswordUpdated => {} UpdatePasswordResult::UserNotFound => { - let payload = ErrorResponse { - code: errors::PLAYER_NOT_FOUND, - message: "No such player exists", - } - .to_body(); - let mut response = Response::new(payload); - *response.status_mut() = StatusCode::UNPROCESSABLE_ENTITY; - return Ok(response); + return Ok(player_not_found()); } } let mut response = Response::new(Full::::default()); @@ -182,7 +161,7 @@ async fn endpoint_set_password( Ok(response) } -pub fn not_found() -> Response> { +fn endpoint_not_found() -> Response> { let payload = ErrorResponse { code: errors::INVALID_PATH, message: "The path does not exist", @@ -194,6 +173,17 @@ pub fn not_found() -> Response> { response } +fn player_not_found() -> Response> { + let payload = ErrorResponse { + code: errors::PLAYER_NOT_FOUND, + message: "No such player exists", + } + .to_body(); + let mut response = Response::new(payload); + *response.status_mut() = StatusCode::UNPROCESSABLE_ENTITY; + response +} + fn malformed_request() -> Response> { let payload = ErrorResponse { code: errors::MALFORMED_REQUEST,