/*
* This file is part of laurelin/api
* Copyright (C) 2023 Jonni Liljamo <jonni@liljamo.com>
*
* Licensed under GPL-3.0-only.
* See LICENSE for licensing information.
*/
use actix_session::Session;
use actix_web::{get, patch, post, web, HttpResponse, Responder};
use laurelin_shared::{error::api::APIError, types::game::PatchGame};
use crate::{actions, session, PgPool};
#[post("/api/game")]
async fn create(session: Session, pool: web::Data<PgPool>) -> impl Responder {
let session_validation = session::validate_session(&session);
match session_validation {
Err(err) => err,
Ok(user_id) => {
let game_create = web::block(move || {
let mut conn = match pool.get() {
Err(_) => return Err(APIError::DatabasePoolGetFailed),
Ok(conn) => conn,
};
actions::game::create(&mut conn, &user_id)
})
.await;
match game_create {
Err(_err) => HttpResponse::InternalServerError().json(APIError::Undefined),
Ok(game_res) => match game_res {
Err(err) => HttpResponse::InternalServerError().body(err.to_string()),
Ok(game) => HttpResponse::Ok().json(game),
},
}
}
}
}
#[get("/api/game/all_forming")]
async fn all_forming(session: Session, pool: web::Data<PgPool>) -> impl Responder {
let session_validation = session::validate_session(&session);
match session_validation {
Err(err) => err,
Ok(_user_id) => {
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) => 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),
},
}
}
}
}
#[get("/api/game/my_games")]
async fn my_games(session: Session, pool: web::Data<PgPool>) -> impl Responder {
let session_validation = session::validate_session(&session);
match session_validation {
Err(err) => err,
Ok(user_id) => {
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) => 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),
},
}
}
}
}
#[get("/api/game/{id}")]
async fn info(session: Session, pool: web::Data<PgPool>, id: web::Path<String>) -> impl Responder {
let session_validation = session::validate_session(&session);
match session_validation {
Err(err) => err,
Ok(_user_id) => {
let game = web::block(move || {
let mut conn = match pool.get() {
Err(_) => return Err(APIError::DatabasePoolGetFailed),
Ok(conn) => conn,
};
actions::game::info(&mut conn, &id)
})
.await;
match game {
Err(_err) => HttpResponse::InternalServerError().json(APIError::Undefined),
Ok(game_res) => match game_res {
Err(err) => HttpResponse::InternalServerError().body(err.to_string()),
Ok(game) => HttpResponse::Ok().json(game),
},
}
}
}
}
#[patch("/api/game/{id}")]
async fn patch(
pool: web::Data<PgPool>,
id: web::Path<String>,
patch: web::Json<PatchGame>,
) -> 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(),
}
}