adds notifications table and models

This commit is contained in:
2019-10-19 21:59:17 +02:00
parent 2248e25aec
commit ed3ac2abcb
5 changed files with 59 additions and 1 deletions

View File

@@ -2,6 +2,7 @@ use crate::schema::players;
use crate::{DbConnection, QueryResult};
use diesel::prelude::*;
mod notification;
pub mod wealth;
pub use wealth::Wealth;

View File

@@ -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<Notification> {
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,
}

View File

@@ -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,
);