forked from Roflin/gamenight
36 lines
1.3 KiB
Rust
36 lines
1.3 KiB
Rust
use diesel::{BoolExpressionMethods, ExpressionMethods, Insertable, QueryDsl, Queryable, RunQueryDsl};
|
|
use serde::{Serialize, Deserialize};
|
|
use uuid::Uuid;
|
|
|
|
use crate::schema::gamenight_participant;
|
|
|
|
use super::error::DatabaseError;
|
|
use super::DbConnection;
|
|
|
|
#[derive(Serialize, Deserialize, Debug, Insertable, Queryable)]
|
|
#[diesel(belongs_to(Gamenight))]
|
|
#[diesel(belongs_to(User))]
|
|
#[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)?)
|
|
|
|
} |