Adds own/disown game logic

This commit is contained in:
2025-07-12 20:00:22 +02:00
parent 3f99b68d62
commit df1e3ff905
13 changed files with 427 additions and 4 deletions

View File

@@ -4,6 +4,7 @@ pub mod schema;
pub mod gamenight;
pub mod gamenight_participants;
pub mod game;
pub mod owned_game;
use diesel::r2d2::ConnectionManager;
use diesel::r2d2::ManageConnection;

View File

@@ -0,0 +1,28 @@
use diesel::{dsl::{delete, insert_into}, prelude::{Insertable, Queryable}, BoolExpressionMethods, ExpressionMethods, PgConnection, QueryDsl, RunQueryDsl};
use serde::{Serialize, Deserialize};
use uuid::Uuid;
use crate::{schema::owned_game, user::DatabaseError};
#[derive(Serialize, Deserialize, Debug, Insertable, Queryable)]
#[diesel(table_name = owned_game)]
pub struct OwnedGame {
pub user_id: Uuid,
pub game_id: Uuid
}
pub fn own_game(conn: &mut PgConnection, owned_game: OwnedGame) -> Result<usize, DatabaseError>{
Ok(insert_into(owned_game::table).values(&owned_game).execute(conn)?)
}
pub fn disown_game(conn: &mut PgConnection, owned_game: OwnedGame) -> Result<usize, DatabaseError> {
Ok(delete(owned_game::table)
.filter(owned_game::user_id.eq(&owned_game.user_id)
.and(owned_game::game_id.eq(&owned_game.game_id))
).execute(conn)?)
}
pub fn owned_games(conn: &mut PgConnection, uuid: Uuid) -> Result<Vec<Uuid>, DatabaseError> {
Ok(owned_game::table.select(owned_game::game_id)
.filter(owned_game::user_id.eq(uuid))
.get_results(conn)?)
}