forked from lavina/lavina
1
0
Fork 0
This commit is contained in:
Nikita Vilunov 2024-04-29 02:21:21 +02:00
parent 7bc2216ebc
commit 03eb861131
1 changed files with 18 additions and 28 deletions

View File

@ -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::<Bytes>::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::<Bytes>::default());
@ -182,7 +161,7 @@ async fn endpoint_set_password(
Ok(response)
}
pub fn not_found() -> Response<Full<Bytes>> {
fn endpoint_not_found() -> Response<Full<Bytes>> {
let payload = ErrorResponse {
code: errors::INVALID_PATH,
message: "The path does not exist",
@ -194,6 +173,17 @@ pub fn not_found() -> Response<Full<Bytes>> {
response
}
fn player_not_found() -> Response<Full<Bytes>> {
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<Full<Bytes>> {
let payload = ErrorResponse {
code: errors::MALFORMED_REQUEST,