use crate::{schema::owned_game, user::DatabaseError}; use diesel::{ BoolExpressionMethods, ExpressionMethods, PgConnection, QueryDsl, RunQueryDsl, dsl::{delete, insert_into}, prelude::{Insertable, Queryable}, }; use serde::{Deserialize, Serialize}; use uuid::Uuid; #[derive(Serialize, Deserialize, Debug, Insertable, Queryable)] #[diesel(belongs_to(user, foreign_key=FK_user_id))] #[diesel(belongs_to(game, foreign_key=FK_gamenight_id))] #[diesel(table_name = owned_game)] pub struct OwnedGame { pub user_id: Uuid, pub game_id: Uuid, pub location_id: Option } pub fn own_game(conn: &mut PgConnection, owned_game: OwnedGame) -> Result { Ok(insert_into(owned_game::table) .values(&owned_game) .execute(conn)?) } pub fn disown_game(conn: &mut PgConnection, owned_game: OwnedGame) -> Result { 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, DatabaseError> { Ok(owned_game::table .select(owned_game::game_id) .filter(owned_game::user_id.eq(uuid)) .get_results(conn)?) }