Add logic to unpick the box
This commit is contained in:
201
src/loota.rs
201
src/loota.rs
@@ -1,17 +1,20 @@
|
||||
use actix_web::{get, post, web, HttpResponse, Responder};
|
||||
use chrono::{NaiveDateTime};
|
||||
use diesel::prelude::*;
|
||||
use diesel::result::Error;
|
||||
use uuid::Uuid;
|
||||
use crate::constants::APPLICATION_JSON;
|
||||
use crate::connection::establish_connection;
|
||||
use crate::models::{Customer, Box, Order, LootaResponse, LootaBoxResponse, LootaRequest, LootaOrderAdminResponse, LootaUserAdminResponse, LootaBoxAdminResponse, LootaBoxOrderAdminResponse};
|
||||
use crate::constants::APPLICATION_JSON;
|
||||
use crate::models::{
|
||||
Box, Customer, LootaBoxAdminResponse, LootaBoxOrderAdminResponse, LootaBoxResponse,
|
||||
LootaOrderAdminResponse, LootaRequest, LootaResponse, LootaUserAdminResponse, Order,
|
||||
};
|
||||
use crate::schema::loota_admin::dsl::loota_admin;
|
||||
use crate::schema::loota_box::dsl::loota_box;
|
||||
use crate::schema::loota_box::{delivery_date, id, pickup_date};
|
||||
use crate::schema::loota_customer::dsl::loota_customer;
|
||||
use crate::schema::loota_customer::identifier;
|
||||
use crate::schema::loota_order::dsl::loota_order;
|
||||
use actix_web::{HttpResponse, Responder, get, post, web};
|
||||
use chrono::NaiveDateTime;
|
||||
use diesel::prelude::*;
|
||||
use diesel::result::Error;
|
||||
use uuid::Uuid;
|
||||
|
||||
fn find_boxes(_identifier: String, _order: &Order, conn: &mut PgConnection) -> LootaResponse {
|
||||
let boxes = Box::belonging_to(_order)
|
||||
@@ -21,12 +24,10 @@ fn find_boxes(_identifier: String, _order: &Order, conn: &mut PgConnection) -> L
|
||||
|
||||
match boxes {
|
||||
Ok(boxes) => {
|
||||
let _box_responses = boxes.iter().map(|b| {
|
||||
LootaBoxResponse {
|
||||
id: b.id.to_string(),
|
||||
delivery_date: b.delivery_date,
|
||||
pickup_date: b.pickup_date,
|
||||
}
|
||||
let _box_responses = boxes.iter().map(|b| LootaBoxResponse {
|
||||
id: b.id.to_string(),
|
||||
delivery_date: b.delivery_date,
|
||||
pickup_date: b.pickup_date,
|
||||
});
|
||||
|
||||
LootaResponse {
|
||||
@@ -46,11 +47,13 @@ fn find_boxes(_identifier: String, _order: &Order, conn: &mut PgConnection) -> L
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fn find_admin_boxes(delivery_date_param: String, conn: &mut PgConnection) -> Result<Vec<LootaBoxAdminResponse>, Error> {
|
||||
let parsed_date = chrono::NaiveDateTime::parse_from_str(&delivery_date_param, "%Y-%m-%dT%H:%M:%S")
|
||||
.map_err(|_| Error::NotFound)?;
|
||||
|
||||
fn find_admin_boxes(
|
||||
delivery_date_param: String,
|
||||
conn: &mut PgConnection,
|
||||
) -> Result<Vec<LootaBoxAdminResponse>, Error> {
|
||||
let parsed_date =
|
||||
chrono::NaiveDateTime::parse_from_str(&delivery_date_param, "%Y-%m-%dT%H:%M:%S")
|
||||
.map_err(|_| Error::NotFound)?;
|
||||
|
||||
let boxes = loota_box
|
||||
.filter(delivery_date.ge(Some(parsed_date)))
|
||||
@@ -60,27 +63,23 @@ fn find_admin_boxes(delivery_date_param: String, conn: &mut PgConnection) -> Res
|
||||
|
||||
let response = boxes
|
||||
.into_iter()
|
||||
.map(|(box_data, order)| {
|
||||
LootaBoxAdminResponse {
|
||||
id: box_data.id.to_string(),
|
||||
delivery_date: box_data.delivery_date,
|
||||
pickup_date: box_data.pickup_date,
|
||||
order: (LootaBoxOrderAdminResponse {
|
||||
id: order.id.to_string(),
|
||||
customer_id: order.customer_id.to_string(),
|
||||
location: order.location,
|
||||
created_time: order.created_time,
|
||||
})
|
||||
}
|
||||
.map(|(box_data, order)| LootaBoxAdminResponse {
|
||||
id: box_data.id.to_string(),
|
||||
delivery_date: box_data.delivery_date,
|
||||
pickup_date: box_data.pickup_date,
|
||||
order: (LootaBoxOrderAdminResponse {
|
||||
id: order.id.to_string(),
|
||||
customer_id: order.customer_id.to_string(),
|
||||
location: order.location,
|
||||
created_time: order.created_time,
|
||||
}),
|
||||
})
|
||||
.collect();
|
||||
|
||||
Ok(response)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
fn find_admin_orders(conn: &mut PgConnection) -> Result<Vec<LootaOrderAdminResponse>, Error> {
|
||||
|
||||
let orders = loota_order
|
||||
.inner_join(loota_customer)
|
||||
.select((Order::as_select(), Customer::as_select()))
|
||||
@@ -88,25 +87,21 @@ fn find_admin_orders(conn: &mut PgConnection) -> Result<Vec<LootaOrderAdminRespo
|
||||
|
||||
let response = orders
|
||||
.into_iter()
|
||||
.map(|(order, customer)| {
|
||||
LootaOrderAdminResponse {
|
||||
id: order.id.to_string(),
|
||||
location: order.location,
|
||||
created_time: order.created_time,
|
||||
user: (LootaUserAdminResponse {
|
||||
id: customer.id.to_string(),
|
||||
identifier: customer.identifier,
|
||||
created_time: customer.created_time,
|
||||
})
|
||||
}
|
||||
.map(|(order, customer)| LootaOrderAdminResponse {
|
||||
id: order.id.to_string(),
|
||||
location: order.location,
|
||||
created_time: order.created_time,
|
||||
user: (LootaUserAdminResponse {
|
||||
id: customer.id.to_string(),
|
||||
identifier: customer.identifier,
|
||||
created_time: customer.created_time,
|
||||
}),
|
||||
})
|
||||
.collect();
|
||||
|
||||
Ok(response)
|
||||
}
|
||||
|
||||
|
||||
|
||||
fn find_order(_customer: &Customer, conn: &mut PgConnection) -> Result<LootaResponse, Error> {
|
||||
let _identifier = _customer.identifier.clone();
|
||||
let order = Order::belonging_to(_customer)
|
||||
@@ -117,14 +112,13 @@ fn find_order(_customer: &Customer, conn: &mut PgConnection) -> Result<LootaResp
|
||||
match order {
|
||||
Ok(order) => match order.first() {
|
||||
Some(order) => Ok(find_boxes(_identifier, order, conn)),
|
||||
_ => Err(Error::NotFound)
|
||||
}
|
||||
_ => Err(Error::NotFound),
|
||||
},
|
||||
Err(err) => {
|
||||
println!("Error: {:?}", err);
|
||||
Err(err)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
fn find_admin(_identifier: String) -> bool {
|
||||
@@ -138,7 +132,6 @@ fn find_admin(_identifier: String) -> bool {
|
||||
admin_exists.is_ok()
|
||||
}
|
||||
|
||||
|
||||
fn find_customer(_identifier: String) -> Result<LootaResponse, Error> {
|
||||
let conn = &mut establish_connection();
|
||||
|
||||
@@ -150,17 +143,16 @@ fn find_customer(_identifier: String) -> Result<LootaResponse, Error> {
|
||||
match customer {
|
||||
Ok(customer) => match customer.first() {
|
||||
Some(customer) => Ok(find_order(customer, conn)?),
|
||||
_ => Err(Error::NotFound)
|
||||
}
|
||||
_ => Err(Error::NotFound),
|
||||
},
|
||||
Err(err) => {
|
||||
println!("Error: {:?}", err);
|
||||
Err(err)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
fn update_box(_id: Uuid, _pickup_date: NaiveDateTime) -> Result<LootaBoxResponse, Error> {
|
||||
fn update_box(_id: Uuid, _pickup_date: Option<NaiveDateTime>) -> Result<LootaBoxResponse, Error> {
|
||||
let conn = &mut establish_connection();
|
||||
|
||||
let loota = diesel::update(loota_box.filter(id.eq(&_id)))
|
||||
@@ -169,19 +161,16 @@ fn update_box(_id: Uuid, _pickup_date: NaiveDateTime) -> Result<LootaBoxResponse
|
||||
.get_result(conn);
|
||||
|
||||
match loota {
|
||||
Ok(loota) => {
|
||||
Ok(LootaBoxResponse {
|
||||
id: loota.id.to_string(),
|
||||
delivery_date: loota.delivery_date,
|
||||
pickup_date: loota.pickup_date,
|
||||
})
|
||||
}
|
||||
Ok(loota) => Ok(LootaBoxResponse {
|
||||
id: loota.id.to_string(),
|
||||
delivery_date: loota.delivery_date,
|
||||
pickup_date: loota.pickup_date,
|
||||
}),
|
||||
Err(err) => {
|
||||
println!("Error: {:?}", err);
|
||||
Err(err)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
fn parse_uuid(_id: String) -> Result<Uuid, uuid::Error> {
|
||||
@@ -199,10 +188,8 @@ async fn get_admin(path: web::Path<String>) -> impl Responder {
|
||||
let exists = web::block(move || find_admin(_identifier)).await.unwrap();
|
||||
|
||||
match exists {
|
||||
true => {
|
||||
HttpResponse::Ok()
|
||||
},
|
||||
false => HttpResponse::NotFound()
|
||||
true => HttpResponse::Ok(),
|
||||
false => HttpResponse::NotFound(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -212,7 +199,7 @@ async fn get_admin_orders() -> impl Responder {
|
||||
let conn = &mut establish_connection();
|
||||
find_admin_orders(conn)
|
||||
})
|
||||
.await;
|
||||
.await;
|
||||
|
||||
match orders {
|
||||
Ok(Ok(response)) => HttpResponse::Ok()
|
||||
@@ -230,12 +217,11 @@ async fn get_admin_orders() -> impl Responder {
|
||||
async fn get_admin_boxes(path: web::Path<String>) -> impl Responder {
|
||||
let _delivery_date = path.into_inner();
|
||||
|
||||
|
||||
let boxes = web::block(move || {
|
||||
let conn = &mut establish_connection();
|
||||
find_admin_boxes(_delivery_date, conn)
|
||||
})
|
||||
.await;
|
||||
.await;
|
||||
|
||||
match boxes {
|
||||
Ok(Ok(response)) => HttpResponse::Ok()
|
||||
@@ -246,10 +232,8 @@ async fn get_admin_boxes(path: web::Path<String>) -> impl Responder {
|
||||
.await
|
||||
.unwrap(),
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
#[get("/loota/{id}")]
|
||||
async fn get(path: web::Path<String>) -> impl Responder {
|
||||
let identifer = path.into_inner();
|
||||
@@ -257,10 +241,9 @@ async fn get(path: web::Path<String>) -> impl Responder {
|
||||
let response = web::block(move || find_customer(identifer)).await.unwrap();
|
||||
|
||||
match response {
|
||||
Ok(response) => {
|
||||
HttpResponse::Ok()
|
||||
.content_type(APPLICATION_JSON).json(response)
|
||||
}
|
||||
Ok(response) => HttpResponse::Ok()
|
||||
.content_type(APPLICATION_JSON)
|
||||
.json(response),
|
||||
_ => HttpResponse::NotFound()
|
||||
.content_type(APPLICATION_JSON)
|
||||
.await
|
||||
@@ -271,39 +254,49 @@ async fn get(path: web::Path<String>) -> impl Responder {
|
||||
#[post("/loota/")]
|
||||
async fn update(req: web::Json<LootaRequest>) -> impl Responder {
|
||||
let box_id = parse_uuid(req.id.clone());
|
||||
println!("Box ID: {:?}", box_id);
|
||||
println!("Pickup Date: {:?}", req.pickup_date);
|
||||
|
||||
match box_id {
|
||||
Ok(box_id) => {
|
||||
|
||||
let date = parse_date(req.pickup_date.clone());
|
||||
match date {
|
||||
Ok(date) => {
|
||||
|
||||
let updated_box = update_box(box_id, date);
|
||||
match updated_box {
|
||||
Ok(updated_box) => {
|
||||
HttpResponse::Ok()
|
||||
if req.pickup_date.is_none() {
|
||||
let updated_box = update_box(box_id, None);
|
||||
match updated_box {
|
||||
Ok(updated_box) => HttpResponse::Ok()
|
||||
.content_type(APPLICATION_JSON)
|
||||
.json(updated_box),
|
||||
Err(err) => {
|
||||
println!("Error: {:?}", err);
|
||||
HttpResponse::BadRequest()
|
||||
.content_type(APPLICATION_JSON)
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
} else {
|
||||
let date = parse_date(req.pickup_date.clone().unwrap());
|
||||
match date {
|
||||
Ok(date) => {
|
||||
let updated_box = update_box(box_id, Some(date));
|
||||
match updated_box {
|
||||
Ok(updated_box) => HttpResponse::Ok()
|
||||
.content_type(APPLICATION_JSON)
|
||||
.json(updated_box)
|
||||
}
|
||||
Err(err) => {
|
||||
println!("Error: {:?}", err);
|
||||
HttpResponse::BadRequest()
|
||||
.content_type(APPLICATION_JSON)
|
||||
.finish()
|
||||
.json(updated_box),
|
||||
Err(err) => {
|
||||
println!("Error: {:?}", err);
|
||||
HttpResponse::BadRequest()
|
||||
.content_type(APPLICATION_JSON)
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Err(err) => {
|
||||
println!("Error: {:?}", err);
|
||||
HttpResponse::BadRequest()
|
||||
.content_type(APPLICATION_JSON)
|
||||
.finish()
|
||||
Err(err) => {
|
||||
println!("Error: {:?}", err);
|
||||
HttpResponse::BadRequest()
|
||||
.content_type(APPLICATION_JSON)
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Err(err) => {
|
||||
println!("Error: {:?}", err);
|
||||
@@ -312,12 +305,4 @@ async fn update(req: web::Json<LootaRequest>) -> impl Responder {
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user