2023-05-30 09:34:16 +00:00
|
|
|
use rocket::{
|
|
|
|
get,
|
|
|
|
http::Status,
|
|
|
|
patch,
|
|
|
|
post,
|
|
|
|
response::status::Custom,
|
|
|
|
serde::json::Json,
|
|
|
|
};
|
2023-05-29 10:14:01 +00:00
|
|
|
|
2023-06-03 14:17:58 +00:00
|
|
|
use neveko_core::*;
|
2023-05-29 10:14:01 +00:00
|
|
|
|
|
|
|
// JSON APIs
|
|
|
|
|
2023-06-03 05:13:56 +00:00
|
|
|
/// Create a product by passings json product
|
2023-05-29 10:14:01 +00:00
|
|
|
#[post("/create", data = "<req_product>")]
|
|
|
|
pub async fn create_product(
|
|
|
|
req_product: Json<models::Product>,
|
|
|
|
_token: auth::BearerToken,
|
|
|
|
) -> Custom<Json<models::Product>> {
|
|
|
|
let m_product: models::Product = product::create(req_product);
|
2023-06-03 05:13:56 +00:00
|
|
|
Custom(Status::Created, Json(m_product))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get a product by passing id
|
2023-06-04 09:27:01 +00:00
|
|
|
#[get("/<pid>")]
|
2023-06-03 05:13:56 +00:00
|
|
|
pub async fn get_product(pid: String, _token: auth::BearerToken) -> Custom<Json<models::Product>> {
|
|
|
|
let m_product: models::Product = product::find(&pid);
|
2023-05-30 09:34:16 +00:00
|
|
|
Custom(Status::Ok, Json(m_product))
|
2023-05-29 10:14:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Update product information
|
2023-06-03 05:13:56 +00:00
|
|
|
#[patch("/update", data = "<product>")]
|
2023-05-29 10:14:01 +00:00
|
|
|
pub async fn update_product(
|
|
|
|
product: Json<models::Product>,
|
|
|
|
_token: auth::BearerToken,
|
|
|
|
) -> Custom<Json<models::Product>> {
|
|
|
|
let m_product: models::Product = product::modify(product);
|
2023-05-30 09:34:16 +00:00
|
|
|
Custom(Status::Ok, Json(m_product))
|
2023-05-29 10:14:01 +00:00
|
|
|
}
|
|
|
|
|
2023-06-03 05:13:56 +00:00
|
|
|
/// Return all products
|
2023-06-04 09:27:01 +00:00
|
|
|
#[get("/")]
|
2023-06-03 05:13:56 +00:00
|
|
|
pub async fn get_products(_token: auth::BearerToken) -> Custom<Json<Vec<models::Product>>> {
|
|
|
|
let m_products: Vec<models::Product> = product::find_all();
|
|
|
|
Custom(Status::Ok, Json(m_products))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get a order by passing id
|
2023-06-04 09:27:01 +00:00
|
|
|
#[get("/<orid>")]
|
2023-06-03 05:13:56 +00:00
|
|
|
pub async fn get_order(orid: String, _token: auth::BearerToken) -> Custom<Json<models::Order>> {
|
|
|
|
let m_order: models::Order = order::find(&orid);
|
|
|
|
Custom(Status::Ok, Json(m_order))
|
|
|
|
}
|
2023-05-29 10:14:01 +00:00
|
|
|
|
2023-06-03 05:13:56 +00:00
|
|
|
/// Get a order by passing id
|
2023-06-04 09:27:01 +00:00
|
|
|
#[get("/")]
|
2023-06-03 05:13:56 +00:00
|
|
|
pub async fn get_orders(_token: auth::BearerToken) -> Custom<Json<Vec<models::Order>>> {
|
|
|
|
let m_orders: Vec<models::Order> = order::find_all();
|
|
|
|
Custom(Status::Ok, Json(m_orders))
|
|
|
|
}
|
2023-05-29 10:14:01 +00:00
|
|
|
|
2023-06-03 05:13:56 +00:00
|
|
|
/// Update order information
|
|
|
|
#[patch("/update", data = "<order>")]
|
|
|
|
pub async fn update_order(
|
|
|
|
order: Json<models::Order>,
|
|
|
|
_token: auth::BearerToken,
|
|
|
|
) -> Custom<Json<models::Order>> {
|
|
|
|
let m_order: models::Order = order::modify(order);
|
|
|
|
Custom(Status::Ok, Json(m_order))
|
|
|
|
}
|
2023-05-29 10:14:01 +00:00
|
|
|
|
|
|
|
/// Create a dispute
|
|
|
|
#[post("/create", data = "<dispute>")]
|
|
|
|
pub async fn create_dispute(
|
|
|
|
dispute: Json<models::Dispute>,
|
|
|
|
_token: auth::BearerToken,
|
|
|
|
) -> Custom<Json<models::Dispute>> {
|
|
|
|
let m_dispute: models::Dispute = dispute::create(dispute);
|
2023-05-30 09:34:16 +00:00
|
|
|
Custom(Status::Ok, Json(m_dispute))
|
2023-05-29 10:14:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Create a dispute
|
|
|
|
#[get("/<did>")]
|
2023-05-30 09:34:16 +00:00
|
|
|
pub async fn get_dispute(_token: auth::BearerToken, did: String) -> Custom<Json<models::Dispute>> {
|
2023-05-29 10:14:01 +00:00
|
|
|
let m_dispute: models::Dispute = dispute::find(&did);
|
2023-05-30 09:34:16 +00:00
|
|
|
Custom(Status::Ok, Json(m_dispute))
|
2023-05-29 10:14:01 +00:00
|
|
|
}
|
|
|
|
// END JSON APIs
|