Fakultas Ilmu Komputer UI

Skip to content
Snippets Groups Projects
Commit c34136cd authored by Ferdinand57's avatar Ferdinand57
Browse files

Implement publish function in Program service and Program controller.

parent b7ab15e2
No related branches found
No related tags found
No related merge requests found
......@@ -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])
});
}
......@@ -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
......@@ -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);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment