From 30c822efe2d6ff4ccc51a49c8c84a640e36db8ce Mon Sep 17 00:00:00 2001 From: lantry-glitch <allan.kwek.18@gmail.com> Date: Fri, 28 Mar 2025 14:51:11 +0700 Subject: [PATCH] Implement publish function in Program service and Program controller. --- src/controller/mod.rs | 3 ++- src/controller/product.rs | 9 +++++++++ src/model/subscriber.rs | 4 ++-- src/service/product.rs | 14 ++++++++++++++ 4 files changed, 27 insertions(+), 3 deletions(-) diff --git a/src/controller/mod.rs b/src/controller/mod.rs index fe4a544..9fd1483 100644 --- a/src/controller/mod.rs +++ b/src/controller/mod.rs @@ -6,7 +6,8 @@ use rocket::fairing::AdHoc; pub fn route_stage() -> AdHoc { return AdHoc::on_ignite("Initializing controller routes...", |rocket| async { rocket - .mount("/product", routes![product::create, product::list, product::read, product::delete]) + .mount("/product", routes![product::create, product::list, + product::read, product::delete, product::publish]) .mount("/notification", routes![notification::subscribe, notification::unsubscribe]) }); } diff --git a/src/controller/product.rs b/src/controller/product.rs index 1cc5384..b67edee 100644 --- a/src/controller/product.rs +++ b/src/controller/product.rs @@ -37,3 +37,12 @@ pub fn delete(id: usize) -> Result<Json<Product>> { Err(e) => Err(e) }; } + +#[post("/<id>/publish")] +pub fn publish(id: usize) -> Result<Json<Product>> { + return match ProductService::publish(id) { + Ok(f) => Ok(Json::from(f)), + Err(e) => Err(e) + }; +} + diff --git a/src/model/subscriber.rs b/src/model/subscriber.rs index da38175..4786d58 100644 --- a/src/model/subscriber.rs +++ b/src/model/subscriber.rs @@ -15,12 +15,12 @@ pub struct Subscriber { impl Subscriber { #[tokio::main] pub async fn update(&self, payload: Notification) { - REQUEST_CLIENT + REQWEST_CLIENT .post(&self.url) .header("Content-Type", "JSON") .body(to_string(&payload).unwrap()) .send().await.ok(); - log::warn!("Sent {} notification of: [{}] {}, to: {}", + log::warn_!("Sent {} notification of: [{}] {}, to: {}", payload.status, payload.product_type, payload.product_title, self.url); } } diff --git a/src/service/product.rs b/src/service/product.rs index 775bf09..5d4c240 100644 --- a/src/service/product.rs +++ b/src/service/product.rs @@ -42,4 +42,18 @@ impl ProductService { return Ok(Json::from(product)); } + + pub fn publish(id: usize) -> Result<Product> { + let product_opt: Option<Product> = ProductRepository::get_by_id(id); + if product_opt.is_none() { + return Err(compose_error_response( + Status::NotFound, + String::from("Product not found.") + )); + } + let product: Product = product_opt.unwrap(); + NotificationService.notify(&product.product_type, "PROMOTION", product.clone()); + return Ok(product); + } + } -- GitLab