From 32aa7e7d1eb68d54d9150f964f6ce468c3a99e79 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Fri, 10 Jul 2020 12:55:50 +0200 Subject: [PATCH] Can count the number of unread notifications --- src/api_data/mod.rs | 3 ++- src/api_data/res_number_unread_notifications.rs | 16 ++++++++++++++++ src/constants.rs | 3 +++ src/controllers/mod.rs | 1 + src/controllers/notifications_controller.rs | 14 ++++++++++++++ src/controllers/routes.rs | 4 +++- src/helpers/mod.rs | 3 ++- src/helpers/notifications_helper.rs | 17 +++++++++++++++++ 8 files changed, 58 insertions(+), 3 deletions(-) create mode 100644 src/api_data/res_number_unread_notifications.rs create mode 100644 src/controllers/notifications_controller.rs create mode 100644 src/helpers/notifications_helper.rs diff --git a/src/api_data/mod.rs b/src/api_data/mod.rs index c33e174..139587e 100644 --- a/src/api_data/mod.rs +++ b/src/api_data/mod.rs @@ -37,4 +37,5 @@ pub mod survey_api; pub mod comment_api; pub mod res_create_post; pub mod posts_targets_api; -pub mod res_create_comment; \ No newline at end of file +pub mod res_create_comment; +pub mod res_number_unread_notifications; \ No newline at end of file diff --git a/src/api_data/res_number_unread_notifications.rs b/src/api_data/res_number_unread_notifications.rs new file mode 100644 index 0000000..36c3433 --- /dev/null +++ b/src/api_data/res_number_unread_notifications.rs @@ -0,0 +1,16 @@ +//! # Number of unread notifications +//! +//! @author Pierre Hubert + +use serde::Serialize; + +#[derive(Serialize)] +pub struct ResNumberUnreadNotifications { + number: u64 +} + +impl ResNumberUnreadNotifications { + pub fn new(number: u64) -> ResNumberUnreadNotifications { + ResNumberUnreadNotifications { number } + } +} \ No newline at end of file diff --git a/src/constants.rs b/src/constants.rs index d71508c..63d4a22 100644 --- a/src/constants.rs +++ b/src/constants.rs @@ -52,6 +52,9 @@ pub mod database_tables_names { /// Comments table pub const COMMENTS_TABLE: &str = "commentaires"; + + /// Notifications table + pub const NOTIFICATIONS_TABLE: &str = "comunic_notifications"; } /// The account image to show for user who do not have any diff --git a/src/controllers/mod.rs b/src/controllers/mod.rs index 37bc7d1..8e79236 100644 --- a/src/controllers/mod.rs +++ b/src/controllers/mod.rs @@ -12,5 +12,6 @@ pub mod posts_controller; pub mod comments_controller; pub mod likes_controller; pub mod surveys_controller; +pub mod notifications_controller; pub mod movies_controller; pub mod virtual_directory_controller; diff --git a/src/controllers/notifications_controller.rs b/src/controllers/notifications_controller.rs new file mode 100644 index 0000000..a522d13 --- /dev/null +++ b/src/controllers/notifications_controller.rs @@ -0,0 +1,14 @@ +//! # Notifications controller +//! +//! @author Pierre Hubert + +use crate::api_data::res_number_unread_notifications::ResNumberUnreadNotifications; +use crate::controllers::routes::RequestResult; +use crate::data::http_request_handler::HttpRequestHandler; +use crate::helpers::notifications_helper; + +/// Count the number of unread notifications +pub fn count_unread(r: &mut HttpRequestHandler) -> RequestResult { + let number = notifications_helper::count_unread(r.user_id_ref()?)?; + r.set_response(ResNumberUnreadNotifications::new(number)) +} \ No newline at end of file diff --git a/src/controllers/routes.rs b/src/controllers/routes.rs index 51c6534..9dcccd3 100644 --- a/src/controllers/routes.rs +++ b/src/controllers/routes.rs @@ -1,6 +1,6 @@ use std::error::Error; -use crate::controllers::{account_controller, comments_controller, conversations_controller, friends_controller, groups_controller, likes_controller, movies_controller, posts_controller, search_controller, server_controller, surveys_controller, user_controller, virtual_directory_controller}; +use crate::controllers::{account_controller, comments_controller, conversations_controller, friends_controller, groups_controller, likes_controller, movies_controller, notifications_controller, posts_controller, search_controller, server_controller, surveys_controller, user_controller, virtual_directory_controller}; use crate::controllers::routes::Method::{GET, POST}; use crate::data::http_request_handler::HttpRequestHandler; @@ -244,6 +244,8 @@ pub fn get_routes() -> Vec { Route::post("/surveys/block_new_choices_creation", Box::new(surveys_controller::block_new_choices_creation)), + // Notifications controller + Route::post("/notifications/count_unread", Box::new(notifications_controller::count_unread)), // Movies controller Route::post("/movies/get_list", Box::new(movies_controller::get_list)), diff --git a/src/helpers/mod.rs b/src/helpers/mod.rs index d764aba..7676e7f 100644 --- a/src/helpers/mod.rs +++ b/src/helpers/mod.rs @@ -13,4 +13,5 @@ pub mod conversations_helper; pub mod virtual_directory_helper; pub mod movies_helper; pub mod survey_helper; -pub mod comments_helper; \ No newline at end of file +pub mod comments_helper; +pub mod notifications_helper; \ No newline at end of file diff --git a/src/helpers/notifications_helper.rs b/src/helpers/notifications_helper.rs new file mode 100644 index 0000000..da07f64 --- /dev/null +++ b/src/helpers/notifications_helper.rs @@ -0,0 +1,17 @@ +//! # Notifications helper +//! +//! @author Pierre Hubert + +use crate::constants::database_tables_names::NOTIFICATIONS_TABLE; +use crate::data::error::ResultBoxError; +use crate::data::user::UserID; +use crate::helpers::database; + +/// Count the number of unread notifications +pub fn count_unread(user_id: &UserID) -> ResultBoxError { + database::QueryInfo::new(NOTIFICATIONS_TABLE) + .cond_user_id("dest_user_id", user_id) + .cond_legacy_bool("seen", false) + .exec_count() + .map(|c| c as u64) +} \ No newline at end of file