diff --git a/src/service/notification.rs b/src/service/notification.rs index 2319c8381bb1e0bb06a627fc8d5871966e725113..e8c26753fdfee349c1742d21c6df53ed0fb37705 100644 --- a/src/service/notification.rs +++ b/src/service/notification.rs @@ -4,7 +4,7 @@ use bambangshop::{Result, compose_error_response}; use rocket::http::Status; use crate::model::notification::Notification; use crate::model::product::{self, Product}; -use crate::model::subscriber::Subscriber; +use crate::model::subscriber::{self, Subscriber}; use crate::repository::subscriber::SubscriberRepository; pub struct NotificationService; @@ -29,4 +29,22 @@ impl NotificationService{ } return Ok(result.unwrap()); } + + pub fn notify(&self, product_type: &str, status: &str, product: Product) { + let mut payload: Notification = Notification { + product_title: product.clone().title, + product_type: String::from(product_type), + product_url: product.clone().get_url(), + subscriber_name: String::from(""), + status: String::from(status) + }; + + let subscribers: Vec<Subscriber> = SubscriberRepository::list_all(product_type); + for subscriber in subscribers { + payload.subscriber_name = subscriber.clone().name; + let subscriber_clone = subscriber.clone(); + let payload_clone = payload.clone(); + thread::spawn(move || subscriber_clone.update(payload_clone)); + } + } } \ No newline at end of file