diff --git a/src/model/subscriber.rs b/src/model/subscriber.rs index 1d1e8f35710efe89ab147d0e2fda0caa78decd64..d8cb33a07388d1b3bb19999e515745d47c03bc0c 100644 --- a/src/model/subscriber.rs +++ b/src/model/subscriber.rs @@ -17,9 +17,9 @@ impl Subscriber { pub async fn update(&self, payload: Notification) { REQUEST_CLIENT .post(&self.url) - .header("Content-Type", "JSON") // Note: Should probably be "application/json" + .header("Content-Type", "JSON") .body(to_string(&payload).unwrap()) - .send().await.ok(); // Note: It might be better to handle potential errors here + .send().await.ok(); log::warn_!("Sent {} notification of: [{}] {}, to: {}", payload.status, payload.product_type, payload.product_title, self.url); diff --git a/src/service/notification.rs b/src/service/notification.rs index a8eca0d4cb63e005564b8be70e736ab553b042f2..91a8ec69c06e9ccc52b4b383e9cef2b10513b9e5 100644 --- a/src/service/notification.rs +++ b/src/service/notification.rs @@ -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