Fakultas Ilmu Komputer UI

Skip to content
Snippets Groups Projects
Commit 10a20e90 authored by Christian Raphael Heryanto's avatar Christian Raphael Heryanto
Browse files

Implement publish function in Program service and Program controller.

parent 075d3910
No related branches found
No related tags found
No related merge requests found
......@@ -12,7 +12,8 @@ pub fn route_stage() -> AdHoc {
product::create,
product::list,
product::read,
product::delete
product::delete,
product::publish,
],
)
.mount(
......
use rocket::response::status::Created;
use rocket::serde::json::Json;
use bambangshop::Result;
use crate::model::product::Product;
use crate::service::product::ProductService;
use bambangshop::Result;
#[post("/", data = "<product>")]
pub fn create(product: Json<Product>) -> Result<Created<Json<Product>>> {
return match ProductService::create(product.into_inner()) {
Ok(f) => Ok(Created::new("/").body(Json::from(f))),
Err(e) => Err(e)
Err(e) => Err(e),
};
}
......@@ -18,7 +17,7 @@ pub fn create(product: Json<Product>) -> Result<Created<Json<Product>>> {
pub fn list() -> Result<Json<Vec<Product>>> {
return match ProductService::list() {
Ok(f) => Ok(Json::from(f)),
Err(e) => Err(e)
Err(e) => Err(e),
};
}
......@@ -26,7 +25,7 @@ pub fn list() -> Result<Json<Vec<Product>>> {
pub fn read(id: usize) -> Result<Json<Product>> {
return match ProductService::read(id) {
Ok(f) => Ok(Json::from(f)),
Err(e) => Err(e)
Err(e) => Err(e),
};
}
......@@ -34,6 +33,14 @@ pub fn read(id: usize) -> Result<Json<Product>> {
pub fn delete(id: usize) -> Result<Json<Product>> {
return match ProductService::delete(id) {
Ok(f) => Ok(Json::from(f)),
Err(e) => Err(e)
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),
};
}
use rocket::http::Status;
use rocket::serde::json::Json;
use bambangshop::{Result, compose_error_response};
use crate::model::product::Product;
use crate::repository::product::ProductRepository;
use crate::service::notification::NotificationService;
use bambangshop::{compose_error_response, Result};
use rocket::http::Status;
use rocket::serde::json::Json;
pub struct ProductService;
......@@ -24,7 +24,7 @@ impl ProductService {
if product_opt.is_none() {
return Err(compose_error_response(
Status::NotFound,
String::from("Product not found.")
String::from("Product not found."),
));
}
return Ok(product_opt.unwrap());
......@@ -35,11 +35,25 @@ impl ProductService {
if product_opt.is_none() {
return Err(compose_error_response(
Status::NotFound,
String::from("Product not found.")
String::from("Product not found."),
));
}
let product: Product = product_opt.unwrap();
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.
Finish editing this message first!
Please register or to comment