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