From bf470ec3df2540833c6243656054e52f14acb0d7 Mon Sep 17 00:00:00 2001 From: Scallss <pascalhafidz2005@gmail.com> Date: Fri, 28 Mar 2025 16:31:48 +0700 Subject: [PATCH] Implement publish function in Program service and Program controller. --- src/controller/mod.rs | 2 +- src/controller/product.rs | 8 ++++++++ src/service/product.rs | 14 ++++++++++++++ 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/controller/mod.rs b/src/controller/mod.rs index fe4a544..6ff4d58 100644 --- a/src/controller/mod.rs +++ b/src/controller/mod.rs @@ -6,7 +6,7 @@ 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..92d3e27 100644 --- a/src/controller/product.rs +++ b/src/controller/product.rs @@ -37,3 +37,11 @@ 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), + }; +} \ No newline at end of file diff --git a/src/service/product.rs b/src/service/product.rs index 775bf09..443b7fa 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