From ed3ac2abcb3f5865a6d645047a5e891d071697ac Mon Sep 17 00:00:00 2001 From: Artus Date: Sat, 19 Oct 2019 21:59:17 +0200 Subject: [PATCH] adds notifications table and models --- .../2019-10-19-194459_notifications/down.sql | 1 + .../2019-10-19-194459_notifications/up.sql | 6 ++++ lootalot_db/src/models/player/mod.rs | 1 + lootalot_db/src/models/player/notification.rs | 35 +++++++++++++++++++ lootalot_db/src/schema.rs | 17 ++++++++- 5 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 lootalot_db/migrations/2019-10-19-194459_notifications/down.sql create mode 100644 lootalot_db/migrations/2019-10-19-194459_notifications/up.sql create mode 100644 lootalot_db/src/models/player/notification.rs diff --git a/lootalot_db/migrations/2019-10-19-194459_notifications/down.sql b/lootalot_db/migrations/2019-10-19-194459_notifications/down.sql new file mode 100644 index 0000000..45f6e69 --- /dev/null +++ b/lootalot_db/migrations/2019-10-19-194459_notifications/down.sql @@ -0,0 +1 @@ +DROP TABLE notifications; diff --git a/lootalot_db/migrations/2019-10-19-194459_notifications/up.sql b/lootalot_db/migrations/2019-10-19-194459_notifications/up.sql new file mode 100644 index 0000000..bf48543 --- /dev/null +++ b/lootalot_db/migrations/2019-10-19-194459_notifications/up.sql @@ -0,0 +1,6 @@ +CREATE TABLE notifications ( + id INTEGER PRIMARY KEY NOT NULL, + player_id INTEGER NOT NULL, + text VARCHAR NOT NULL, + FOREIGN KEY (player_id) REFERENCES players(id) +); diff --git a/lootalot_db/src/models/player/mod.rs b/lootalot_db/src/models/player/mod.rs index 2567017..6f3cdda 100644 --- a/lootalot_db/src/models/player/mod.rs +++ b/lootalot_db/src/models/player/mod.rs @@ -2,6 +2,7 @@ use crate::schema::players; use crate::{DbConnection, QueryResult}; use diesel::prelude::*; +mod notification; pub mod wealth; pub use wealth::Wealth; diff --git a/lootalot_db/src/models/player/notification.rs b/lootalot_db/src/models/player/notification.rs new file mode 100644 index 0000000..1b85a7c --- /dev/null +++ b/lootalot_db/src/models/player/notification.rs @@ -0,0 +1,35 @@ +use diesel::prelude::*; +use crate::{ + DbConnection, + schema::notifications, + models::player::Player, +}; + +#[derive(Identifiable, Queryable, Associations, Serialize, Debug)] +#[belongs_to(Player)] +struct Notification { + pub id: i32, + pub player_id: i32, + pub text: String, +} + +impl Notification { + fn add<'a, S: Into<&'a str>>(conn: &DbConnection, id: i32, text: S) -> QueryResult { + diesel::insert_into(notifications::table) + .values(&NewNotification { + player_id: id, + text: text.into(), + }) + .execute(conn)?; + notifications::table + .order(notifications::dsl::id.desc()) + .first(conn) + } +} + +#[derive(Insertable)] +#[table_name="notifications"] +struct NewNotification<'a> { + player_id: i32, + text: &'a str, +} diff --git a/lootalot_db/src/schema.rs b/lootalot_db/src/schema.rs index 3989f64..c5e41fe 100644 --- a/lootalot_db/src/schema.rs +++ b/lootalot_db/src/schema.rs @@ -24,6 +24,14 @@ table! { } } +table! { + notifications (id) { + id -> Integer, + player_id -> Integer, + text -> Text, + } +} + table! { players (id) { id -> Integer, @@ -39,5 +47,12 @@ table! { joinable!(claims -> looted (loot_id)); joinable!(claims -> players (player_id)); joinable!(looted -> players (owner_id)); +joinable!(notifications -> players (player_id)); -allow_tables_to_appear_in_same_query!(claims, items, looted, players,); +allow_tables_to_appear_in_same_query!( + claims, + items, + looted, + notifications, + players, +);