From ccb39742ed6512bf2dd6d2378f84aa4e4df25589 Mon Sep 17 00:00:00 2001 From: Jonni Liljamo Date: Wed, 15 Mar 2023 12:48:14 +0200 Subject: [PATCH] feat(api): reimpl all_games and my_games --- api/src/actions/game/all_forming.rs | 19 ++++++++++++--- api/src/actions/game/my_games.rs | 22 ++++++++++++++--- api/src/handlers/game/mod.rs | 38 ++++++++++++++++++++++++++--- 3 files changed, 68 insertions(+), 11 deletions(-) diff --git a/api/src/actions/game/all_forming.rs b/api/src/actions/game/all_forming.rs index c2a046b..c528376 100644 --- a/api/src/actions/game/all_forming.rs +++ b/api/src/actions/game/all_forming.rs @@ -6,9 +6,20 @@ * See LICENSE for licensing information. */ -use diesel::PgConnection; -use laurelin_shared::error::api::APIError; +use diesel::{ExpressionMethods, PgConnection, QueryDsl, RunQueryDsl}; +use laurelin_shared::{error::api::APIError, types::game::GAMESTATE_FORMING}; -pub(crate) fn all_forming(conn: &mut PgConnection) -> Result<(), APIError> { - Err(APIError::Undefined) +use crate::{models::Game, schema::games}; + +pub(crate) fn all_forming(conn: &mut PgConnection) -> Result, APIError> { + let games_res = games::table + .filter(games::state.eq(GAMESTATE_FORMING)) + .load::(conn); + + let games = match games_res { + Err(_) => return Err(APIError::Undefined), // TODO: new error type + Ok(games) => games, + }; + + Ok(games) } diff --git a/api/src/actions/game/my_games.rs b/api/src/actions/game/my_games.rs index 6183558..f90a522 100644 --- a/api/src/actions/game/my_games.rs +++ b/api/src/actions/game/my_games.rs @@ -6,9 +6,25 @@ * See LICENSE for licensing information. */ -use diesel::PgConnection; +use diesel::{BoolExpressionMethods, ExpressionMethods, PgConnection, QueryDsl, RunQueryDsl}; use laurelin_shared::error::api::APIError; +use uuid::Uuid; -pub(crate) fn my_games(conn: &mut PgConnection) -> Result<(), APIError> { - Err(APIError::Undefined) +use crate::{models::Game, schema::games}; + +pub(crate) fn my_games(conn: &mut PgConnection, user_id: &str) -> Result, APIError> { + let games_res = games::table + .filter( + games::host_id + .eq(Uuid::parse_str(user_id).unwrap()) + .or(games::guest_id.eq(Uuid::parse_str(user_id).unwrap())), + ) + .load::(conn); + + let games = match games_res { + Err(_) => return Err(APIError::Undefined), // TODO: new error type + Ok(games) => games, + }; + + Ok(games) } diff --git a/api/src/handlers/game/mod.rs b/api/src/handlers/game/mod.rs index 6033691..b90f07d 100644 --- a/api/src/handlers/game/mod.rs +++ b/api/src/handlers/game/mod.rs @@ -73,8 +73,23 @@ async fn all_forming(session: Session, pool: web::Data) -> impl Responde match session_validation { Err(err) => err, Ok(user_id) => { - // - return HttpResponse::Ok().body("ALL_FORMING"); + let all_forming = web::block(move || { + let mut conn = match pool.get() { + Err(_) => return Err(APIError::DatabasePoolGetFailed), + Ok(conn) => conn, + }; + actions::game::all_forming(&mut conn) + }) + .await; + match all_forming { + Err(_err) => { + return HttpResponse::InternalServerError().json(APIError::Undefined); + } + Ok(games_res) => match games_res { + Err(err) => HttpResponse::InternalServerError().body(err.to_string()), + Ok(games) => HttpResponse::Ok().json(games), + }, + } } } } @@ -86,8 +101,23 @@ async fn my_games(session: Session, pool: web::Data) -> impl Responder { match session_validation { Err(err) => err, Ok(user_id) => { - // - return HttpResponse::Ok().body("MY_GAMES"); + let my_games = web::block(move || { + let mut conn = match pool.get() { + Err(_) => return Err(APIError::DatabasePoolGetFailed), + Ok(conn) => conn, + }; + actions::game::my_games(&mut conn, &user_id) + }) + .await; + match my_games { + Err(_err) => { + return HttpResponse::InternalServerError().json(APIError::Undefined); + } + Ok(games_res) => match games_res { + Err(err) => HttpResponse::InternalServerError().body(err.to_string()), + Ok(games) => HttpResponse::Ok().json(games), + }, + } } } } -- 2.44.1