From b487c450a27c0e35fc05ecc7e2736b77527b8c5e Mon Sep 17 00:00:00 2001 From: Jonni Liljamo Date: Tue, 28 Mar 2023 13:22:42 +0300 Subject: [PATCH] feat(api, shared): patch game endpoint and request, cleanup unused apis --- api/src/actions/game/info.rs | 14 -------- api/src/actions/game/join.rs | 14 -------- api/src/actions/game/mod.rs | 7 ++-- api/src/actions/game/patch.rs | 26 ++++++++++++++ api/src/handlers/game/mod.rs | 58 ++++++++++++------------------- api/src/main.rs | 6 +--- shared/src/api/game/mod.rs | 4 +-- shared/src/api/game/patch.rs | 31 +++++++++++++++++ shared/src/api/game/patchstate.rs | 45 ------------------------ shared/src/types/game.rs | 11 +++++- 10 files changed, 94 insertions(+), 122 deletions(-) delete mode 100644 api/src/actions/game/info.rs delete mode 100644 api/src/actions/game/join.rs create mode 100644 api/src/actions/game/patch.rs create mode 100644 shared/src/api/game/patch.rs delete mode 100644 shared/src/api/game/patchstate.rs diff --git a/api/src/actions/game/info.rs b/api/src/actions/game/info.rs deleted file mode 100644 index 7d2fd33..0000000 --- a/api/src/actions/game/info.rs +++ /dev/null @@ -1,14 +0,0 @@ -/* - * This file is part of laurelin/api - * Copyright (C) 2023 Jonni Liljamo - * - * Licensed under GPL-3.0-only. - * See LICENSE for licensing information. - */ - -use diesel::PgConnection; -use laurelin_shared::error::api::APIError; - -pub(crate) fn info(_conn: &mut PgConnection) -> Result<(), APIError> { - Err(APIError::Undefined) -} diff --git a/api/src/actions/game/join.rs b/api/src/actions/game/join.rs deleted file mode 100644 index 92511fd..0000000 --- a/api/src/actions/game/join.rs +++ /dev/null @@ -1,14 +0,0 @@ -/* - * This file is part of laurelin/api - * Copyright (C) 2023 Jonni Liljamo - * - * Licensed under GPL-3.0-only. - * See LICENSE for licensing information. - */ - -use diesel::PgConnection; -use laurelin_shared::error::api::APIError; - -pub(crate) fn join(_conn: &mut PgConnection) -> Result<(), APIError> { - Err(APIError::Undefined) -} diff --git a/api/src/actions/game/mod.rs b/api/src/actions/game/mod.rs index db2c3ac..c8b8175 100644 --- a/api/src/actions/game/mod.rs +++ b/api/src/actions/game/mod.rs @@ -15,8 +15,5 @@ pub(crate) use all_forming::all_forming; mod my_games; pub(crate) use my_games::my_games; -mod info; -pub(crate) use info::info; - -mod join; -pub(crate) use join::join; +mod patch; +pub(crate) use patch::patch; diff --git a/api/src/actions/game/patch.rs b/api/src/actions/game/patch.rs new file mode 100644 index 0000000..e1b79f1 --- /dev/null +++ b/api/src/actions/game/patch.rs @@ -0,0 +1,26 @@ +/* + * This file is part of laurelin/api + * Copyright (C) 2023 Jonni Liljamo + * + * Licensed under GPL-3.0-only. + * See LICENSE for licensing information. + */ + +use diesel::{ExpressionMethods, PgConnection, RunQueryDsl}; +use laurelin_schema::schema::games; +use laurelin_shared::{error::api::APIError, types::game::PatchGame}; +use uuid::Uuid; + +pub(crate) fn patch( + conn: &mut PgConnection, + game_id: &str, + patch: &PatchGame, +) -> Result<(), APIError> { + // TODO: handle stuffs, yeah? + let _update_res = diesel::update(games::table) + .filter(games::id.eq(Uuid::try_parse(game_id).unwrap())) + .set(patch) + .execute(conn); + + Ok(()) +} diff --git a/api/src/handlers/game/mod.rs b/api/src/handlers/game/mod.rs index 4628d3c..077fe3d 100644 --- a/api/src/handlers/game/mod.rs +++ b/api/src/handlers/game/mod.rs @@ -7,8 +7,8 @@ */ use actix_session::Session; -use actix_web::{get, post, web, HttpResponse, Responder}; -use laurelin_shared::error::api::APIError; +use actix_web::{get, patch, post, web, HttpResponse, Responder}; +use laurelin_shared::{error::api::APIError, types::game::PatchGame}; use crate::{actions, session, PgPool}; @@ -38,40 +38,6 @@ async fn create(session: Session, pool: web::Data) -> impl Responder { } } -#[get("/api/game/{id}")] -async fn info( - session: Session, - _pool: web::Data, - _id: web::Path, -) -> impl Responder { - let session_validation = session::validate_session(&session); - - match session_validation { - Err(err) => err, - Ok(_user_id) => { - // - HttpResponse::Ok().body("INFO") - } - } -} - -#[post("/api/game/{id}/join")] -async fn join( - session: Session, - _pool: web::Data, - _id: web::Path, -) -> impl Responder { - let session_validation = session::validate_session(&session); - - match session_validation { - Err(err) => err, - Ok(_user_id) => { - // - HttpResponse::Ok().body("JOIN") - } - } -} - #[get("/api/game/all_forming")] async fn all_forming(session: Session, pool: web::Data) -> impl Responder { let session_validation = session::validate_session(&session); @@ -123,3 +89,23 @@ async fn my_games(session: Session, pool: web::Data) -> impl Responder { } } } + +#[patch("/api/game/{id}")] +async fn patch( + pool: web::Data, + id: web::Path, + patch: web::Json, +) -> impl Responder { + let patch_res = web::block(move || { + let mut conn = match pool.get() { + Err(_) => return Err(APIError::DatabasePoolGetFailed), + Ok(conn) => conn, + }; + actions::game::patch(&mut conn, &id, &patch) + }) + .await; + match patch_res { + Err(_err) => HttpResponse::InternalServerError().json(APIError::Undefined), + Ok(_) => HttpResponse::Ok().into(), + } +} diff --git a/api/src/main.rs b/api/src/main.rs index f609178..521f124 100644 --- a/api/src/main.rs +++ b/api/src/main.rs @@ -103,11 +103,7 @@ async fn main() -> std::io::Result<()> { .service(handlers::game::create) .service(handlers::game::all_forming) .service(handlers::game::my_games) - // NOTE: these have to be registered last! - // otherwise the positional {id} will catch - // things like "all_forming" - .service(handlers::game::info) - .service(handlers::game::join) + .service(handlers::game::patch) }) .bind(("0.0.0.0", 8080))? .run() diff --git a/shared/src/api/game/mod.rs b/shared/src/api/game/mod.rs index d090697..a80fb6f 100644 --- a/shared/src/api/game/mod.rs +++ b/shared/src/api/game/mod.rs @@ -21,5 +21,5 @@ pub use all_forming::*; mod mygames; pub use mygames::*; -//mod patchstate; -//pub use patchstate::*; +mod patch; +pub use patch::*; diff --git a/shared/src/api/game/patch.rs b/shared/src/api/game/patch.rs new file mode 100644 index 0000000..8bdbea9 --- /dev/null +++ b/shared/src/api/game/patch.rs @@ -0,0 +1,31 @@ +/* + * This file is part of laurelin/shared + * Copyright (C) 2023 Jonni Liljamo + * + * Licensed under GPL-3.0-only. + * See LICENSE for licensing information. + */ + +use reqwest; +use serde::{Deserialize, Serialize}; + +use crate::{error::api::APIError, types::game::PatchGame}; + +#[derive(Debug, Serialize, Deserialize)] +#[serde(untagged)] +pub enum ResponsePatchGame { + Error(APIError), + Valid(()), +} + +pub fn patch(api_address: String, game_id: String, patch: PatchGame) -> ResponsePatchGame { + let client = reqwest::blocking::Client::new(); + + let resp = client + .patch(format!("{}/game/{}", api_address, game_id)) + .json(&patch) + .send() + .unwrap(); + + resp.json().unwrap() +} diff --git a/shared/src/api/game/patchstate.rs b/shared/src/api/game/patchstate.rs deleted file mode 100644 index 1e17167..0000000 --- a/shared/src/api/game/patchstate.rs +++ /dev/null @@ -1,45 +0,0 @@ -/* - * This file is part of laurelin/shared - * Copyright (C) 2023 Jonni Liljamo - * - * Licensed under GPL-3.0-only. - * See LICENSE for licensing information. - */ - -use reqwest; -use serde::{Deserialize, Serialize}; - -use super::{ - types::{Game, GameState}, - APIErrorWrapper, -}; - -#[derive(Serialize)] -pub struct PostState { - pub state: GameState, -} - -#[derive(Debug, Serialize, Deserialize)] -#[serde(untagged)] -pub enum ResponsePatchGameState { - Error(APIErrorWrapper), - Valid(Game), -} - -pub fn patch_state( - api_address: String, - token: String, - game_id: String, - state: GameState, -) -> ResponsePatchGameState { - let client = reqwest::blocking::Client::new(); - - let resp = client - .patch(&format!("{}/game/{}/state", api_address, game_id)) - .header("Authorization", token) - .json(&PostState { state }) - .send() - .unwrap(); - - resp.json().unwrap() -} diff --git a/shared/src/types/game.rs b/shared/src/types/game.rs index 8b04a66..c7f7b63 100644 --- a/shared/src/types/game.rs +++ b/shared/src/types/game.rs @@ -7,7 +7,7 @@ */ use chrono::NaiveDateTime; -use diesel::{Insertable, Queryable}; +use diesel::{AsChangeset, Insertable, Queryable}; use naia_bevy_shared::Serde; use serde::{Deserialize, Serialize}; use uuid::Uuid; @@ -71,6 +71,15 @@ pub struct InsertableGame { pub game_data_id: Uuid, } +#[derive(Serialize, Deserialize, AsChangeset)] +#[diesel(table_name=games)] +pub struct PatchGame { + pub guest_id: Option, + pub state: Option, + pub ended_at: Option, + pub game_data_id: Option, +} + #[derive(Serialize, Deserialize, Queryable)] pub struct Action { pub id: Uuid, -- 2.44.1