From e4bc3fca47b6e413e7c35765a284fa6396b618c3 Mon Sep 17 00:00:00 2001 From: Andrew4Coding <andrewdevitoaryo@gmail.com> Date: Fri, 28 Mar 2025 13:14:51 +0800 Subject: [PATCH] Implement receive_notification function in Notification service --- src/controller/mod.rs | 2 +- src/controller/notification.rs | 8 ++++++++ src/repository/notification.rs | 4 ++-- src/service/notification.rs | 6 ++++++ 4 files changed, 17 insertions(+), 3 deletions(-) diff --git a/src/controller/mod.rs b/src/controller/mod.rs index 83e8f32..782fc65 100644 --- a/src/controller/mod.rs +++ b/src/controller/mod.rs @@ -4,6 +4,6 @@ use rocket::fairing::AdHoc; pub fn route_stage() -> AdHoc { return AdHoc::on_ignite("Initializing controller routes...", |rocket| async { rocket - .mount("/", routes![notification::subscribe]) + .mount("/", routes![notification::subscribe, notification::unsubscribe]) }); } diff --git a/src/controller/notification.rs b/src/controller/notification.rs index a027fa8..1bf721a 100644 --- a/src/controller/notification.rs +++ b/src/controller/notification.rs @@ -11,4 +11,12 @@ pub fn subscribe(product_type: &str) -> Result<Json<SubscriberRequest>> { Ok(f) => Ok(Json::from(f)), Err(e) => Err(e) } +} + +#[get("/unsubscribe/<product_type>")] +pub fn unsubscribe(product_type: &str) -> Result<Json<SubscriberRequest>> { + return match NotificationService::unsubscribe(product_type) { + Ok(f) => Ok(Json::from(f)), + Err(e) => Err(e) + } } \ No newline at end of file diff --git a/src/repository/notification.rs b/src/repository/notification.rs index 202f9ca..7d569b6 100644 --- a/src/repository/notification.rs +++ b/src/repository/notification.rs @@ -11,10 +11,10 @@ lazy_static! { pub struct NotificationRepository; impl NotificationRepository { - pub async fn add_notification(notification: Notification) -> Notification { + pub fn add(notification: Notification) -> Notification { NOTIFICATIONS.write().unwrap().push(notification.clone()); - notification + return notification; } pub fn list_all_as_string() -> Vec<String> { diff --git a/src/service/notification.rs b/src/service/notification.rs index 51a24a1..9ca0a85 100644 --- a/src/service/notification.rs +++ b/src/service/notification.rs @@ -8,6 +8,7 @@ use rocket::tokio; use bambangshop_receiver::{APP_CONFIG, REQWEST_CLIENT, Result, compose_error_response}; +use crate::controller::notification::subscribe; use crate::model::notification::Notification; use crate::model::subscriber::SubscriberRequest; use crate::repository::notification::NotificationRepository; @@ -92,4 +93,9 @@ impl NotificationService { .join() .unwrap(); } + + pub fn receive_notification(payload: Notification) -> Result<Notification> { + let subscriber_result = NotificationRepository::add(payload); + return Ok(subscriber_result); + } } \ No newline at end of file -- GitLab