Added Location and location ownership/rights to gamenight.

This commit is contained in:
2025-12-24 14:48:54 +01:00
parent 8a48119c80
commit ff88029a4b
57 changed files with 3034 additions and 995 deletions

View File

@@ -0,0 +1,8 @@
ALTER TABLE gamenight
DROP CONSTRAINT FK_location_id,
DROP column location_id;
DROP TABLE location_owner;
DROP TABLE location;

View File

@@ -0,0 +1,18 @@
CREATE TABLE location (
id UUID NOT NULL PRIMARY KEY,
name VARCHAR UNIQUE NOT NULL,
address VARCHAR,
note VARCHAR
);
CREATE TABLE location_owner (
location_id UUID NOT NULL,
user_id UUID NOT NULL,
CONSTRAINT FK_location_id FOREIGN KEY (location_id) REFERENCES location(id) ON DELETE CASCADE,
CONSTRAINT FK_user_id FOREIGN KEY (user_id) REFERENCES client(id) ON DELETE CASCADE,
PRIMARY KEY(location_id, user_id)
);
ALTER TABLE gamenight
ADD location_id UUID,
ADD CONSTRAINT FK_location_id FOREIGN KEY (location_id) REFERENCES location(id) ON DELETE SET NULL;

View File

@@ -11,4 +11,4 @@ impl From<argon2::password_hash::Error> for DatabaseError {
fn from(value: argon2::password_hash::Error) -> Self {
DatabaseError(value.to_string())
}
}
}

View File

@@ -1,7 +1,9 @@
use diesel::{dsl::insert_into, ExpressionMethods, Insertable, PgConnection, QueryDsl, Queryable, RunQueryDsl};
use serde::{Serialize, Deserialize};
use uuid::Uuid;
use crate::schema::game;
use diesel::{
ExpressionMethods, Insertable, PgConnection, QueryDsl, Queryable, RunQueryDsl, dsl::insert_into,
};
use serde::{Deserialize, Serialize};
use uuid::Uuid;
use super::error::DatabaseError;
@@ -12,7 +14,7 @@ pub struct Game {
pub name: String,
}
pub fn games(conn: &mut PgConnection) -> Result<Vec::<Game>, DatabaseError> {
pub fn games(conn: &mut PgConnection) -> Result<Vec<Game>, DatabaseError> {
Ok(game::table.load::<Game>(conn)?)
}
@@ -24,8 +26,12 @@ pub fn insert_game(conn: &mut PgConnection, game: Game) -> Result<usize, Databas
Ok(insert_into(game::table).values(&game).execute(conn)?)
}
pub fn rename_game(conn: &mut PgConnection, id: Uuid, name: String) -> Result<usize, DatabaseError> {
pub fn rename_game(
conn: &mut PgConnection,
id: Uuid,
name: String,
) -> Result<usize, DatabaseError> {
Ok(diesel::update(game::table.filter(game::id.eq(id)))
.set(game::name.eq(&name))
.execute(conn)?)
}
.execute(conn)?)
}

View File

@@ -1,8 +1,8 @@
use chrono::{DateTime, Utc};
use diesel::{Insertable, Queryable, PgConnection, RunQueryDsl, insert_into, QueryDsl};
use serde::{Serialize, Deserialize};
use uuid::Uuid;
use crate::schema::gamenight;
use chrono::{DateTime, Utc};
use diesel::{Insertable, PgConnection, QueryDsl, Queryable, RunQueryDsl, insert_into};
use serde::{Deserialize, Serialize};
use uuid::Uuid;
use super::error::DatabaseError;
@@ -13,16 +13,22 @@ pub struct Gamenight {
pub name: String,
pub datetime: DateTime<Utc>,
pub owner_id: Uuid,
pub location_id: Option<Uuid>,
}
pub fn gamenights(conn: &mut PgConnection) -> Result<Vec::<Gamenight>, DatabaseError> {
pub fn gamenights(conn: &mut PgConnection) -> Result<Vec<Gamenight>, DatabaseError> {
Ok(gamenight::table.load::<Gamenight>(conn)?)
}
pub fn add_gamenight(conn: &mut PgConnection, gamenight: Gamenight) -> Result<usize, DatabaseError> {
Ok(insert_into(gamenight::table).values(&gamenight).execute(conn)?)
pub fn add_gamenight(
conn: &mut PgConnection,
gamenight: Gamenight,
) -> Result<usize, DatabaseError> {
Ok(insert_into(gamenight::table)
.values(&gamenight)
.execute(conn)?)
}
pub fn get_gamenight(conn: &mut PgConnection, id: Uuid) -> Result<Gamenight, DatabaseError> {
Ok(gamenight::table.find(id).first(conn)?)
}
}

View File

@@ -1,11 +1,13 @@
use diesel::{BoolExpressionMethods, ExpressionMethods, Insertable, QueryDsl, Queryable, RunQueryDsl};
use serde::{Serialize, Deserialize};
use diesel::{
BoolExpressionMethods, ExpressionMethods, Insertable, QueryDsl, Queryable, RunQueryDsl,
};
use serde::{Deserialize, Serialize};
use uuid::Uuid;
use crate::schema::gamenight_participant;
use super::error::DatabaseError;
use super::DbConnection;
use super::error::DatabaseError;
#[derive(Serialize, Deserialize, Debug, Insertable, Queryable)]
#[diesel(belongs_to(Gamenight))]
@@ -23,14 +25,23 @@ pub fn get_participants(conn: &mut DbConnection, id: &Uuid) -> Result<Vec<Uuid>,
.get_results(conn)?)
}
pub fn insert_gamenight_participant(conn: &mut DbConnection, gp: GamenightParticipant) -> Result<usize, DatabaseError> {
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)?)
}
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)?)
}

View File

@@ -1,25 +1,27 @@
pub mod user;
pub mod error;
pub mod schema;
pub mod game;
pub mod gamenight;
pub mod gamenight_participants;
pub mod game;
pub mod location;
pub mod location_owner;
pub mod owned_game;
pub mod schema;
pub mod user;
use diesel::PgConnection;
use diesel::r2d2::ConnectionManager;
use diesel::r2d2::ManageConnection;
use diesel::r2d2::Pool;
use diesel::r2d2::PooledConnection;
use diesel::PgConnection;
use diesel_migrations::embed_migrations;
use diesel_migrations::EmbeddedMigrations;
use diesel_migrations::embed_migrations;
use diesel_migrations::MigrationHarness;
pub use user::login;
pub use user::register;
pub use game::games;
pub use gamenight::gamenights;
pub use gamenight_participants::get_participants;
pub use game::games;
pub use user::login;
pub use user::register;
pub const MIGRATIONS: EmbeddedMigrations = embed_migrations!();
pub type DbConnection = PgConnection;
@@ -38,7 +40,10 @@ pub fn get_connection_pool(url: &str) -> DbPool {
.build(manager)
.expect("Could not build connection pool")
}
pub trait GetConnection<T> where T: ManageConnection {
pub trait GetConnection<T>
where
T: ManageConnection,
{
fn get_conn(&self) -> PooledConnection<T>;
}
@@ -46,4 +51,4 @@ impl<T: ManageConnection> GetConnection<T> for Pool<T> {
fn get_conn(&self) -> PooledConnection<T> {
self.get().expect("Couldn't get db connection from pool")
}
}
}

View File

@@ -0,0 +1,44 @@
use crate::schema::location::{self, id};
use diesel::{
ExpressionMethods, Insertable, PgConnection, QueryDsl, Queryable, RunQueryDsl, dsl::insert_into,
};
use serde::{Deserialize, Serialize};
use uuid::Uuid;
use super::error::DatabaseError;
#[derive(Serialize, Deserialize, Debug, Insertable, Queryable)]
#[diesel(table_name = location)]
pub struct Location {
pub id: Uuid,
pub name: String,
pub address: Option<String>,
pub note: Option<String>,
}
pub fn locations(conn: &mut PgConnection) -> Result<Vec<Location>, DatabaseError> {
Ok(location::table.load::<Location>(conn)?)
}
pub fn load_location(conn: &mut PgConnection, uuid: Uuid) -> Result<Location, DatabaseError> {
Ok(location::table.find(uuid).get_result(conn)?)
}
pub fn insert_location(conn: &mut PgConnection, location: Location) -> Result<Uuid, DatabaseError> {
Ok(insert_into(location::table)
.values(&location)
.returning(id)
.get_result(conn)?)
}
pub fn rename_location(
conn: &mut PgConnection,
uuid: Uuid,
name: String,
) -> Result<usize, DatabaseError> {
Ok(
diesel::update(location::table.filter(location::id.eq(uuid)))
.set(location::name.eq(&name))
.execute(conn)?,
)
}

View File

@@ -0,0 +1,47 @@
use crate::{DbConnection, schema::location_owner, user::DatabaseError};
use diesel::{
BoolExpressionMethods, ExpressionMethods, QueryDsl, RunQueryDsl,
dsl::{delete, insert_into},
prelude::{Insertable, Queryable},
};
use serde::{Deserialize, Serialize};
use uuid::Uuid;
#[derive(Serialize, Deserialize, Debug, Insertable, Queryable)]
#[diesel(table_name = location_owner)]
pub struct LocationOwner {
pub location_id: Uuid,
pub user_id: Uuid,
}
pub fn grant_permission(
conn: &mut DbConnection,
location_owner: LocationOwner,
) -> Result<usize, DatabaseError> {
Ok(insert_into(location_owner::table)
.values(&location_owner)
.execute(conn)?)
}
pub fn revoke_permission(
conn: &mut DbConnection,
location_owner: LocationOwner,
) -> Result<usize, DatabaseError> {
Ok(delete(location_owner::table)
.filter(
location_owner::location_id
.eq(location_owner.location_id)
.and(location_owner::user_id.eq(location_owner.user_id)),
)
.execute(conn)?)
}
pub fn location_permissions(
conn: &mut DbConnection,
location_id: Uuid,
) -> Result<Vec<Uuid>, DatabaseError> {
Ok(location_owner::table
.select(location_owner::user_id)
.filter(location_owner::location_id.eq(location_id))
.get_results(conn)?)
}

View File

@@ -1,28 +1,38 @@
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};
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(table_name = owned_game)]
pub struct OwnedGame {
pub user_id: Uuid,
pub game_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 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)?)
.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)
Ok(owned_game::table
.select(owned_game::game_id)
.filter(owned_game::user_id.eq(uuid))
.get_results(conn)?)
}
}

View File

@@ -31,6 +31,7 @@ diesel::table! {
name -> Varchar,
datetime -> Timestamptz,
owner_id -> Uuid,
location_id -> Nullable<Uuid>,
}
}
@@ -48,6 +49,22 @@ diesel::table! {
}
}
diesel::table! {
location (id) {
id -> Uuid,
name -> Varchar,
address -> Nullable<Varchar>,
note -> Nullable<Varchar>,
}
}
diesel::table! {
location_owner (location_id, user_id) {
location_id -> Uuid,
user_id -> Uuid,
}
}
diesel::table! {
owned_game (user_id, game_id) {
user_id -> Uuid,
@@ -73,10 +90,13 @@ diesel::table! {
}
diesel::joinable!(gamenight -> client (owner_id));
diesel::joinable!(gamenight -> location (location_id));
diesel::joinable!(gamenight_gamelist -> game (game_id));
diesel::joinable!(gamenight_gamelist -> gamenight (gamenight_id));
diesel::joinable!(gamenight_participant -> client (user_id));
diesel::joinable!(gamenight_participant -> gamenight (gamenight_id));
diesel::joinable!(location_owner -> client (user_id));
diesel::joinable!(location_owner -> location (location_id));
diesel::joinable!(owned_game -> client (user_id));
diesel::joinable!(owned_game -> game (game_id));
diesel::joinable!(pwd -> client (user_id));
@@ -87,6 +107,8 @@ diesel::allow_tables_to_appear_in_same_query!(
gamenight,
gamenight_gamelist,
gamenight_participant,
location,
location_owner,
owned_game,
pwd,
registration_tokens,

View File

@@ -1,22 +1,20 @@
use argon2::password_hash::Salt;
use diesel::Connection;
use serde::{Serialize, Deserialize};
use uuid::Uuid;
use diesel::{ExpressionMethods, QueryDsl, RunQueryDsl, Insertable, Queryable};
use diesel_derive_enum::DbEnum;
use argon2::password_hash::SaltString;
use crate::DbConnection;
use argon2::Argon2;
use argon2::PasswordHash;
use argon2::PasswordVerifier;
use argon2::Argon2;
use argon2::password_hash::PasswordHasher;
use argon2::password_hash::Salt;
use argon2::password_hash::SaltString;
use argon2::password_hash::rand_core::OsRng;
use argon2::password_hash::rand_core::RngCore;
use crate::DbConnection;
use diesel::Connection;
use diesel::{ExpressionMethods, Insertable, QueryDsl, Queryable, RunQueryDsl};
use diesel_derive_enum::DbEnum;
use serde::{Deserialize, Serialize};
use uuid::Uuid;
use super::schema::{pwd, client};
pub use super::error::DatabaseError;
use super::schema::{client, pwd};
#[derive(Serialize, Deserialize, Debug, Insertable, Queryable)]
#[diesel(table_name = pwd)]
@@ -38,12 +36,12 @@ pub struct User {
#[ExistingTypePath = "crate::schema::sql_types::Role"]
pub enum Role {
Admin,
User
User,
}
pub struct LoginUser {
pub username: String,
pub password: String
pub password: String,
}
#[derive(Serialize, Deserialize)]
@@ -61,7 +59,7 @@ pub struct RegisterResult {
pub struct Register {
pub username: String,
pub email: String,
pub password: String
pub password: String,
}
pub fn login(conn: &mut DbConnection, user: LoginUser) -> Result<Option<User>, DatabaseError> {
@@ -85,7 +83,7 @@ pub fn login(conn: &mut DbConnection, user: LoginUser) -> Result<Option<User>, D
Ok(Some(client::table.find(id).first(conn)?))
} else {
Ok(None)
}
}
}
pub fn get_user(conn: &mut DbConnection, id: Uuid) -> Result<User, DatabaseError> {
@@ -125,16 +123,26 @@ pub fn register(conn: &mut DbConnection, register: Register) -> Result<(), Datab
})
}
pub fn count_users_with_username(conn: &mut DbConnection, username: &String) -> Result<i64, DatabaseError> {
pub fn count_users_with_username(
conn: &mut DbConnection,
username: &String,
) -> Result<i64, DatabaseError> {
Ok(client::table
.count()
.filter(client::username.eq(username))
.get_result::<i64>(conn)?)
}
pub fn count_users_with_email(conn: &mut DbConnection, email: &String) -> Result<i64, DatabaseError> {
pub fn count_users_with_email(
conn: &mut DbConnection,
email: &String,
) -> Result<i64, DatabaseError> {
Ok(client::table
.count()
.filter(client::email.eq(email))
.get_result::<i64>(conn)?)
}
}
pub fn get_users(conn: &mut DbConnection) -> Result<Vec<User>, DatabaseError> {
Ok(client::table.load(conn)?)
}