routing the rest of implemented features, hits response problem

This commit is contained in:
2019-10-20 16:09:16 +02:00
parent ed3ac2abcb
commit edf236ef8c
5 changed files with 60 additions and 9 deletions

View File

@@ -49,13 +49,20 @@ impl<'q> Players<'q> {
}
/// Notify all players of an event
pub fn notifiy_all<S: Into<String>>(&self, text: S) -> QueryResult<()> {
Err(diesel::result::Error::NotFound)
pub fn notifiy_all(&self, text: &str) -> QueryResult<()> {
for id in self.all()?
.into_iter()
.map(|p| p.id)
{
self.notify(id, text);
}
Ok(())
}
/// Notify a single player of an event
pub fn notify<S: Into<String>>(&self, id: i32, text: S) -> QueryResult<()> {
Err(diesel::result::Error::NotFound)
pub fn notify(&self, id: i32, text: &str) -> QueryResult<()> {
let _ = notification::Notification::add(self.0, id, text)?;
Ok(())
}
}
@@ -63,6 +70,10 @@ impl<'q> Players<'q> {
pub struct AsPlayer<'q>(pub &'q DbConnection, pub i32);
impl<'q> AsPlayer<'q> {
/// Fetch notifications for this player
pub fn notifications(&self) -> QueryResult<Vec<String>> {
notification::pop_all_for(self.1, self.0)
}
/// Updates this player's wealth, returning the difference
pub fn update_wealth(&self, value_in_gp: f64) -> QueryResult<Wealth> {
use crate::schema::players::dsl::*;

View File

@@ -5,16 +5,25 @@ use crate::{
models::player::Player,
};
/// Pops all notification for a player, deleting the database entities
pub(super) fn pop_all_for(id: i32, conn: &DbConnection) -> QueryResult<Vec<String>> {
let select = notifications::table.filter(notifications::dsl::player_id.eq(id));
let popped = select.load(conn)?;
diesel::delete(select).execute(conn)?;
Ok(popped.into_iter().map(|n: Notification| n.text).collect())
}
#[derive(Identifiable, Queryable, Associations, Serialize, Debug)]
#[belongs_to(Player)]
struct Notification {
pub(super) 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> {
pub(super) 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,