Add logic to unpick the box

This commit is contained in:
2025-06-17 18:18:44 +03:00
parent b570da2f38
commit 99ffc21cf0

View File

@@ -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,11 +47,13 @@ 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,
.map_err(|_| Error::NotFound)?; ) -> 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 let boxes = loota_box
.filter(delivery_date.ge(Some(parsed_date))) .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 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, order: (LootaBoxOrderAdminResponse {
order: (LootaBoxOrderAdminResponse { id: order.id.to_string(),
id: order.id.to_string(), 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,25 +87,21 @@ 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, user: (LootaUserAdminResponse {
user: (LootaUserAdminResponse { 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()
} }
} }
@@ -212,7 +199,7 @@ async fn get_admin_orders() -> impl Responder {
let conn = &mut establish_connection(); let conn = &mut establish_connection();
find_admin_orders(conn) find_admin_orders(conn)
}) })
.await; .await;
match orders { match orders {
Ok(Ok(response)) => HttpResponse::Ok() 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 { 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)
}) })
.await; .await;
match boxes { match boxes {
Ok(Ok(response)) => HttpResponse::Ok() Ok(Ok(response)) => HttpResponse::Ok()
@@ -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,39 +254,49 @@ 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 date { match updated_box {
Ok(date) => { Ok(updated_box) => HttpResponse::Ok()
.content_type(APPLICATION_JSON)
let updated_box = update_box(box_id, date); .json(updated_box),
match updated_box { Err(err) => {
Ok(updated_box) => { println!("Error: {:?}", err);
HttpResponse::Ok() 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) .content_type(APPLICATION_JSON)
.json(updated_box) .json(updated_box),
} Err(err) => {
Err(err) => { println!("Error: {:?}", err);
println!("Error: {:?}", err); HttpResponse::BadRequest()
HttpResponse::BadRequest() .content_type(APPLICATION_JSON)
.content_type(APPLICATION_JSON) .finish()
.finish() }
} }
} }
Err(err) => {
println!("Error: {:?}", err);
} HttpResponse::BadRequest()
Err(err) => { .content_type(APPLICATION_JSON)
println!("Error: {:?}", err); .finish()
HttpResponse::BadRequest() }
.content_type(APPLICATION_JSON)
.finish()
} }
} }
} }
Err(err) => { Err(err) => {
println!("Error: {:?}", err); println!("Error: {:?}", err);
@@ -312,12 +305,4 @@ async fn update(req: web::Json<LootaRequest>) -> impl Responder {
.finish() .finish()
} }
} }
}
}