M api/src/actions/game/all_forming.rs => api/src/actions/game/all_forming.rs +15 -4
@@ 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<Vec<Game>, APIError> {
+ let games_res = games::table
+ .filter(games::state.eq(GAMESTATE_FORMING))
+ .load::<Game>(conn);
+
+ let games = match games_res {
+ Err(_) => return Err(APIError::Undefined), // TODO: new error type
+ Ok(games) => games,
+ };
+
+ Ok(games)
}
M api/src/actions/game/my_games.rs => api/src/actions/game/my_games.rs +19 -3
@@ 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<Vec<Game>, 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::<Game>(conn);
+
+ let games = match games_res {
+ Err(_) => return Err(APIError::Undefined), // TODO: new error type
+ Ok(games) => games,
+ };
+
+ Ok(games)
}
M api/src/handlers/game/mod.rs => api/src/handlers/game/mod.rs +34 -4
@@ 73,8 73,23 @@ async fn all_forming(session: Session, pool: web::Data<PgPool>) -> 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<PgPool>) -> 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),
+ },
+ }
}
}
}