From 55dc7a43b07f2a1caaad2b5d4ed9e4e362bea658 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Sat, 11 Jul 2020 13:47:07 +0200 Subject: [PATCH] Can delete a single notification --- src/controllers/notifications_controller.rs | 17 ++++++++++++-- src/data/notification.rs | 4 ++++ src/helpers/database.rs | 6 +++++ src/helpers/notifications_helper.rs | 26 ++++++++++++++++++++- 4 files changed, 50 insertions(+), 3 deletions(-) diff --git a/src/controllers/notifications_controller.rs b/src/controllers/notifications_controller.rs index d61d0f0..cfe656e 100644 --- a/src/controllers/notifications_controller.rs +++ b/src/controllers/notifications_controller.rs @@ -58,6 +58,19 @@ pub fn mark_seen(r: &mut HttpRequestHandler) -> RequestResult { let notif_id = r.post_notif_id("notifID")?; let delete_similar = r.post_bool_opt("delete_similar", false); - // TODO : continue implementation - r.success("continue implementation") + // Check if we are targeting a precise notification or an undetermined number of similar + // notifications + if !delete_similar { + notifications_helper::delete( + &PartialNotification::new() + .set_id(notif_id) + )?; + } + + else { + // TODO : implement me + unimplemented!(); + } + + r.success("Notification deleted") } \ No newline at end of file diff --git a/src/data/notification.rs b/src/data/notification.rs index 1c8064a..0e334cd 100644 --- a/src/data/notification.rs +++ b/src/data/notification.rs @@ -189,6 +189,10 @@ impl PartialNotification { self } + pub fn has_id(&self) -> bool { + self.id.is_some() + } + pub fn set_dest_user_id(mut self, id: &UserID) -> PartialNotification { self.dest_user_id = Some(id.clone()); self diff --git a/src/helpers/database.rs b/src/helpers/database.rs index 3d1be84..6a9787f 100644 --- a/src/helpers/database.rs +++ b/src/helpers/database.rs @@ -660,6 +660,12 @@ impl DeleteQuery { } } + /// Add batch conditions + pub fn add_conditions(mut self, conditions: HashMap) -> Self { + self.conditions.extend(conditions.into_iter()); + self + } + /// Add a string condition pub fn cond_str(mut self, key: &str, value: &str) -> DeleteQuery { self.conditions.insert(key.to_string(), Value::from(value)); diff --git a/src/helpers/notifications_helper.rs b/src/helpers/notifications_helper.rs index 8842b0d..bc7c52d 100644 --- a/src/helpers/notifications_helper.rs +++ b/src/helpers/notifications_helper.rs @@ -10,7 +10,31 @@ use crate::data::notification::{NotifElemType, NotifEventType, NotifEventVisibil use crate::data::user::UserID; use crate::helpers::database; -/// check out whether a similar notification exists for given specifications +/// Delete notifications +pub fn delete(notification: &PartialNotification) -> ResultBoxError { + + // Check if we have to delete a specific notification or a group of similar notifications + let conditions = match notification.id { + Some(id) => { + let mut map = HashMap::new(); + map.insert("id".to_string(), mysql::Value::from(id)); + map + } + + // Delete similar notifications + None => { + notif_to_db(notification, false) + } + }; + + + // Delete the notifications + database::DeleteQuery::new(NOTIFICATIONS_TABLE) + .add_conditions(conditions) + .exec() +} + +/// Check out whether a similar notification exists for given specifications pub fn similar_exists(n: &PartialNotification) -> ResultBoxError { database::QueryInfo::new(NOTIFICATIONS_TABLE) .add_conditions(¬if_to_db(n, false))