50 lines
1.2 KiB
Rust
50 lines
1.2 KiB
Rust
use actix_web::{get, HttpResponse, post, web};
|
|
|
|
use crate::models::order::*;
|
|
use crate::utils::api_error::ApiError;
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
/// Model for user input for an __Order__ item
|
|
#[derive(Serialize, Deserialize, Debug)]
|
|
pub struct InputOrderDetail {
|
|
pub productid: i32,
|
|
pub quantity: i32,
|
|
}
|
|
|
|
/// Model for user input for an __Order__
|
|
#[derive(Serialize, Deserialize, Debug)]
|
|
pub struct InputOrder {
|
|
pub email: String,
|
|
pub line_items: Vec<InputOrderDetail>
|
|
}
|
|
|
|
#[get("/orders")]
|
|
async fn find_all() -> core::result::Result<HttpResponse, ApiError> {
|
|
Ok(HttpResponse::Ok().json(Order::find_all()))
|
|
}
|
|
|
|
/// Accepts JSON input in the following format:-
|
|
/// ```json
|
|
/// {
|
|
/// "email": "mail@id.com",
|
|
/// "line_items": [
|
|
/// {
|
|
/// "productid": 1,
|
|
/// "quantity": 10
|
|
/// },
|
|
/// {
|
|
/// "productid": 2,
|
|
/// "quantity": 30
|
|
/// }
|
|
/// ]
|
|
///}
|
|
/// ```
|
|
#[post("/orders")]
|
|
async fn insert_order(order: web::Json<InputOrder>) -> core::result::Result<HttpResponse, ApiError> {
|
|
Ok(HttpResponse::Ok().json(Order::insert(order.into_inner())))
|
|
}
|
|
|
|
pub fn routes(cfg: &mut web::ServiceConfig) {
|
|
cfg.service(find_all);
|
|
cfg.service(insert_order);
|
|
} |