Adds Adding of Games to the system.

This commit is contained in:
2025-06-27 22:08:23 +02:00
parent 3f51d52edf
commit 28f7306d57
40 changed files with 792 additions and 130 deletions

View File

@@ -2,6 +2,7 @@ use std::{fs::{exists, read_dir, remove_dir_all, File}, io::Write, process::Comm
fn main() {
println!("cargo::rerun-if-changed=gamenight-api.yaml");
if exists("src/models").unwrap() {
remove_dir_all("src/models").unwrap();

View File

@@ -38,7 +38,6 @@ paths:
parameters: []
security:
- JWT-Auth: []
/user:
post:
summary: ''
@@ -146,7 +145,7 @@ paths:
post:
responses:
'200':
description: OK
description: "OK"
'401':
$ref: '#/components/responses/FailureResponse'
'422':
@@ -155,6 +154,42 @@ paths:
$ref: '#/components/requestBodies/LeaveGamenight'
security:
- JWT-Auth: []
/games:
get:
responses:
'200':
$ref: '#/components/responses/GamesResponse'
'401':
$ref: '#/components/responses/FailureResponse'
'422':
$ref: '#/components/responses/FailureResponse'
security:
- JWT-Auth: []
/game:
get:
responses:
'200':
$ref: '#/components/responses/GameResponse'
'401':
$ref: '#/components/responses/FailureResponse'
'422':
$ref: '#/components/responses/FailureResponse'
requestBody:
$ref: '#/components/requestBodies/GetGameRequest'
security:
- JWT-Auth: []
post:
responses:
'200':
description: "OK"
'401':
$ref: '#/components/responses/FailureResponse'
'422':
$ref: '#/components/responses/FailureResponse'
requestBody:
$ref: '#/components/requestBodies/AddGameRequest'
security:
- JWT-Auth: []
components:
schemas:
@@ -256,6 +291,14 @@ components:
type: string
required:
- gamenight_id
GameId:
title: GameId
type: object
properties:
game_id:
type: string
required:
- game_id
GetGamenightRequestBody:
type: object
properties:
@@ -273,6 +316,24 @@ components:
required:
- id
- username
Game:
type: object
properties:
id:
type: string
name:
type: string
required:
- id
- name
AddGameRequestBody:
type: object
properties:
name:
type: string
required:
- name
requestBodies:
LoginRequest:
content:
@@ -314,6 +375,16 @@ components:
application/json:
schema:
$ref: '#/components/schemas/GamenightId'
GetGameRequest:
content:
application/json:
schema:
$ref: '#/components/schemas/GameId'
AddGameRequest:
content:
application/json:
schema:
$ref: '#/components/schemas/AddGameRequestBody'
responses:
TokenResponse:
description: Example response
@@ -353,6 +424,20 @@ components:
application/json:
schema:
$ref: '#/components/schemas/User'
GamesResponse:
description: A list of Games in this gamenight instance.
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/Game'
GameResponse:
description: A game.
content:
application/json:
schema:
$ref: '#/components/schemas/Game'
securitySchemes:
JWT-Auth:
type: http

View File

@@ -48,6 +48,8 @@ async fn main() -> std::io::Result<()> {
.service(post_join_gamenight)
.service(post_leave_gamenight)
.service(get_get_participants)
.service(get_games)
.service(post_game)
})
.bind(("::1", 8080))?
.run()

View File

@@ -0,0 +1,39 @@
use actix_web::{get, http::header::ContentType, post, web, HttpResponse, Responder};
use gamenight_database::{game::insert_game, DbPool, GetConnection};
use uuid::Uuid;
use crate::{models::{add_game_request_body::AddGameRequestBody, game::Game}, request::{authorization::AuthUser, error::ApiError}};
#[get("/games")]
pub async fn get_games(pool: web::Data<DbPool>, _user: AuthUser) -> Result<impl Responder, ApiError> {
let mut conn = pool.get_conn();
let games: Vec<gamenight_database::game::Game> = gamenight_database::games(&mut conn)?;
let model: Vec<Game> = games.iter().map(|x| {
Game {
id: x.id.to_string(),
name: x.name.clone(),
}}
).collect();
Ok(HttpResponse::Ok()
.content_type(ContentType::json())
.body(serde_json::to_string(&model)?)
)
}
impl From<AddGameRequestBody> for gamenight_database::game::Game {
fn from(value: AddGameRequestBody) -> Self {
Self {
id: Uuid::new_v4(),
name: value.name
}
}
}
#[post("/game")]
pub async fn post_game(pool: web::Data<DbPool>, _user: AuthUser, game_data: web::Json<AddGameRequestBody>) -> Result<impl Responder, ApiError> {
let mut conn = pool.get_conn();
insert_game(&mut conn, game_data.0.into())?;
Ok(HttpResponse::Ok())
}

View File

@@ -5,6 +5,7 @@ mod error;
mod authorization;
mod join_gamenight;
mod participant_handlers;
mod game;
pub use user_handlers::login;
pub use user_handlers::refresh;
@@ -17,3 +18,5 @@ pub use user_handlers::get_user_unauthenticated;
pub use join_gamenight::post_join_gamenight;
pub use join_gamenight::post_leave_gamenight;
pub use participant_handlers::get_get_participants;
pub use game::get_games;
pub use game::post_game;