diff --git a/src/controller/mod.rs b/src/controller/mod.rs index df6df1a2a3c3ee7c538caba296b381784260470a..4233ca8177df7c4c6f29087c4d11fc7e171afa7c 100644 --- a/src/controller/mod.rs +++ b/src/controller/mod.rs @@ -4,9 +4,9 @@ pub mod notification; use rocket::fairing::AdHoc; pub fn route_stage() -> AdHoc { - return AdHoc::on_ignite("Initializing controller routes...", |rocket| async { + 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]) }); } \ No newline at end of file diff --git a/src/controller/product.rs b/src/controller/product.rs index 1cc538419d9a5a7f6b73e520417c04426fefbb3b..ab9159ff42ad26bb2fd9e9ba2736723c428326d6 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) + }; +} diff --git a/src/service/product.rs b/src/service/product.rs index 775bf09d1d171391547c61071a0f17c976131036..64261da0dcea508c40a1675890559797b7c62786 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); + } }