diff --git a/src/controller/mod.rs b/src/controller/mod.rs index 83e8f322a93d68d51bf9f3ecb35d726199f0e741..782fc656ec155f1cfd6685a86b27e0a83025f5e7 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 a027fa8e227638728efde42bffd9691831a76b6e..1bf721ad89fc16a54c2e0a525e1ff2449ceff92a 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 202f9ca3e3c6143de45f1847551c7b0dc291c251..7d569b6ec1808770ed55bd65389d6e83947f797f 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 51a24a1e920a37e6466256a79f29c4ac3c1d1c50..9ca0a85b496608d9eb5eab8e9eb247a9c4475d46 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