2023-08-29 21:33:59 +02:00
|
|
|
use serde::{Deserialize, Serialize};
|
2023-05-18 00:11:52 +02:00
|
|
|
use sqlx::{
|
|
|
|
|
database::Database,
|
|
|
|
|
database::HasValueRef,
|
|
|
|
|
sqlite::{Sqlite, SqliteRow},
|
|
|
|
|
Decode, Row,
|
|
|
|
|
};
|
2023-05-08 00:05:45 +02:00
|
|
|
use std::convert;
|
|
|
|
|
use std::error;
|
|
|
|
|
use std::fmt;
|
2023-08-29 21:33:59 +02:00
|
|
|
use std::num::TryFromIntError;
|
2023-05-18 00:11:52 +02:00
|
|
|
use std::str::FromStr;
|
2023-05-08 00:05:45 +02:00
|
|
|
use uuid::Uuid;
|
|
|
|
|
|
|
|
|
|
use sqlx::sqlite::SqlitePoolOptions;
|
|
|
|
|
|
2023-05-12 00:31:08 +02:00
|
|
|
use futures::TryFutureExt;
|
2023-05-08 00:05:45 +02:00
|
|
|
use futures::TryStreamExt;
|
|
|
|
|
|
2023-08-29 21:33:59 +02:00
|
|
|
use time::{
|
|
|
|
|
error::Parse as TimeParseError, format_description::FormatItem, macros::format_description,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
pub const DATE_FORMAT: &[FormatItem<'static>] = format_description!("[year]-[month]-[day]");
|
|
|
|
|
|
2023-05-08 00:05:45 +02:00
|
|
|
pub enum Error {
|
|
|
|
|
SqlError { description: String },
|
|
|
|
|
UuidError { description: String },
|
2023-08-29 21:33:59 +02:00
|
|
|
EnumError { description: String },
|
2023-05-17 17:31:48 +02:00
|
|
|
NotFoundError { description: String },
|
2023-08-29 21:33:59 +02:00
|
|
|
IntError { description: String },
|
|
|
|
|
TimeParseError { description: String },
|
2023-05-08 00:05:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl fmt::Display for Error {
|
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
|
match self {
|
|
|
|
|
Self::SqlError { description } => {
|
2023-05-10 01:02:37 +02:00
|
|
|
write!(f, "SQL error: {description}")
|
2023-05-08 00:05:45 +02:00
|
|
|
}
|
|
|
|
|
Self::UuidError { description } => {
|
2023-05-10 01:02:37 +02:00
|
|
|
write!(f, "UUID error: {description}")
|
2023-05-08 00:05:45 +02:00
|
|
|
}
|
2023-05-17 17:31:48 +02:00
|
|
|
Self::NotFoundError { description } => {
|
|
|
|
|
write!(f, "Not found: {description}")
|
|
|
|
|
}
|
2023-08-29 21:33:59 +02:00
|
|
|
Self::IntError { description } => {
|
|
|
|
|
write!(f, "Integer error: {description}")
|
|
|
|
|
}
|
|
|
|
|
Self::EnumError { description } => {
|
|
|
|
|
write!(f, "Enum error: {description}")
|
|
|
|
|
}
|
|
|
|
|
Self::TimeParseError { description } => {
|
|
|
|
|
write!(f, "Date parse error: {description}")
|
|
|
|
|
}
|
2023-05-08 00:05:45 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl fmt::Debug for Error {
|
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
|
// defer to Display
|
2023-05-10 01:02:37 +02:00
|
|
|
write!(f, "SQL error: {self}")
|
2023-05-08 00:05:45 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl convert::From<uuid::Error> for Error {
|
|
|
|
|
fn from(value: uuid::Error) -> Self {
|
|
|
|
|
Error::UuidError {
|
|
|
|
|
description: value.to_string(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl convert::From<sqlx::Error> for Error {
|
|
|
|
|
fn from(value: sqlx::Error) -> Self {
|
|
|
|
|
Error::SqlError {
|
|
|
|
|
description: value.to_string(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-29 21:33:59 +02:00
|
|
|
impl convert::From<TryFromIntError> for Error {
|
|
|
|
|
fn from(value: TryFromIntError) -> Self {
|
|
|
|
|
Error::IntError {
|
|
|
|
|
description: value.to_string(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl convert::From<TimeParseError> for Error {
|
|
|
|
|
fn from(value: TimeParseError) -> Self {
|
|
|
|
|
Error::TimeParseError {
|
|
|
|
|
description: value.to_string(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-08 00:05:45 +02:00
|
|
|
impl error::Error for Error {}
|
|
|
|
|
|
2023-08-29 21:33:59 +02:00
|
|
|
#[derive(sqlx::Type, PartialEq, PartialOrd, Deserialize)]
|
2023-05-18 00:11:52 +02:00
|
|
|
pub enum TripState {
|
2023-08-29 21:33:59 +02:00
|
|
|
Init,
|
2023-05-18 00:11:52 +02:00
|
|
|
Planning,
|
|
|
|
|
Planned,
|
|
|
|
|
Active,
|
|
|
|
|
Review,
|
|
|
|
|
Done,
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-29 21:33:59 +02:00
|
|
|
impl TripState {
|
|
|
|
|
pub fn new() -> Self {
|
|
|
|
|
TripState::Init
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn next(&self) -> Option<Self> {
|
|
|
|
|
match self {
|
|
|
|
|
Self::Init => Some(Self::Planning),
|
|
|
|
|
Self::Planning => Some(Self::Planned),
|
|
|
|
|
Self::Planned => Some(Self::Active),
|
|
|
|
|
Self::Active => Some(Self::Review),
|
|
|
|
|
Self::Review => Some(Self::Done),
|
|
|
|
|
Self::Done => None,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn prev(&self) -> Option<Self> {
|
|
|
|
|
match self {
|
|
|
|
|
Self::Init => None,
|
|
|
|
|
Self::Planning => Some(Self::Init),
|
|
|
|
|
Self::Planned => Some(Self::Planning),
|
|
|
|
|
Self::Active => Some(Self::Planned),
|
|
|
|
|
Self::Review => Some(Self::Active),
|
|
|
|
|
Self::Done => Some(Self::Review),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn is_first(&self) -> bool {
|
|
|
|
|
self == &TripState::new()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn is_last(&self) -> bool {
|
|
|
|
|
self == &TripState::Done
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-18 00:11:52 +02:00
|
|
|
impl fmt::Display for TripState {
|
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
|
write!(
|
|
|
|
|
f,
|
|
|
|
|
"{}",
|
|
|
|
|
match self {
|
2023-08-29 21:33:59 +02:00
|
|
|
Self::Init => "Init",
|
2023-05-18 00:11:52 +02:00
|
|
|
Self::Planning => "Planning",
|
|
|
|
|
Self::Planned => "Planned",
|
|
|
|
|
Self::Active => "Active",
|
|
|
|
|
Self::Review => "Review",
|
|
|
|
|
Self::Done => "Done",
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-29 21:33:59 +02:00
|
|
|
impl std::convert::TryFrom<&str> for TripState {
|
|
|
|
|
type Error = Error;
|
|
|
|
|
|
|
|
|
|
fn try_from(value: &str) -> Result<Self, Self::Error> {
|
|
|
|
|
Ok(match value {
|
2023-08-29 21:33:59 +02:00
|
|
|
"Init" => Self::Init,
|
2023-08-29 21:33:59 +02:00
|
|
|
"Planning" => Self::Planning,
|
|
|
|
|
"Planned" => Self::Planned,
|
|
|
|
|
"Active" => Self::Active,
|
|
|
|
|
"Review" => Self::Review,
|
|
|
|
|
"Done" => Self::Done,
|
|
|
|
|
_ => {
|
|
|
|
|
return Err(Error::EnumError {
|
|
|
|
|
description: format!("{} is not a valid value for TripState", value),
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-29 21:33:59 +02:00
|
|
|
#[derive(Serialize, Debug)]
|
|
|
|
|
pub enum TripItemStateKey {
|
|
|
|
|
Pick,
|
|
|
|
|
Pack,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl fmt::Display for TripItemStateKey {
|
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
|
write!(
|
|
|
|
|
f,
|
|
|
|
|
"{}",
|
|
|
|
|
match self {
|
|
|
|
|
Self::Pick => "pick",
|
|
|
|
|
Self::Pack => "pack",
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
|
pub struct TripCategory {
|
|
|
|
|
pub category: Category,
|
|
|
|
|
pub items: Option<Vec<TripItem>>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl TripCategory {
|
2023-08-29 21:33:59 +02:00
|
|
|
pub fn total_picked_weight(&self) -> i64 {
|
2023-08-29 21:33:59 +02:00
|
|
|
self.items
|
|
|
|
|
.as_ref()
|
|
|
|
|
.unwrap()
|
|
|
|
|
.iter()
|
|
|
|
|
.filter(|item| item.picked)
|
|
|
|
|
.map(|item| item.item.weight)
|
|
|
|
|
.sum()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
|
pub struct TripItem {
|
|
|
|
|
pub item: Item,
|
|
|
|
|
pub picked: bool,
|
|
|
|
|
pub packed: bool,
|
2023-08-29 21:33:59 +02:00
|
|
|
pub new: bool,
|
2023-08-29 21:33:59 +02:00
|
|
|
}
|
|
|
|
|
|
2023-08-29 21:33:59 +02:00
|
|
|
pub struct DbTripRow {
|
|
|
|
|
pub id: String,
|
|
|
|
|
pub name: String,
|
|
|
|
|
pub date_start: String,
|
|
|
|
|
pub date_end: String,
|
|
|
|
|
pub state: String,
|
|
|
|
|
pub location: Option<String>,
|
|
|
|
|
pub temp_min: Option<i64>,
|
|
|
|
|
pub temp_max: Option<i64>,
|
|
|
|
|
pub comment: Option<String>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl TryFrom<DbTripRow> for Trip {
|
|
|
|
|
type Error = Error;
|
|
|
|
|
|
|
|
|
|
fn try_from(row: DbTripRow) -> Result<Self, Self::Error> {
|
|
|
|
|
Ok(Trip {
|
|
|
|
|
id: Uuid::try_parse(&row.id)?,
|
|
|
|
|
name: row.name,
|
|
|
|
|
date_start: time::Date::parse(&row.date_start, DATE_FORMAT)?,
|
|
|
|
|
date_end: time::Date::parse(&row.date_end, DATE_FORMAT)?,
|
|
|
|
|
state: row.state.as_str().try_into()?,
|
|
|
|
|
location: row.location,
|
|
|
|
|
temp_min: row.temp_min,
|
|
|
|
|
temp_max: row.temp_max,
|
|
|
|
|
comment: row.comment,
|
|
|
|
|
types: None,
|
|
|
|
|
categories: None,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-08 00:05:45 +02:00
|
|
|
pub struct Trip {
|
|
|
|
|
pub id: Uuid,
|
|
|
|
|
pub name: String,
|
2023-08-29 21:33:59 +02:00
|
|
|
pub date_start: time::Date,
|
|
|
|
|
pub date_end: time::Date,
|
2023-05-18 00:11:52 +02:00
|
|
|
pub state: TripState,
|
2023-08-29 21:33:59 +02:00
|
|
|
pub location: Option<String>,
|
|
|
|
|
pub temp_min: Option<i64>,
|
|
|
|
|
pub temp_max: Option<i64>,
|
2023-08-29 21:33:59 +02:00
|
|
|
pub comment: Option<String>,
|
2023-05-18 00:11:52 +02:00
|
|
|
types: Option<Vec<TripType>>,
|
2023-08-29 21:33:59 +02:00
|
|
|
categories: Option<Vec<TripCategory>>,
|
2023-05-08 00:05:45 +02:00
|
|
|
}
|
|
|
|
|
|
2023-08-29 21:33:59 +02:00
|
|
|
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
|
|
|
|
|
pub enum TripAttribute {
|
2023-08-29 21:33:59 +02:00
|
|
|
#[serde(rename = "name")]
|
|
|
|
|
Name,
|
2023-08-29 21:33:59 +02:00
|
|
|
#[serde(rename = "date_start")]
|
|
|
|
|
DateStart,
|
|
|
|
|
#[serde(rename = "date_end")]
|
|
|
|
|
DateEnd,
|
|
|
|
|
#[serde(rename = "location")]
|
|
|
|
|
Location,
|
|
|
|
|
#[serde(rename = "temp_min")]
|
|
|
|
|
TempMin,
|
|
|
|
|
#[serde(rename = "temp_max")]
|
|
|
|
|
TempMax,
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-18 00:11:52 +02:00
|
|
|
impl<'a> Trip {
|
|
|
|
|
pub fn types(&'a self) -> &Vec<TripType> {
|
|
|
|
|
self.types
|
|
|
|
|
.as_ref()
|
2023-08-29 21:33:59 +02:00
|
|
|
.expect("you need to call load_trips_types()")
|
2023-05-18 00:11:52 +02:00
|
|
|
}
|
2023-08-29 21:33:59 +02:00
|
|
|
|
|
|
|
|
pub fn categories(&'a self) -> &Vec<TripCategory> {
|
|
|
|
|
self.categories
|
|
|
|
|
.as_ref()
|
2023-08-29 21:33:59 +02:00
|
|
|
.expect("you need to call load_trips_types()")
|
2023-08-29 21:33:59 +02:00
|
|
|
}
|
2023-05-18 00:11:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'a> Trip {
|
2023-08-29 21:33:59 +02:00
|
|
|
pub fn total_picked_weight(&self) -> i64 {
|
|
|
|
|
self.categories()
|
|
|
|
|
.iter()
|
|
|
|
|
.map(|category| -> i64 {
|
|
|
|
|
category
|
|
|
|
|
.items
|
|
|
|
|
.as_ref()
|
|
|
|
|
.unwrap()
|
|
|
|
|
.iter()
|
|
|
|
|
.filter_map(|item| Some(item.item.weight).filter(|_| item.picked))
|
|
|
|
|
.sum::<i64>()
|
|
|
|
|
})
|
|
|
|
|
.sum::<i64>()
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-29 21:33:59 +02:00
|
|
|
pub async fn load_trips_types(
|
2023-05-18 00:11:52 +02:00
|
|
|
&'a mut self,
|
|
|
|
|
pool: &sqlx::Pool<sqlx::Sqlite>,
|
|
|
|
|
) -> Result<(), Error> {
|
2023-08-29 21:33:59 +02:00
|
|
|
let id = self.id.to_string();
|
|
|
|
|
let types = sqlx::query!(
|
2023-05-18 00:11:52 +02:00
|
|
|
"
|
|
|
|
|
SELECT
|
|
|
|
|
type.id as id,
|
|
|
|
|
type.name as name,
|
2023-08-29 21:33:59 +02:00
|
|
|
inner.id IS NOT NULL AS active
|
2023-08-29 21:33:59 +02:00
|
|
|
FROM trips_types AS type
|
2023-05-18 00:11:52 +02:00
|
|
|
LEFT JOIN (
|
|
|
|
|
SELECT type.id as id, type.name as name
|
|
|
|
|
FROM trips as trip
|
2023-08-29 21:33:59 +02:00
|
|
|
INNER JOIN trips_to_trips_types as ttt
|
2023-05-18 00:11:52 +02:00
|
|
|
ON ttt.trip_id = trip.id
|
2023-08-29 21:33:59 +02:00
|
|
|
INNER JOIN trips_types AS type
|
2023-05-18 00:11:52 +02:00
|
|
|
ON type.id == ttt.trip_type_id
|
|
|
|
|
WHERE trip.id = ?
|
|
|
|
|
) AS inner
|
|
|
|
|
ON inner.id = type.id
|
|
|
|
|
",
|
2023-08-29 21:33:59 +02:00
|
|
|
id
|
2023-05-18 00:11:52 +02:00
|
|
|
)
|
|
|
|
|
.fetch(pool)
|
2023-08-29 21:33:59 +02:00
|
|
|
.map_ok(|row| -> Result<TripType, Error> {
|
|
|
|
|
Ok(TripType {
|
|
|
|
|
id: Uuid::try_parse(&row.id)?,
|
|
|
|
|
name: row.name,
|
|
|
|
|
active: match row.active {
|
|
|
|
|
0 => false,
|
|
|
|
|
1 => true,
|
|
|
|
|
_ => unreachable!(),
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
})
|
2023-05-18 00:11:52 +02:00
|
|
|
.try_collect::<Vec<Result<TripType, Error>>>()
|
|
|
|
|
.await?
|
|
|
|
|
.into_iter()
|
|
|
|
|
.collect::<Result<Vec<TripType>, Error>>()?;
|
|
|
|
|
|
|
|
|
|
self.types = Some(types);
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
2023-08-29 21:33:59 +02:00
|
|
|
|
2023-08-29 21:33:59 +02:00
|
|
|
pub async fn sync_trip_items_with_inventory(
|
|
|
|
|
&'a mut self,
|
|
|
|
|
pool: &sqlx::Pool<sqlx::Sqlite>,
|
|
|
|
|
) -> Result<(), Error> {
|
|
|
|
|
// we need to get all items that are part of the inventory but not
|
|
|
|
|
// part of the trip items
|
|
|
|
|
//
|
|
|
|
|
// then, we know which items we need to sync. there are different
|
|
|
|
|
// states for them:
|
|
|
|
|
//
|
|
|
|
|
// * if the trip is new (it's state is INITIAL), we can just forward
|
|
|
|
|
// as-is
|
|
|
|
|
// * if the trip is new, we have to make these new items prominently
|
|
|
|
|
// visible so the user knows that there might be new items to
|
|
|
|
|
// consider
|
|
|
|
|
let trip_id = self.id.to_string();
|
|
|
|
|
let unsynced_items: Vec<Uuid> = sqlx::query!(
|
|
|
|
|
"
|
|
|
|
|
SELECT
|
|
|
|
|
i_item.id AS item_id
|
|
|
|
|
FROM inventory_items AS i_item
|
|
|
|
|
LEFT JOIN (
|
|
|
|
|
SELECT t_item.item_id as item_id
|
|
|
|
|
FROM trips_items AS t_item
|
|
|
|
|
WHERE t_item.trip_id = ?
|
|
|
|
|
) AS t_item
|
|
|
|
|
ON t_item.item_id = i_item.id
|
|
|
|
|
WHERE t_item.item_id IS NULL
|
|
|
|
|
",
|
|
|
|
|
trip_id
|
|
|
|
|
)
|
|
|
|
|
.fetch(pool)
|
|
|
|
|
.map_ok(|row| -> Result<Uuid, Error> { Ok(Uuid::try_parse(&row.item_id)?) })
|
|
|
|
|
.try_collect::<Vec<Result<Uuid, Error>>>()
|
|
|
|
|
.await?
|
|
|
|
|
.into_iter()
|
|
|
|
|
.collect::<Result<Vec<Uuid>, Error>>()?;
|
|
|
|
|
|
|
|
|
|
// looks like there is currently no nice way to do multiple inserts
|
|
|
|
|
// with sqlx. whatever, this won't matter
|
|
|
|
|
|
|
|
|
|
// only mark as new when the trip is already underway
|
|
|
|
|
let mark_as_new = self.state != TripState::new();
|
|
|
|
|
|
|
|
|
|
for unsynced_item in &unsynced_items {
|
|
|
|
|
let item_id = unsynced_item.to_string();
|
|
|
|
|
sqlx::query!(
|
|
|
|
|
"
|
|
|
|
|
INSERT INTO trips_items
|
|
|
|
|
(
|
|
|
|
|
item_id,
|
|
|
|
|
trip_id,
|
|
|
|
|
pick,
|
|
|
|
|
pack,
|
|
|
|
|
new
|
|
|
|
|
)
|
|
|
|
|
VALUES (?, ?, ?, ?, ?)
|
|
|
|
|
",
|
|
|
|
|
item_id,
|
|
|
|
|
trip_id,
|
|
|
|
|
false,
|
|
|
|
|
false,
|
|
|
|
|
mark_as_new,
|
|
|
|
|
)
|
|
|
|
|
.execute(pool)
|
|
|
|
|
.await?;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tracing::info!("unsynced items: {:?}", &unsynced_items);
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-29 21:33:59 +02:00
|
|
|
pub async fn load_categories(
|
|
|
|
|
&'a mut self,
|
|
|
|
|
pool: &sqlx::Pool<sqlx::Sqlite>,
|
|
|
|
|
) -> Result<(), Error> {
|
|
|
|
|
let mut categories: Vec<TripCategory> = vec![];
|
|
|
|
|
// we can ignore the return type as we collect into `categories`
|
|
|
|
|
// in the `map_ok()` closure
|
2023-08-29 21:33:59 +02:00
|
|
|
let id = self.id.to_string();
|
|
|
|
|
sqlx::query!(
|
2023-08-29 21:33:59 +02:00
|
|
|
"
|
|
|
|
|
SELECT
|
|
|
|
|
category.id as category_id,
|
|
|
|
|
category.name as category_name,
|
2023-08-29 21:33:59 +02:00
|
|
|
category.description AS category_description,
|
|
|
|
|
inner.trip_id AS trip_id,
|
|
|
|
|
inner.item_id AS item_id,
|
|
|
|
|
inner.item_name AS item_name,
|
|
|
|
|
inner.item_description AS item_description,
|
|
|
|
|
inner.item_weight AS item_weight,
|
|
|
|
|
inner.item_is_picked AS item_is_picked,
|
2023-08-29 21:33:59 +02:00
|
|
|
inner.item_is_packed AS item_is_packed,
|
|
|
|
|
inner.item_is_new AS item_is_new
|
2023-08-29 21:33:59 +02:00
|
|
|
FROM inventory_items_categories AS category
|
|
|
|
|
LEFT JOIN (
|
|
|
|
|
SELECT
|
|
|
|
|
trip.trip_id AS trip_id,
|
|
|
|
|
category.id as category_id,
|
|
|
|
|
category.name as category_name,
|
|
|
|
|
category.description as category_description,
|
|
|
|
|
item.id as item_id,
|
|
|
|
|
item.name as item_name,
|
|
|
|
|
item.description as item_description,
|
|
|
|
|
item.weight as item_weight,
|
|
|
|
|
trip.pick as item_is_picked,
|
2023-08-29 21:33:59 +02:00
|
|
|
trip.pack as item_is_packed,
|
|
|
|
|
trip.new as item_is_new
|
2023-08-29 21:33:59 +02:00
|
|
|
FROM trips_items as trip
|
|
|
|
|
INNER JOIN inventory_items as item
|
|
|
|
|
ON item.id = trip.item_id
|
|
|
|
|
INNER JOIN inventory_items_categories as category
|
|
|
|
|
ON category.id = item.category_id
|
2023-08-29 21:33:59 +02:00
|
|
|
WHERE trip.trip_id = ?
|
2023-08-29 21:33:59 +02:00
|
|
|
) AS inner
|
|
|
|
|
ON inner.category_id = category.id
|
2023-08-29 21:33:59 +02:00
|
|
|
",
|
2023-08-29 21:33:59 +02:00
|
|
|
id
|
2023-08-29 21:33:59 +02:00
|
|
|
)
|
|
|
|
|
.fetch(pool)
|
|
|
|
|
.map_ok(|row| -> Result<(), Error> {
|
|
|
|
|
let mut category = TripCategory {
|
|
|
|
|
category: Category {
|
2023-08-29 21:33:59 +02:00
|
|
|
id: Uuid::try_parse(&row.category_id)?,
|
|
|
|
|
name: row.category_name,
|
|
|
|
|
description: row.category_description,
|
|
|
|
|
|
2023-08-29 21:33:59 +02:00
|
|
|
items: None,
|
|
|
|
|
},
|
|
|
|
|
items: None,
|
|
|
|
|
};
|
|
|
|
|
|
2023-08-29 21:33:59 +02:00
|
|
|
match row.item_id {
|
2023-08-29 21:33:59 +02:00
|
|
|
None => {
|
|
|
|
|
// we have an empty (unused) category which has NULL values
|
|
|
|
|
// for the item_id column
|
|
|
|
|
category.items = Some(vec![]);
|
|
|
|
|
categories.push(category);
|
|
|
|
|
}
|
|
|
|
|
Some(item_id) => {
|
|
|
|
|
let item = TripItem {
|
|
|
|
|
item: Item {
|
2023-08-29 21:33:59 +02:00
|
|
|
id: Uuid::try_parse(&item_id)?,
|
|
|
|
|
name: row.item_name.unwrap(),
|
|
|
|
|
description: row.item_description,
|
|
|
|
|
weight: row.item_weight.unwrap(),
|
2023-08-29 21:33:59 +02:00
|
|
|
category_id: category.category.id,
|
|
|
|
|
},
|
2023-08-29 21:33:59 +02:00
|
|
|
picked: row.item_is_picked.unwrap(),
|
|
|
|
|
packed: row.item_is_packed.unwrap(),
|
2023-08-29 21:33:59 +02:00
|
|
|
new: row.item_is_new.unwrap(),
|
2023-08-29 21:33:59 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if let Some(&mut ref mut c) = categories
|
|
|
|
|
.iter_mut()
|
|
|
|
|
.find(|c| c.category.id == category.category.id)
|
|
|
|
|
{
|
|
|
|
|
// we always populate c.items when we add a new category, so
|
|
|
|
|
// it's safe to unwrap here
|
|
|
|
|
c.items.as_mut().unwrap().push(item);
|
|
|
|
|
} else {
|
|
|
|
|
category.items = Some(vec![item]);
|
|
|
|
|
categories.push(category);
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-08-29 21:33:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
})
|
|
|
|
|
.try_collect::<Vec<Result<(), Error>>>()
|
|
|
|
|
.await?
|
|
|
|
|
.into_iter()
|
|
|
|
|
.collect::<Result<(), Error>>()?;
|
|
|
|
|
|
|
|
|
|
self.categories = Some(categories);
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
2023-05-18 00:11:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub struct TripType {
|
|
|
|
|
pub id: Uuid,
|
|
|
|
|
pub name: String,
|
|
|
|
|
pub active: bool,
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-29 21:33:59 +02:00
|
|
|
pub struct DbCategoryRow {
|
|
|
|
|
pub id: String,
|
|
|
|
|
pub name: String,
|
|
|
|
|
pub description: Option<String>,
|
2023-05-18 00:11:52 +02:00
|
|
|
}
|
|
|
|
|
|
2023-05-17 17:31:48 +02:00
|
|
|
#[derive(Debug)]
|
2023-05-08 00:05:45 +02:00
|
|
|
pub struct Category {
|
|
|
|
|
pub id: Uuid,
|
|
|
|
|
pub name: String,
|
2023-08-29 21:33:59 +02:00
|
|
|
pub description: Option<String>,
|
2023-05-08 00:05:45 +02:00
|
|
|
items: Option<Vec<Item>>,
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-29 21:33:59 +02:00
|
|
|
impl TryFrom<DbCategoryRow> for Category {
|
2023-05-08 00:05:45 +02:00
|
|
|
type Error = Error;
|
|
|
|
|
|
2023-08-29 21:33:59 +02:00
|
|
|
fn try_from(row: DbCategoryRow) -> Result<Self, Self::Error> {
|
2023-05-08 00:05:45 +02:00
|
|
|
Ok(Category {
|
2023-08-29 21:33:59 +02:00
|
|
|
id: Uuid::try_parse(&row.id)?,
|
|
|
|
|
name: row.name,
|
|
|
|
|
description: row.description,
|
2023-05-08 00:05:45 +02:00
|
|
|
items: None,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-29 21:33:59 +02:00
|
|
|
pub struct DbInventoryItemsRow {
|
|
|
|
|
id: String,
|
|
|
|
|
name: String,
|
|
|
|
|
weight: i64,
|
|
|
|
|
description: Option<String>,
|
|
|
|
|
category_id: String,
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-08 00:05:45 +02:00
|
|
|
impl<'a> Category {
|
|
|
|
|
pub fn items(&'a self) -> &'a Vec<Item> {
|
|
|
|
|
self.items
|
|
|
|
|
.as_ref()
|
|
|
|
|
.expect("you need to call populate_items()")
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-29 21:33:59 +02:00
|
|
|
pub fn total_weight(&self) -> i64 {
|
2023-05-08 00:05:45 +02:00
|
|
|
self.items().iter().map(|item| item.weight).sum()
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-11 16:51:57 +02:00
|
|
|
pub async fn populate_items(
|
|
|
|
|
&'a mut self,
|
|
|
|
|
pool: &sqlx::Pool<sqlx::Sqlite>,
|
|
|
|
|
) -> Result<(), Error> {
|
2023-08-29 21:33:59 +02:00
|
|
|
let id = self.id.to_string();
|
|
|
|
|
let items = sqlx::query_as!(
|
|
|
|
|
DbInventoryItemsRow,
|
2023-05-08 00:05:45 +02:00
|
|
|
"SELECT
|
2023-08-29 21:33:59 +02:00
|
|
|
id,
|
|
|
|
|
name,
|
|
|
|
|
weight,
|
|
|
|
|
description,
|
|
|
|
|
category_id
|
2023-08-29 21:33:59 +02:00
|
|
|
FROM inventory_items
|
2023-08-29 21:33:59 +02:00
|
|
|
WHERE category_id = ?",
|
|
|
|
|
id
|
|
|
|
|
)
|
2023-05-11 16:51:57 +02:00
|
|
|
.fetch(pool)
|
2023-08-29 21:33:59 +02:00
|
|
|
.map_ok(|row| row.try_into())
|
2023-05-08 00:05:45 +02:00
|
|
|
.try_collect::<Vec<Result<Item, Error>>>()
|
|
|
|
|
.await?
|
|
|
|
|
.into_iter()
|
|
|
|
|
.collect::<Result<Vec<Item>, Error>>()?;
|
|
|
|
|
|
|
|
|
|
self.items = Some(items);
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-17 17:31:48 +02:00
|
|
|
#[derive(Debug)]
|
2023-05-08 00:05:45 +02:00
|
|
|
pub struct Item {
|
|
|
|
|
pub id: Uuid,
|
|
|
|
|
pub name: String,
|
2023-08-29 21:33:59 +02:00
|
|
|
pub description: Option<String>,
|
|
|
|
|
pub weight: i64,
|
2023-05-08 00:05:45 +02:00
|
|
|
pub category_id: Uuid,
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-29 21:33:59 +02:00
|
|
|
impl TryFrom<DbInventoryItemsRow> for Item {
|
2023-05-08 00:05:45 +02:00
|
|
|
type Error = Error;
|
|
|
|
|
|
2023-08-29 21:33:59 +02:00
|
|
|
fn try_from(row: DbInventoryItemsRow) -> Result<Self, Self::Error> {
|
2023-05-08 00:05:45 +02:00
|
|
|
Ok(Item {
|
2023-08-29 21:33:59 +02:00
|
|
|
id: Uuid::try_parse(&row.id)?,
|
|
|
|
|
name: row.name,
|
|
|
|
|
description: row.description, // TODO
|
|
|
|
|
weight: row.weight,
|
|
|
|
|
category_id: Uuid::try_parse(&row.category_id)?,
|
2023-05-08 00:05:45 +02:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-05-12 00:31:08 +02:00
|
|
|
|
|
|
|
|
impl Item {
|
|
|
|
|
pub async fn find(pool: &sqlx::Pool<sqlx::Sqlite>, id: Uuid) -> Result<Option<Item>, Error> {
|
2023-08-29 21:33:59 +02:00
|
|
|
let id_param = id.to_string();
|
|
|
|
|
let item: Result<Result<Item, Error>, sqlx::Error> = sqlx::query_as!(
|
|
|
|
|
DbInventoryItemsRow,
|
2023-08-29 21:33:59 +02:00
|
|
|
"SELECT * FROM inventory_items AS item
|
2023-05-12 00:31:08 +02:00
|
|
|
WHERE item.id = ?",
|
2023-08-29 21:33:59 +02:00
|
|
|
id_param,
|
2023-05-12 00:31:08 +02:00
|
|
|
)
|
|
|
|
|
.fetch_one(pool)
|
2023-08-29 21:33:59 +02:00
|
|
|
.map_ok(|row| row.try_into())
|
2023-05-12 00:31:08 +02:00
|
|
|
.await;
|
|
|
|
|
|
|
|
|
|
match item {
|
|
|
|
|
Err(e) => match e {
|
|
|
|
|
sqlx::Error::RowNotFound => Ok(None),
|
|
|
|
|
_ => Err(e.into()),
|
|
|
|
|
},
|
|
|
|
|
Ok(v) => Ok(Some(v?)),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub async fn update(
|
|
|
|
|
pool: &sqlx::Pool<sqlx::Sqlite>,
|
|
|
|
|
id: Uuid,
|
|
|
|
|
name: &str,
|
2023-08-29 21:33:59 +02:00
|
|
|
weight: i64,
|
2023-05-12 00:31:08 +02:00
|
|
|
) -> Result<Option<Uuid>, Error> {
|
2023-08-29 21:33:59 +02:00
|
|
|
let id_param = id.to_string();
|
|
|
|
|
let id: Result<Result<Uuid, Error>, sqlx::Error> = sqlx::query!(
|
2023-08-29 21:33:59 +02:00
|
|
|
"UPDATE inventory_items AS item
|
2023-05-12 00:31:08 +02:00
|
|
|
SET
|
|
|
|
|
name = ?,
|
|
|
|
|
weight = ?
|
|
|
|
|
WHERE item.id = ?
|
2023-08-29 21:33:59 +02:00
|
|
|
RETURNING inventory_items.category_id AS id
|
2023-05-12 00:31:08 +02:00
|
|
|
",
|
2023-08-29 21:33:59 +02:00
|
|
|
name,
|
|
|
|
|
weight,
|
|
|
|
|
id_param,
|
2023-05-12 00:31:08 +02:00
|
|
|
)
|
|
|
|
|
.fetch_one(pool)
|
|
|
|
|
.map_ok(|row| {
|
2023-08-29 21:33:59 +02:00
|
|
|
let id: &str = &row.id.unwrap(); // TODO
|
2023-05-12 00:31:08 +02:00
|
|
|
let uuid: Result<Uuid, uuid::Error> = Uuid::try_parse(id);
|
|
|
|
|
let uuid: Result<Uuid, Error> = uuid.map_err(|e| e.into());
|
|
|
|
|
uuid
|
|
|
|
|
})
|
|
|
|
|
.await;
|
|
|
|
|
|
|
|
|
|
match id {
|
|
|
|
|
Err(e) => match e {
|
|
|
|
|
sqlx::Error::RowNotFound => Ok(None),
|
|
|
|
|
_ => Err(e.into()),
|
|
|
|
|
},
|
|
|
|
|
Ok(v) => Ok(Some(v?)),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-08-29 21:33:59 +02:00
|
|
|
|
|
|
|
|
pub struct DbTripsTypesRow {
|
|
|
|
|
pub id: String,
|
|
|
|
|
pub name: String,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
|
|
|
|
|
pub enum TripTypeAttribute {
|
|
|
|
|
#[serde(rename = "name")]
|
|
|
|
|
Name,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub struct TripsType {
|
|
|
|
|
pub id: Uuid,
|
|
|
|
|
pub name: String,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl TryFrom<DbTripsTypesRow> for TripsType {
|
|
|
|
|
type Error = Error;
|
|
|
|
|
|
|
|
|
|
fn try_from(row: DbTripsTypesRow) -> Result<Self, Self::Error> {
|
|
|
|
|
Ok(TripsType {
|
|
|
|
|
id: Uuid::try_parse(&row.id)?,
|
|
|
|
|
name: row.name,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|