diff --git a/src/controller/mod.rs b/src/controller/mod.rs index e707497a3727886bf519e6a14ddfc27da373e956..c1a54e6e0cb58c2489374ef953851a53aadc9d1c 100644 --- a/src/controller/mod.rs +++ b/src/controller/mod.rs @@ -12,7 +12,8 @@ pub fn route_stage() -> AdHoc { product::create, product::list, product::read, - product::delete + product::delete, + product::publish ], ) .mount( diff --git a/src/controller/product.rs b/src/controller/product.rs index 1cc538419d9a5a7f6b73e520417c04426fefbb3b..224a61d8fb8a48db8e408cfee468ee66e9ca84d6 100644 --- a/src/controller/product.rs +++ b/src/controller/product.rs @@ -36,4 +36,13 @@ pub fn delete(id: usize) -> Result<Json<Product>> { Ok(f) => Ok(Json::from(f)), 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 775bf09d1d171391547c61071a0f17c976131036..8369537b02b9dab7576202c88f9c447a084c1a74 100644 --- a/src/service/product.rs +++ b/src/service/product.rs @@ -5,6 +5,8 @@ use bambangshop::{Result, compose_error_response}; use crate::model::product::Product; use crate::repository::product::ProductRepository; +use super::notification::NotificationService; + pub struct ProductService; impl ProductService { @@ -42,4 +44,17 @@ impl ProductService { return Ok(Json::from(product)); } + + pub fn publish(id: usize) -> Result<Product> { + let product_opt = 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_opt.unwrap(); + NotificationService.notify(&product.product_type, "PROMOTION", product.clone()); + return Ok(product); + } }