From 8a2af8d6f2875e575524b265eadb77074a1a43d9 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Sat, 12 Nov 2022 16:34:22 +0100 Subject: [PATCH] Refactor `/score/` to use query parameters --- src/routes/openapi.yaml | 54 ++++++++++++++++++++++++++++++----------- src/routes/score.rs | 23 ++++++------------ 2 files changed, 47 insertions(+), 30 deletions(-) diff --git a/src/routes/openapi.yaml b/src/routes/openapi.yaml index be1282f..65ee7f0 100644 --- a/src/routes/openapi.yaml +++ b/src/routes/openapi.yaml @@ -101,28 +101,40 @@ paths: $ref: "#/components/responses/RedisConnFailed" /score/: + get: + summary: "Get a score from a board" + tags: ["Score"] + parameters: + - $ref: "#/components/parameters/board" + - $ref: "#/components/parameters/player" + responses: + 200: + description: "Score retrieved successfully" + content: + application/json: + schema: + type: number + description: "The score of the specified player." + example: 1234.56 + 502: + $ref: "#/components/responses/RedisCmdFailed" + 504: + $ref: "#/components/responses/RedisConnFailed" + put: summary: "Submit a score to a board" tags: ["Score"] + parameters: + - $ref: "#/components/parameters/board" + - $ref: "#/components/parameters/player" requestBody: required: true content: application/json: schema: - type: object - properties: - board: - type: string - description: "The board to submit the score to." - example: "gravityfusion" - score: - type: number - description: "The score to submit to the board." - example: 1234.56 - player: - type: string - description: "The name of the player that the score should be submitted as." - example: "Steffo" + type: number + description: "The score to submit to the board." + example: 1234.56 security: - XBoardToken: [] responses: @@ -146,6 +158,20 @@ components: in: header name: "Authorization" + parameters: + board: + name: "board" + description: "The name of the board to operate on." + in: query + schema: + type: string + player: + name: "player" + description: "The name of the player to operate on." + in: query + schema: + type: string + responses: RedisCmdFailed: description: "Could not execute Redis command" diff --git a/src/routes/score.rs b/src/routes/score.rs index dd3c074..4586de7 100644 --- a/src/routes/score.rs +++ b/src/routes/score.rs @@ -13,22 +13,11 @@ use crate::utils::kebab::Skewer; use crate::utils::sorting::SortingOrder; -/// Expected input data for `GET /score/`. -pub(crate) struct RouteScoreGetInput { +/// Query parameters for `/score/` routes. +pub(crate) struct RouteScoreQuery { /// 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 { - /// The board to submit the score to. - pub board: String, - /// The score to submit. - pub score: f64, - /// The name of the player submitting the score. + /// The name of the player to access the score of. pub player: String, } @@ -36,7 +25,7 @@ pub(crate) struct RouteScorePutInput { /// Handler for `GET /score/`. pub(crate) async fn route_score_get( // Request query - Query(RouteScoreGetInput {board, player}): Query, + Query(RouteScoreQuery {board, player}): Query, // Redis client Extension(rclient): Extension, ) -> outcome::RequestResult { @@ -64,8 +53,10 @@ pub(crate) async fn route_score_get( pub(crate) async fn route_score_put( // Request headers headers: HeaderMap, + // Request query + Query(RouteScoreQuery {board, player}): Query, // Request body - Json(RouteScorePutInput {board, score, player}): Json, + Json(score): Json, // Redis client Extension(rclient): Extension, ) -> outcome::RequestResult {