1
Fork 0
mirror of https://github.com/Steffo99/distributed-arcade.git synced 2024-11-21 15:44:26 +00:00

Create GET /score/

This commit is contained in:
Steffo 2022-11-12 16:23:18 +01:00
parent 1863bb32a2
commit 18dcc542ed
Signed by: steffo
GPG key ID: 6965406171929D01
2 changed files with 38 additions and 1 deletions

View file

@ -24,6 +24,7 @@ async fn main() {
.route("/", get(routes::home::route_home_get))
.route("/", patch(routes::home::route_home_patch))
.route("/board/", post(routes::board::route_board_post))
.route("/score/", get(routes::score::route_score_get))
.route("/score/", put(routes::score::route_score_put))
.layer(axum::Extension(rclient));

View file

@ -2,7 +2,7 @@
use axum::http::StatusCode;
use axum::http::header::HeaderMap;
use axum::extract::{Extension, Json};
use axum::extract::{Extension, Json, Query};
use redis::AsyncCommands;
use serde::Serialize;
use serde::Deserialize;
@ -13,6 +13,14 @@ use crate::utils::kebab::Skewer;
use crate::utils::sorting::SortingOrder;
/// Expected input data for `GET /score/`.
pub(crate) struct RouteScoreGetInput {
/// The board to access.
pub board: String,
/// The name of the player to see the score of.
pub player: String,
}
/// Expected input data for `PUT /score/`.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub(crate) struct RouteScorePutInput {
@ -25,6 +33,33 @@ pub(crate) struct RouteScorePutInput {
}
/// Handler for `GET /score/`.
pub(crate) async fn route_score_get(
// Request query
Query(RouteScoreGetInput {board, player}): Query<RouteScoreGetInput>,
// Redis client
Extension(rclient): Extension<redis::Client>,
) -> outcome::RequestResult {
let board = board.to_kebab_lowercase();
let player = player.to_kebab_lowercase();
log::trace!("Determining the Redis key name...");
let scores_key = format!("board:{board}:scores");
let mut rconn = rclient.get_connection_or_504().await?;
log::trace!("Getting score...");
let score = rconn.zscore(&scores_key, &player).await
.map_err(|_| outcome::redis_cmd_failed())?;
log::trace!("Score is: {score:?}");
Ok((
StatusCode::OK,
outcome::req_success!(score)
))
}
/// Handler for `PUT /score/`.
pub(crate) async fn route_score_put(
// Request headers
@ -35,6 +70,7 @@ pub(crate) async fn route_score_put(
Extension(rclient): Extension<redis::Client>,
) -> outcome::RequestResult {
let board = board.to_kebab_lowercase();
let player = player.to_kebab_lowercase();
log::trace!("Determining the Redis key names...");
let order_key = format!("board:{board}:order");