forked from Roflin/gamenight
48 lines
1.4 KiB
Rust
48 lines
1.4 KiB
Rust
use diesel::{
|
|
BoolExpressionMethods, ExpressionMethods, Insertable, QueryDsl, Queryable, RunQueryDsl,
|
|
};
|
|
use serde::{Deserialize, Serialize};
|
|
use uuid::Uuid;
|
|
|
|
use crate::schema::gamenight_participant;
|
|
|
|
use super::DbConnection;
|
|
use super::error::DatabaseError;
|
|
|
|
#[derive(Serialize, Deserialize, Debug, Insertable, Queryable)]
|
|
#[diesel(belongs_to(Gamenight, foreign_key=FK_gamenight_id))]
|
|
#[diesel(belongs_to(User, foreign_key=FK_user_id))]
|
|
#[diesel(table_name = gamenight_participant)]
|
|
pub struct GamenightParticipant {
|
|
pub gamenight_id: Uuid,
|
|
pub user_id: Uuid,
|
|
}
|
|
|
|
pub fn get_participants(conn: &mut DbConnection, id: &Uuid) -> Result<Vec<Uuid>, DatabaseError> {
|
|
Ok(gamenight_participant::table
|
|
.filter(gamenight_participant::gamenight_id.eq(&id))
|
|
.select(gamenight_participant::user_id)
|
|
.get_results(conn)?)
|
|
}
|
|
|
|
pub fn insert_gamenight_participant(
|
|
conn: &mut DbConnection,
|
|
gp: GamenightParticipant,
|
|
) -> Result<usize, DatabaseError> {
|
|
Ok(gp.insert_into(gamenight_participant::table).execute(conn)?)
|
|
}
|
|
|
|
pub fn delete_gamenight_participant(
|
|
conn: &mut DbConnection,
|
|
gp: GamenightParticipant,
|
|
) -> Result<usize, DatabaseError> {
|
|
Ok(diesel::delete(
|
|
gamenight_participant::table.filter(
|
|
gamenight_participant::gamenight_id
|
|
.eq(&gp.gamenight_id)
|
|
.and(gamenight_participant::user_id.eq(gp.user_id)),
|
|
),
|
|
)
|
|
.execute(conn)?)
|
|
}
|