forked from Roflin/gamenight
Adds Adding of Games to the system.
This commit is contained in:
@@ -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()
|
||||
|
||||
39
backend-actix/src/request/game.rs
Normal file
39
backend-actix/src/request/game.rs
Normal 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())
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user