mirror of
https://github.com/Steffo99/distributed-arcade.git
synced 2024-11-21 23:54:25 +00:00
Create GET /score/
This commit is contained in:
parent
1863bb32a2
commit
18dcc542ed
2 changed files with 38 additions and 1 deletions
|
@ -24,6 +24,7 @@ async fn main() {
|
||||||
.route("/", get(routes::home::route_home_get))
|
.route("/", get(routes::home::route_home_get))
|
||||||
.route("/", patch(routes::home::route_home_patch))
|
.route("/", patch(routes::home::route_home_patch))
|
||||||
.route("/board/", post(routes::board::route_board_post))
|
.route("/board/", post(routes::board::route_board_post))
|
||||||
|
.route("/score/", get(routes::score::route_score_get))
|
||||||
.route("/score/", put(routes::score::route_score_put))
|
.route("/score/", put(routes::score::route_score_put))
|
||||||
.layer(axum::Extension(rclient));
|
.layer(axum::Extension(rclient));
|
||||||
|
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
use axum::http::StatusCode;
|
use axum::http::StatusCode;
|
||||||
use axum::http::header::HeaderMap;
|
use axum::http::header::HeaderMap;
|
||||||
use axum::extract::{Extension, Json};
|
use axum::extract::{Extension, Json, Query};
|
||||||
use redis::AsyncCommands;
|
use redis::AsyncCommands;
|
||||||
use serde::Serialize;
|
use serde::Serialize;
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
|
@ -13,6 +13,14 @@ use crate::utils::kebab::Skewer;
|
||||||
use crate::utils::sorting::SortingOrder;
|
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/`.
|
/// Expected input data for `PUT /score/`.
|
||||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||||
pub(crate) struct RouteScorePutInput {
|
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/`.
|
/// Handler for `PUT /score/`.
|
||||||
pub(crate) async fn route_score_put(
|
pub(crate) async fn route_score_put(
|
||||||
// Request headers
|
// Request headers
|
||||||
|
@ -35,6 +70,7 @@ pub(crate) async fn route_score_put(
|
||||||
Extension(rclient): Extension<redis::Client>,
|
Extension(rclient): Extension<redis::Client>,
|
||||||
) -> outcome::RequestResult {
|
) -> outcome::RequestResult {
|
||||||
let board = board.to_kebab_lowercase();
|
let board = board.to_kebab_lowercase();
|
||||||
|
let player = player.to_kebab_lowercase();
|
||||||
|
|
||||||
log::trace!("Determining the Redis key names...");
|
log::trace!("Determining the Redis key names...");
|
||||||
let order_key = format!("board:{board}:order");
|
let order_key = format!("board:{board}:order");
|
||||||
|
|
Loading…
Reference in a new issue