schema stuff works
This commit is contained in:
@@ -37,7 +37,7 @@ pub struct InventoryCategoryList;
|
||||
|
||||
impl InventoryCategoryList {
|
||||
pub fn build(state: &ClientState, categories: &Vec<Category>) -> Markup {
|
||||
let biggest_category_weight: u32 = categories
|
||||
let biggest_category_weight: i64 = categories
|
||||
.iter()
|
||||
.map(Category::total_weight)
|
||||
.max()
|
||||
@@ -115,8 +115,8 @@ impl InventoryCategoryList {
|
||||
format!(
|
||||
"width: {width}%;position:absolute;left:0;bottom:0;right:0;",
|
||||
width=(
|
||||
f64::from(category.total_weight())
|
||||
/ f64::from(biggest_category_weight)
|
||||
(category.total_weight() as f64)
|
||||
/ (biggest_category_weight as f64)
|
||||
* 100.0
|
||||
)
|
||||
)
|
||||
@@ -130,7 +130,7 @@ impl InventoryCategoryList {
|
||||
}
|
||||
td ."border" ."p-0" ."m-0" {
|
||||
p ."p-2" ."m-2" {
|
||||
(categories.iter().map(Category::total_weight).sum::<u32>().to_string())
|
||||
(categories.iter().map(Category::total_weight).sum::<i64>().to_string())
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -145,7 +145,7 @@ pub struct InventoryItemList;
|
||||
|
||||
impl InventoryItemList {
|
||||
pub fn build(state: &ClientState, items: &Vec<Item>) -> Markup {
|
||||
let biggest_item_weight: u32 = items.iter().map(|item| item.weight).max().unwrap_or(1);
|
||||
let biggest_item_weight: i64 = items.iter().map(|item| item.weight).max().unwrap_or(1);
|
||||
html!(
|
||||
div #items {
|
||||
@if items.is_empty() {
|
||||
@@ -267,7 +267,7 @@ impl InventoryItemList {
|
||||
position:absolute;
|
||||
left:0;
|
||||
bottom:0;
|
||||
right:0;", width=(f64::from(item.weight) / f64::from(biggest_item_weight) * 100.0))) {}
|
||||
right:0;", width=((item.weight as f64) / (biggest_item_weight as f64) * 100.0))) {}
|
||||
}
|
||||
td
|
||||
."border-none"
|
||||
|
||||
@@ -29,7 +29,9 @@ impl Root {
|
||||
link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@mdi/font@6.9.96/css/materialdesignicons.min.css";
|
||||
script { (include_str!(concat!(env!("CARGO_MANIFEST_DIR"),"/js/app.js"))) }
|
||||
}
|
||||
body hx-boost="true" {
|
||||
body
|
||||
hx-boost="true"
|
||||
{
|
||||
header
|
||||
."bg-gray-200"
|
||||
."p-5"
|
||||
|
||||
@@ -276,7 +276,7 @@ pub struct TripInfoRow;
|
||||
impl TripInfoRow {
|
||||
pub fn build(
|
||||
name: &str,
|
||||
value: impl std::fmt::Display,
|
||||
value: Option<impl std::fmt::Display>,
|
||||
attribute_key: TripAttribute,
|
||||
edit_attribute: Option<&TripAttribute>,
|
||||
input_type: InputType,
|
||||
@@ -303,7 +303,7 @@ impl TripInfoRow {
|
||||
id="new-value"
|
||||
name="new-value"
|
||||
form="edit-trip"
|
||||
value=(value)
|
||||
value=(value.map_or("".to_string(), |v| v.to_string()))
|
||||
;
|
||||
}
|
||||
}
|
||||
@@ -355,7 +355,7 @@ impl TripInfoRow {
|
||||
}
|
||||
} @else {
|
||||
td ."border" ."p-2" { (name) }
|
||||
td ."border" ."p-2" { (value) }
|
||||
td ."border" ."p-2" { (value.map_or("".to_string(), |v| v.to_string())) }
|
||||
td
|
||||
."border-none"
|
||||
."bg-blue-100"
|
||||
@@ -397,9 +397,9 @@ impl TripInfo {
|
||||
."w-full"
|
||||
{
|
||||
tbody {
|
||||
(TripInfoRow::build("Location", &trip.location, TripAttribute::Location, state.trip_edit_attribute.as_ref(), InputType::Text))
|
||||
(TripInfoRow::build("Start date", trip.date_start, TripAttribute::DateStart, state.trip_edit_attribute.as_ref(), InputType::Date))
|
||||
(TripInfoRow::build("End date", trip.date_end, TripAttribute::DateEnd, state.trip_edit_attribute.as_ref(), InputType::Date))
|
||||
(TripInfoRow::build("Location", trip.location.as_ref(), TripAttribute::Location, state.trip_edit_attribute.as_ref(), InputType::Text))
|
||||
(TripInfoRow::build("Start date", Some(trip.date_start), TripAttribute::DateStart, state.trip_edit_attribute.as_ref(), InputType::Date))
|
||||
(TripInfoRow::build("End date", Some(trip.date_end), TripAttribute::DateEnd, state.trip_edit_attribute.as_ref(), InputType::Date))
|
||||
(TripInfoRow::build("Temp (min)", trip.temp_min, TripAttribute::TempMin, state.trip_edit_attribute.as_ref(), InputType::Number))
|
||||
(TripInfoRow::build("Temp (max)", trip.temp_max, TripAttribute::TempMax, state.trip_edit_attribute.as_ref(), InputType::Number))
|
||||
tr .h-full {
|
||||
@@ -587,7 +587,7 @@ impl TripCategoryList {
|
||||
pub fn build(state: &ClientState, trip: &models::Trip) -> Markup {
|
||||
let categories = trip.categories();
|
||||
|
||||
let biggest_category_weight: u32 = categories
|
||||
let biggest_category_weight: i64 = categories
|
||||
.iter()
|
||||
.map(TripCategory::total_picked_weight)
|
||||
.max()
|
||||
@@ -655,8 +655,8 @@ impl TripCategoryList {
|
||||
format!(
|
||||
"width: {width}%;position:absolute;left:0;bottom:0;right:0;",
|
||||
width=(
|
||||
f64::from(category.total_picked_weight())
|
||||
/ f64::from(biggest_category_weight)
|
||||
(category.total_picked_weight() as f64)
|
||||
/ (biggest_category_weight as f64)
|
||||
* 100.0
|
||||
)
|
||||
)
|
||||
@@ -670,7 +670,7 @@ impl TripCategoryList {
|
||||
}
|
||||
td ."border" ."p-0" ."m-0" {
|
||||
p ."p-2" ."m-2" {
|
||||
(categories.iter().map(TripCategory::total_picked_weight).sum::<u32>().to_string())
|
||||
(categories.iter().map(TripCategory::total_picked_weight).sum::<i64>().to_string())
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -684,7 +684,7 @@ pub struct TripItemList;
|
||||
|
||||
impl TripItemList {
|
||||
pub fn build(state: &ClientState, trip: &models::Trip, items: &Vec<TripItem>) -> Markup {
|
||||
let biggest_item_weight: u32 = items.iter().map(|item| item.item.weight).max().unwrap_or(1);
|
||||
let biggest_item_weight: i64 = items.iter().map(|item| item.item.weight).max().unwrap_or(1);
|
||||
|
||||
html!(
|
||||
@if items.is_empty() {
|
||||
@@ -778,7 +778,7 @@ impl TripItemList {
|
||||
position:absolute;
|
||||
left:0;
|
||||
bottom:0;
|
||||
right:0;", width=(f64::from(item.item.weight) / f64::from(biggest_item_weight) * 100.0))) {}
|
||||
right:0;", width=((item.item.weight as f64) / (biggest_item_weight as f64) * 100.0))) {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
276
rust/src/main.rs
276
rust/src/main.rs
@@ -15,8 +15,8 @@ use serde_variant::to_variant_name;
|
||||
|
||||
use sqlx::{
|
||||
error::DatabaseError,
|
||||
query,
|
||||
sqlite::{SqliteConnectOptions, SqliteError, SqlitePoolOptions},
|
||||
query, query_as,
|
||||
sqlite::{SqliteConnectOptions, SqliteError, SqlitePoolOptions, SqliteRow},
|
||||
Pool, Row, Sqlite,
|
||||
};
|
||||
|
||||
@@ -170,29 +170,32 @@ async fn inventory(
|
||||
) -> Result<(StatusCode, Markup), (StatusCode, Markup)> {
|
||||
state.client_state.active_category_id = active_id;
|
||||
|
||||
let mut categories = query("SELECT id,name,description FROM inventory_items_categories")
|
||||
.fetch(&state.database_pool)
|
||||
.map_ok(std::convert::TryInto::try_into)
|
||||
.try_collect::<Vec<Result<Category, models::Error>>>()
|
||||
.await
|
||||
// we have two error handling lines here. these are distinct errors
|
||||
// this one is the SQL error that may arise during the query
|
||||
.map_err(|e| {
|
||||
(
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
ErrorPage::build(&e.to_string()),
|
||||
)
|
||||
})?
|
||||
.into_iter()
|
||||
.collect::<Result<Vec<Category>, models::Error>>()
|
||||
// and this one is the model mapping error that may arise e.g. during
|
||||
// reading of the rows
|
||||
.map_err(|e| {
|
||||
(
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
ErrorPage::build(&e.to_string()),
|
||||
)
|
||||
})?;
|
||||
let mut categories = query_as!(
|
||||
DbCategoryRow,
|
||||
"SELECT id,name,description FROM inventory_items_categories"
|
||||
)
|
||||
.fetch(&state.database_pool)
|
||||
.map_ok(|row: DbCategoryRow| row.try_into())
|
||||
.try_collect::<Vec<Result<Category, models::Error>>>()
|
||||
.await
|
||||
// we have two error handling lines here. these are distinct errors
|
||||
// this one is the SQL error that may arise during the query
|
||||
.map_err(|e| {
|
||||
(
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
ErrorPage::build(&e.to_string()),
|
||||
)
|
||||
})?
|
||||
.into_iter()
|
||||
.collect::<Result<Vec<Category>, models::Error>>()
|
||||
// and this one is the model mapping error that may arise e.g. during
|
||||
// reading of the rows
|
||||
.map_err(|e| {
|
||||
(
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
ErrorPage::build(&e.to_string()),
|
||||
)
|
||||
})?;
|
||||
|
||||
for category in &mut categories {
|
||||
category
|
||||
@@ -241,17 +244,21 @@ async fn inventory_item_create(
|
||||
State(state): State<AppState>,
|
||||
Form(new_item): Form<NewItem>,
|
||||
) -> Result<Redirect, (StatusCode, String)> {
|
||||
query(
|
||||
let id = Uuid::new_v4();
|
||||
let id_param = id.to_string();
|
||||
let name = &new_item.name;
|
||||
let category_id = new_item.category_id.to_string();
|
||||
query!(
|
||||
"INSERT INTO inventory_items
|
||||
(id, name, description, weight, category_id)
|
||||
VALUES
|
||||
(?, ?, ?, ?, ?)",
|
||||
id_param,
|
||||
name,
|
||||
"",
|
||||
new_item.weight,
|
||||
category_id,
|
||||
)
|
||||
.bind(Uuid::new_v4().to_string())
|
||||
.bind(&new_item.name)
|
||||
.bind("")
|
||||
.bind(new_item.weight)
|
||||
.bind(new_item.category_id.to_string())
|
||||
.execute(&state.database_pool)
|
||||
.await
|
||||
.map_err(|e| match e {
|
||||
@@ -306,11 +313,12 @@ async fn inventory_item_delete(
|
||||
headers: HeaderMap,
|
||||
Path(id): Path<Uuid>,
|
||||
) -> Result<Redirect, (StatusCode, String)> {
|
||||
let results = query(
|
||||
let id_param = id.to_string();
|
||||
let results = query!(
|
||||
"DELETE FROM inventory_items
|
||||
WHERE id = ?",
|
||||
id_param,
|
||||
)
|
||||
.bind(id.to_string())
|
||||
.execute(&state.database_pool)
|
||||
.await
|
||||
.map_err(|e| (StatusCode::BAD_REQUEST, e.to_string()))?;
|
||||
@@ -348,7 +356,7 @@ async fn inventory_item_delete(
|
||||
// .await
|
||||
// .unwrap();
|
||||
|
||||
// let items = query(&format!(
|
||||
// let items = query!(&format!(
|
||||
// //TODO bind this stuff!!!!!!! no sql injection pls
|
||||
// "SELECT
|
||||
// i.id, i.name, i.description, i.weight, i.category_id
|
||||
@@ -392,14 +400,29 @@ async fn inventory_item_edit(
|
||||
State(state): State<AppState>,
|
||||
Path(id): Path<Uuid>,
|
||||
Form(edit_item): Form<EditItem>,
|
||||
) -> Result<Redirect, (StatusCode, String)> {
|
||||
let id = Item::update(&state.database_pool, id, &edit_item.name, edit_item.weight)
|
||||
.await
|
||||
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?
|
||||
.ok_or((
|
||||
StatusCode::NOT_FOUND,
|
||||
format!("item with id {id} not found", id = id),
|
||||
))?;
|
||||
) -> Result<Redirect, (StatusCode, Markup)> {
|
||||
let id = Item::update(
|
||||
&state.database_pool,
|
||||
id,
|
||||
&edit_item.name,
|
||||
i64::try_from(edit_item.weight).map_err(|e| {
|
||||
(
|
||||
StatusCode::UNPROCESSABLE_ENTITY,
|
||||
ErrorPage::build(&e.to_string()),
|
||||
)
|
||||
})?,
|
||||
)
|
||||
.await
|
||||
.map_err(|e| {
|
||||
(
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
ErrorPage::build(&e.to_string()),
|
||||
)
|
||||
})?
|
||||
.ok_or((
|
||||
StatusCode::NOT_FOUND,
|
||||
ErrorPage::build(&format!("item with id {id} not found", id = id)),
|
||||
))?;
|
||||
|
||||
Ok(Redirect::to(&format!("/inventory/category/{id}/", id = id)))
|
||||
}
|
||||
@@ -437,16 +460,26 @@ async fn trip_create(
|
||||
Form(new_trip): Form<NewTrip>,
|
||||
) -> Result<Redirect, (StatusCode, String)> {
|
||||
let id = Uuid::new_v4();
|
||||
query(
|
||||
let id_param = id.to_string();
|
||||
let date_start = new_trip
|
||||
.date_start
|
||||
.format(DATE_FORMAT)
|
||||
.map_err(|e| (StatusCode::BAD_REQUEST, e.to_string()))?;
|
||||
let date_end = new_trip
|
||||
.date_end
|
||||
.format(DATE_FORMAT)
|
||||
.map_err(|e| (StatusCode::BAD_REQUEST, e.to_string()))?;
|
||||
query!(
|
||||
"INSERT INTO trips
|
||||
(id, name, date_start, date_end)
|
||||
(id, name, date_start, date_end, state)
|
||||
VALUES
|
||||
(?, ?, ?, ?)",
|
||||
(?, ?, ?, ?, ?)",
|
||||
id_param,
|
||||
new_trip.name,
|
||||
date_start,
|
||||
date_end,
|
||||
TripState::Planning,
|
||||
)
|
||||
.bind(id.to_string())
|
||||
.bind(&new_trip.name)
|
||||
.bind(new_trip.date_start)
|
||||
.bind(new_trip.date_end)
|
||||
.execute(&state.database_pool)
|
||||
.await
|
||||
.map_err(|e| match e {
|
||||
@@ -482,35 +515,52 @@ async fn trip_create(
|
||||
),
|
||||
})?;
|
||||
|
||||
Ok(Redirect::to(&format!("/trip/{id}/", id = id.to_string())))
|
||||
Ok(Redirect::to(&format!("/trip/{id}/", id = id)))
|
||||
}
|
||||
|
||||
async fn trips(
|
||||
State(state): State<AppState>,
|
||||
) -> Result<(StatusCode, Markup), (StatusCode, Markup)> {
|
||||
let trips: Vec<models::Trip> = query("SELECT * FROM trips")
|
||||
.fetch(&state.database_pool)
|
||||
.map_ok(std::convert::TryInto::try_into)
|
||||
.try_collect::<Vec<Result<models::Trip, models::Error>>>()
|
||||
.await
|
||||
// we have two error handling lines here. these are distinct errors
|
||||
// this one is the SQL error that may arise during the query
|
||||
.map_err(|e| {
|
||||
(
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
ErrorPage::build(&e.to_string()),
|
||||
)
|
||||
})?
|
||||
.into_iter()
|
||||
.collect::<Result<Vec<models::Trip>, models::Error>>()
|
||||
// and this one is the model mapping error that may arise e.g. during
|
||||
// reading of the rows
|
||||
.map_err(|e| {
|
||||
(
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
ErrorPage::build(&e.to_string()),
|
||||
)
|
||||
})?;
|
||||
tracing::info!("receiving trips");
|
||||
|
||||
let trips: Vec<models::Trip> = query_as!(
|
||||
DbTripRow,
|
||||
"SELECT
|
||||
id,
|
||||
name,
|
||||
CAST (date_start AS TEXT) date_start,
|
||||
CAST (date_end AS TEXT) date_end,
|
||||
state,
|
||||
location,
|
||||
temp_min,
|
||||
temp_max,
|
||||
comment
|
||||
FROM trips",
|
||||
)
|
||||
.fetch(&state.database_pool)
|
||||
.map_ok(|row| row.try_into())
|
||||
.try_collect::<Vec<Result<models::Trip, models::Error>>>()
|
||||
.await
|
||||
// we have two error handling lines here. these are distinct errors
|
||||
// this one is the SQL error that may arise during the query
|
||||
.map_err(|e| {
|
||||
(
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
ErrorPage::build(&e.to_string()),
|
||||
)
|
||||
})?
|
||||
.into_iter()
|
||||
.collect::<Result<Vec<models::Trip>, models::Error>>()
|
||||
// and this one is the model mapping error that may arise e.g. during
|
||||
// reading of the rows
|
||||
.map_err(|e| {
|
||||
(
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
ErrorPage::build(&e.to_string()),
|
||||
)
|
||||
})?;
|
||||
|
||||
tracing::info!("received trips");
|
||||
|
||||
Ok((
|
||||
StatusCode::OK,
|
||||
@@ -532,20 +582,42 @@ async fn trip(
|
||||
state.client_state.trip_edit_attribute = trip_query.edit;
|
||||
state.client_state.active_category_id = trip_query.category;
|
||||
|
||||
let mut trip: models::Trip =
|
||||
query("SELECT id,name,date_start,date_end,state,location,temp_min,temp_max,comment FROM trips WHERE id = ?")
|
||||
.bind(id.to_string())
|
||||
.fetch_one(&state.database_pool)
|
||||
.map_ok(std::convert::TryInto::try_into)
|
||||
.await
|
||||
.map_err(|e: sqlx::Error| match e {
|
||||
sqlx::Error::RowNotFound => (
|
||||
StatusCode::NOT_FOUND,
|
||||
ErrorPage::build(&format!("trip with id {} not found", id)),
|
||||
),
|
||||
_ => (StatusCode::INTERNAL_SERVER_ERROR, ErrorPage::build(&e.to_string())),
|
||||
})?
|
||||
.map_err(|e: Error| (StatusCode::INTERNAL_SERVER_ERROR, ErrorPage::build(&e.to_string())))?;
|
||||
let id_param = id.to_string();
|
||||
let mut trip: models::Trip = query_as!(
|
||||
DbTripRow,
|
||||
"SELECT
|
||||
id,
|
||||
name,
|
||||
CAST (date_start AS TEXT) AS date_start,
|
||||
CAST (date_end AS TEXT) AS date_end,
|
||||
state,
|
||||
location,
|
||||
temp_min,
|
||||
temp_max,
|
||||
comment
|
||||
FROM trips
|
||||
WHERE id = ?",
|
||||
id_param,
|
||||
)
|
||||
.fetch_one(&state.database_pool)
|
||||
.map_ok(|row| row.try_into())
|
||||
.await
|
||||
.map_err(|e: sqlx::Error| match e {
|
||||
sqlx::Error::RowNotFound => (
|
||||
StatusCode::NOT_FOUND,
|
||||
ErrorPage::build(&format!("trip with id {} not found", id)),
|
||||
),
|
||||
_ => (
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
ErrorPage::build(&e.to_string()),
|
||||
),
|
||||
})?
|
||||
.map_err(|e: Error| {
|
||||
(
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
ErrorPage::build(&e.to_string()),
|
||||
)
|
||||
})?;
|
||||
|
||||
trip.load_trips_types(&state.database_pool)
|
||||
.await
|
||||
@@ -586,14 +658,16 @@ async fn trip_type_remove(
|
||||
State(state): State<AppState>,
|
||||
Path((trip_id, type_id)): Path<(Uuid, Uuid)>,
|
||||
) -> Result<Redirect, (StatusCode, Markup)> {
|
||||
let results = query(
|
||||
let trip_id = trip_id.to_string();
|
||||
let type_id = type_id.to_string();
|
||||
let results = query!(
|
||||
"DELETE FROM trips_to_trips_types AS ttt
|
||||
WHERE ttt.trip_id = ?
|
||||
AND ttt.trip_type_id = ?
|
||||
",
|
||||
trip_id,
|
||||
type_id
|
||||
)
|
||||
.bind(trip_id.to_string())
|
||||
.bind(type_id.to_string())
|
||||
.execute(&state.database_pool)
|
||||
.await
|
||||
.map_err(|e| (StatusCode::BAD_REQUEST, ErrorPage::build(&e.to_string())))?;
|
||||
@@ -612,12 +686,14 @@ async fn trip_type_add(
|
||||
State(state): State<AppState>,
|
||||
Path((trip_id, type_id)): Path<(Uuid, Uuid)>,
|
||||
) -> Result<Redirect, (StatusCode, Markup)> {
|
||||
query(
|
||||
let trip_id = trip_id.to_string();
|
||||
let type_id = type_id.to_string();
|
||||
query!(
|
||||
"INSERT INTO trips_to_trips_types
|
||||
(trip_id, trip_type_id) VALUES (?, ?)",
|
||||
trip_id,
|
||||
type_id
|
||||
)
|
||||
.bind(trip_id.to_string())
|
||||
.bind(type_id.to_string())
|
||||
.execute(&state.database_pool)
|
||||
.await
|
||||
.map_err(|e| match e {
|
||||
@@ -682,13 +758,14 @@ async fn trip_comment_set(
|
||||
Path(trip_id): Path<Uuid>,
|
||||
Form(comment_update): Form<CommentUpdate>,
|
||||
) -> Result<Redirect, (StatusCode, Markup)> {
|
||||
let result = query(
|
||||
let trip_id = trip_id.to_string();
|
||||
let result = query!(
|
||||
"UPDATE trips
|
||||
SET comment = ?
|
||||
WHERE id = ?",
|
||||
comment_update.new_comment,
|
||||
trip_id,
|
||||
)
|
||||
.bind(comment_update.new_comment)
|
||||
.bind(trip_id.to_string())
|
||||
.execute(&state.database_pool)
|
||||
.await
|
||||
.map_err(|e| (StatusCode::BAD_REQUEST, ErrorPage::build(&e.to_string())))?;
|
||||
@@ -884,14 +961,15 @@ async fn inventory_category_create(
|
||||
Form(new_category): Form<NewCategory>,
|
||||
) -> Result<Redirect, (StatusCode, Markup)> {
|
||||
let id = Uuid::new_v4();
|
||||
query(
|
||||
let id_param = id.to_string();
|
||||
query!(
|
||||
"INSERT INTO inventory_items_categories
|
||||
(id, name)
|
||||
VALUES
|
||||
(?, ?)",
|
||||
id_param,
|
||||
new_category.name
|
||||
)
|
||||
.bind(id.to_string())
|
||||
.bind(&new_category.name)
|
||||
.execute(&state.database_pool)
|
||||
.map_err(|e| match e {
|
||||
sqlx::Error::Database(ref error) => {
|
||||
|
||||
@@ -8,6 +8,7 @@ use sqlx::{
|
||||
use std::convert;
|
||||
use std::error;
|
||||
use std::fmt;
|
||||
use std::num::TryFromIntError;
|
||||
use std::str::FromStr;
|
||||
use uuid::Uuid;
|
||||
|
||||
@@ -16,10 +17,19 @@ use sqlx::sqlite::SqlitePoolOptions;
|
||||
use futures::TryFutureExt;
|
||||
use futures::TryStreamExt;
|
||||
|
||||
use time::{
|
||||
error::Parse as TimeParseError, format_description::FormatItem, macros::format_description,
|
||||
};
|
||||
|
||||
pub const DATE_FORMAT: &[FormatItem<'static>] = format_description!("[year]-[month]-[day]");
|
||||
|
||||
pub enum Error {
|
||||
SqlError { description: String },
|
||||
UuidError { description: String },
|
||||
EnumError { description: String },
|
||||
NotFoundError { description: String },
|
||||
IntError { description: String },
|
||||
TimeParseError { description: String },
|
||||
}
|
||||
|
||||
impl fmt::Display for Error {
|
||||
@@ -34,6 +44,15 @@ impl fmt::Display for Error {
|
||||
Self::NotFoundError { description } => {
|
||||
write!(f, "Not found: {description}")
|
||||
}
|
||||
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}")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -61,6 +80,22 @@ impl convert::From<sqlx::Error> for Error {
|
||||
}
|
||||
}
|
||||
|
||||
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(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl error::Error for Error {}
|
||||
|
||||
#[derive(sqlx::Type)]
|
||||
@@ -88,6 +123,25 @@ impl fmt::Display for TripState {
|
||||
}
|
||||
}
|
||||
|
||||
impl std::convert::TryFrom<&str> for TripState {
|
||||
type Error = Error;
|
||||
|
||||
fn try_from(value: &str) -> Result<Self, Self::Error> {
|
||||
Ok(match value {
|
||||
"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),
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize, Debug)]
|
||||
pub enum TripItemStateKey {
|
||||
Pick,
|
||||
@@ -114,7 +168,7 @@ pub struct TripCategory {
|
||||
}
|
||||
|
||||
impl TripCategory {
|
||||
pub fn total_picked_weight(&self) -> u32 {
|
||||
pub fn total_picked_weight(&self) -> i64 {
|
||||
self.items
|
||||
.as_ref()
|
||||
.unwrap()
|
||||
@@ -132,15 +186,47 @@ pub struct TripItem {
|
||||
pub packed: bool,
|
||||
}
|
||||
|
||||
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,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Trip {
|
||||
pub id: Uuid,
|
||||
pub name: String,
|
||||
pub date_start: time::Date,
|
||||
pub date_end: time::Date,
|
||||
pub state: TripState,
|
||||
pub location: String,
|
||||
pub temp_min: i32,
|
||||
pub temp_max: i32,
|
||||
pub location: Option<String>,
|
||||
pub temp_min: Option<i64>,
|
||||
pub temp_max: Option<i64>,
|
||||
pub comment: Option<String>,
|
||||
types: Option<Vec<TripType>>,
|
||||
categories: Option<Vec<TripCategory>>,
|
||||
@@ -193,37 +279,37 @@ pub enum TripAttribute {
|
||||
// }
|
||||
// }
|
||||
|
||||
impl TryFrom<SqliteRow> for Trip {
|
||||
type Error = Error;
|
||||
// impl TryFrom<SqliteRow> for Trip {
|
||||
// type Error = Error;
|
||||
|
||||
fn try_from(row: SqliteRow) -> Result<Self, Self::Error> {
|
||||
let name: &str = row.try_get("name")?;
|
||||
let id: &str = row.try_get("id")?;
|
||||
let date_start: time::Date = row.try_get("date_start")?;
|
||||
let date_end: time::Date = row.try_get("date_end")?;
|
||||
let state: TripState = row.try_get("state")?;
|
||||
let location = row.try_get("location")?;
|
||||
let temp_min = row.try_get("temp_min")?;
|
||||
let temp_max = row.try_get("temp_max")?;
|
||||
let comment = row.try_get("comment")?;
|
||||
// fn try_from(row: SqliteRow) -> Result<Self, Self::Error> {
|
||||
// let name: &str = row.try_get("name")?;
|
||||
// let id: &str = row.try_get("id")?;
|
||||
// let date_start: time::Date = row.try_get("date_start")?;
|
||||
// let date_end: time::Date = row.try_get("date_end")?;
|
||||
// let state: TripState = row.try_get("state")?;
|
||||
// let location = row.try_get("location")?;
|
||||
// let temp_min = row.try_get("temp_min")?;
|
||||
// let temp_max = row.try_get("temp_max")?;
|
||||
// let comment = row.try_get("comment")?;
|
||||
|
||||
let id: Uuid = Uuid::try_parse(id)?;
|
||||
// let id: Uuid = Uuid::try_parse(id)?;
|
||||
|
||||
Ok(Trip {
|
||||
id,
|
||||
name: name.to_string(),
|
||||
date_start,
|
||||
date_end,
|
||||
state,
|
||||
location,
|
||||
temp_min,
|
||||
temp_max,
|
||||
comment,
|
||||
types: None,
|
||||
categories: None,
|
||||
})
|
||||
}
|
||||
}
|
||||
// Ok(Trip {
|
||||
// id,
|
||||
// name: name.to_string(),
|
||||
// date_start,
|
||||
// date_end,
|
||||
// state,
|
||||
// location,
|
||||
// temp_min,
|
||||
// temp_max,
|
||||
// comment,
|
||||
// types: None,
|
||||
// categories: None,
|
||||
// })
|
||||
// }
|
||||
// }
|
||||
|
||||
impl<'a> Trip {
|
||||
pub fn types(&'a self) -> &Vec<TripType> {
|
||||
@@ -244,12 +330,13 @@ impl<'a> Trip {
|
||||
&'a mut self,
|
||||
pool: &sqlx::Pool<sqlx::Sqlite>,
|
||||
) -> Result<(), Error> {
|
||||
let types = sqlx::query(
|
||||
let id = self.id.to_string();
|
||||
let types = sqlx::query!(
|
||||
"
|
||||
SELECT
|
||||
type.id as id,
|
||||
type.name as name,
|
||||
CASE WHEN inner.id IS NOT NULL THEN true ELSE false END AS active
|
||||
inner.id IS NOT NULL AS active
|
||||
FROM trips_types AS type
|
||||
LEFT JOIN (
|
||||
SELECT type.id as id, type.name as name
|
||||
@@ -262,10 +349,20 @@ impl<'a> Trip {
|
||||
) AS inner
|
||||
ON inner.id = type.id
|
||||
",
|
||||
id
|
||||
)
|
||||
.bind(self.id.to_string())
|
||||
.fetch(pool)
|
||||
.map_ok(std::convert::TryInto::try_into)
|
||||
.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!(),
|
||||
},
|
||||
})
|
||||
})
|
||||
.try_collect::<Vec<Result<TripType, Error>>>()
|
||||
.await?
|
||||
.into_iter()
|
||||
@@ -282,14 +379,14 @@ impl<'a> Trip {
|
||||
let mut categories: Vec<TripCategory> = vec![];
|
||||
// we can ignore the return type as we collect into `categories`
|
||||
// in the `map_ok()` closure
|
||||
sqlx::query(
|
||||
let id = self.id.to_string();
|
||||
sqlx::query!(
|
||||
"
|
||||
SELECT
|
||||
category.id as category_id,
|
||||
category.name as category_name,
|
||||
category.description AS category_description,
|
||||
inner.trip_id AS trip_id,
|
||||
inner.category_description AS category_description,
|
||||
inner.item_id AS item_id,
|
||||
inner.item_name AS item_name,
|
||||
inner.item_description AS item_description,
|
||||
@@ -314,25 +411,28 @@ impl<'a> Trip {
|
||||
ON item.id = trip.item_id
|
||||
INNER JOIN inventory_items_categories as category
|
||||
ON category.id = item.category_id
|
||||
WHERE trip.trip_id = 'a8b181d6-3b16-4a41-99fa-0713b94a34d9'
|
||||
WHERE trip.trip_id = ?
|
||||
) AS inner
|
||||
ON inner.category_id = category.id
|
||||
",
|
||||
id
|
||||
)
|
||||
.bind(self.id.to_string())
|
||||
.fetch(pool)
|
||||
.map_ok(|row| -> Result<(), Error> {
|
||||
let mut category = TripCategory {
|
||||
category: Category {
|
||||
id: Uuid::try_parse(row.try_get("category_id")?)?,
|
||||
name: row.try_get("category_name")?,
|
||||
description: row.try_get("category_description")?,
|
||||
id: Uuid::try_parse(&row.category_id)?,
|
||||
name: row.category_name,
|
||||
// TODO align optionality between code and database
|
||||
// idea: make description nullable
|
||||
description: row.category_description,
|
||||
|
||||
items: None,
|
||||
},
|
||||
items: None,
|
||||
};
|
||||
|
||||
match row.try_get("item_id")? {
|
||||
match row.item_id {
|
||||
None => {
|
||||
// we have an empty (unused) category which has NULL values
|
||||
// for the item_id column
|
||||
@@ -342,14 +442,14 @@ impl<'a> Trip {
|
||||
Some(item_id) => {
|
||||
let item = TripItem {
|
||||
item: Item {
|
||||
id: Uuid::try_parse(item_id)?,
|
||||
name: row.try_get("item_name")?,
|
||||
description: row.try_get("item_description")?,
|
||||
weight: row.try_get("item_weight")?,
|
||||
id: Uuid::try_parse(&item_id)?,
|
||||
name: row.item_name.unwrap(),
|
||||
description: row.item_description,
|
||||
weight: row.item_weight.unwrap(),
|
||||
category_id: category.category.id,
|
||||
},
|
||||
picked: row.try_get("item_is_picked")?,
|
||||
packed: row.try_get("item_is_packed")?,
|
||||
picked: row.item_is_picked.unwrap(),
|
||||
packed: row.item_is_packed.unwrap(),
|
||||
};
|
||||
|
||||
if let Some(&mut ref mut c) = categories
|
||||
@@ -385,43 +485,70 @@ pub struct TripType {
|
||||
pub active: bool,
|
||||
}
|
||||
|
||||
impl TryFrom<SqliteRow> for TripType {
|
||||
type Error = Error;
|
||||
// impl TryFrom<SqliteRow> for TripType {
|
||||
// type Error = Error;
|
||||
|
||||
fn try_from(row: SqliteRow) -> Result<Self, Self::Error> {
|
||||
let id: Uuid = Uuid::try_parse(row.try_get("id")?)?;
|
||||
let name: String = row.try_get::<&str, _>("name")?.to_string();
|
||||
let active: bool = row.try_get("active")?;
|
||||
// fn try_from(row: SqliteRow) -> Result<Self, Self::Error> {
|
||||
// let id: Uuid = Uuid::try_parse(row.try_get("id")?)?;
|
||||
// let name: String = row.try_get::<&str, _>("name")?.to_string();
|
||||
// let active: bool = row.try_get("active")?;
|
||||
|
||||
Ok(Self { id, name, active })
|
||||
}
|
||||
// Ok(Self { id, name, active })
|
||||
// }
|
||||
// }
|
||||
|
||||
pub struct DbCategoryRow {
|
||||
pub id: String,
|
||||
pub name: String,
|
||||
pub description: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Category {
|
||||
pub id: Uuid,
|
||||
pub name: String,
|
||||
pub description: String,
|
||||
pub description: Option<String>,
|
||||
items: Option<Vec<Item>>,
|
||||
}
|
||||
|
||||
impl TryFrom<SqliteRow> for Category {
|
||||
impl TryFrom<DbCategoryRow> for Category {
|
||||
type Error = Error;
|
||||
|
||||
fn try_from(row: SqliteRow) -> Result<Self, Self::Error> {
|
||||
let name: &str = row.try_get("name")?;
|
||||
let description: &str = row.try_get("description")?;
|
||||
let id: Uuid = Uuid::try_parse(row.try_get("id")?)?;
|
||||
|
||||
fn try_from(row: DbCategoryRow) -> Result<Self, Self::Error> {
|
||||
Ok(Category {
|
||||
id,
|
||||
name: name.to_string(),
|
||||
description: description.to_string(),
|
||||
id: Uuid::try_parse(&row.id)?,
|
||||
name: row.name,
|
||||
description: row.description,
|
||||
items: None,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// impl TryFrom<SqliteRow> for Category {
|
||||
// type Error = Error;
|
||||
|
||||
// fn try_from(row: SqliteRow) -> Result<Self, Self::Error> {
|
||||
// let name: &str = row.try_get("name")?;
|
||||
// let description: &str = row.try_get("description")?;
|
||||
// let id: Uuid = Uuid::try_parse(row.try_get("id")?)?;
|
||||
|
||||
// Ok(Category {
|
||||
// id,
|
||||
// name: name.to_string(),
|
||||
// description: description.to_string(),
|
||||
// items: None,
|
||||
// })
|
||||
// }
|
||||
// }
|
||||
|
||||
pub struct DbInventoryItemsRow {
|
||||
id: String,
|
||||
name: String,
|
||||
weight: i64,
|
||||
description: Option<String>,
|
||||
category_id: String,
|
||||
}
|
||||
|
||||
impl<'a> Category {
|
||||
pub fn items(&'a self) -> &'a Vec<Item> {
|
||||
self.items
|
||||
@@ -429,7 +556,7 @@ impl<'a> Category {
|
||||
.expect("you need to call populate_items()")
|
||||
}
|
||||
|
||||
pub fn total_weight(&self) -> u32 {
|
||||
pub fn total_weight(&self) -> i64 {
|
||||
self.items().iter().map(|item| item.weight).sum()
|
||||
}
|
||||
|
||||
@@ -437,15 +564,21 @@ impl<'a> Category {
|
||||
&'a mut self,
|
||||
pool: &sqlx::Pool<sqlx::Sqlite>,
|
||||
) -> Result<(), Error> {
|
||||
let items = sqlx::query(&format!(
|
||||
let id = self.id.to_string();
|
||||
let items = sqlx::query_as!(
|
||||
DbInventoryItemsRow,
|
||||
"SELECT
|
||||
id,name,weight,description,category_id
|
||||
id,
|
||||
name,
|
||||
weight,
|
||||
description,
|
||||
category_id
|
||||
FROM inventory_items
|
||||
WHERE category_id = '{id}'",
|
||||
id = self.id
|
||||
))
|
||||
WHERE category_id = ?",
|
||||
id
|
||||
)
|
||||
.fetch(pool)
|
||||
.map_ok(std::convert::TryInto::try_into)
|
||||
.map_ok(|row| row.try_into())
|
||||
.try_collect::<Vec<Result<Item, Error>>>()
|
||||
.await?
|
||||
.into_iter()
|
||||
@@ -460,40 +593,56 @@ impl<'a> Category {
|
||||
pub struct Item {
|
||||
pub id: Uuid,
|
||||
pub name: String,
|
||||
pub description: String,
|
||||
pub weight: u32,
|
||||
pub description: Option<String>,
|
||||
pub weight: i64,
|
||||
pub category_id: Uuid,
|
||||
}
|
||||
|
||||
impl TryFrom<SqliteRow> for Item {
|
||||
impl TryFrom<DbInventoryItemsRow> for Item {
|
||||
type Error = Error;
|
||||
|
||||
fn try_from(row: SqliteRow) -> Result<Self, Self::Error> {
|
||||
let name: &str = row.try_get("name")?;
|
||||
let description: &str = row.try_get("description")?;
|
||||
let weight: u32 = row.try_get("weight")?;
|
||||
let id: Uuid = Uuid::try_parse(row.try_get("id")?)?;
|
||||
let category_id: Uuid = Uuid::try_parse(row.try_get("category_id")?)?;
|
||||
|
||||
fn try_from(row: DbInventoryItemsRow) -> Result<Self, Self::Error> {
|
||||
Ok(Item {
|
||||
id,
|
||||
name: name.to_string(),
|
||||
weight,
|
||||
description: description.to_string(),
|
||||
category_id,
|
||||
id: Uuid::try_parse(&row.id)?,
|
||||
name: row.name,
|
||||
description: row.description, // TODO
|
||||
weight: row.weight,
|
||||
category_id: Uuid::try_parse(&row.category_id)?,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// impl TryFrom<SqliteRow> for Item {
|
||||
// type Error = Error;
|
||||
|
||||
// fn try_from(row: SqliteRow) -> Result<Self, Self::Error> {
|
||||
// let name: &str = row.try_get("name")?;
|
||||
// let description: &str = row.try_get("description")?;
|
||||
// let weight: i64 = row.try_get("weight")?;
|
||||
// let id: Uuid = Uuid::try_parse(row.try_get("id")?)?;
|
||||
// let category_id: Uuid = Uuid::try_parse(row.try_get("category_id")?)?;
|
||||
|
||||
// Ok(Item {
|
||||
// id,
|
||||
// name: name.to_string(),
|
||||
// weight,
|
||||
// description: description.to_string(),
|
||||
// category_id,
|
||||
// })
|
||||
// }
|
||||
// }
|
||||
|
||||
impl Item {
|
||||
pub async fn find(pool: &sqlx::Pool<sqlx::Sqlite>, id: Uuid) -> Result<Option<Item>, Error> {
|
||||
let item: Result<Result<Item, Error>, sqlx::Error> = sqlx::query(
|
||||
let id_param = id.to_string();
|
||||
let item: Result<Result<Item, Error>, sqlx::Error> = sqlx::query_as!(
|
||||
DbInventoryItemsRow,
|
||||
"SELECT * FROM inventory_items AS item
|
||||
WHERE item.id = ?",
|
||||
id_param,
|
||||
)
|
||||
.bind(id.to_string())
|
||||
.fetch_one(pool)
|
||||
.map_ok(std::convert::TryInto::try_into)
|
||||
.map_ok(|row| row.try_into())
|
||||
.await;
|
||||
|
||||
match item {
|
||||
@@ -509,9 +658,10 @@ impl Item {
|
||||
pool: &sqlx::Pool<sqlx::Sqlite>,
|
||||
id: Uuid,
|
||||
name: &str,
|
||||
weight: u32,
|
||||
weight: i64,
|
||||
) -> Result<Option<Uuid>, Error> {
|
||||
let id: Result<Result<Uuid, Error>, sqlx::Error> = sqlx::query(
|
||||
let id_param = id.to_string();
|
||||
let id: Result<Result<Uuid, Error>, sqlx::Error> = sqlx::query!(
|
||||
"UPDATE inventory_items AS item
|
||||
SET
|
||||
name = ?,
|
||||
@@ -519,13 +669,13 @@ impl Item {
|
||||
WHERE item.id = ?
|
||||
RETURNING inventory_items.category_id AS id
|
||||
",
|
||||
name,
|
||||
weight,
|
||||
id_param,
|
||||
)
|
||||
.bind(name)
|
||||
.bind(weight)
|
||||
.bind(id.to_string())
|
||||
.fetch_one(pool)
|
||||
.map_ok(|row| {
|
||||
let id: &str = row.try_get("id")?;
|
||||
let id: &str = &row.id.unwrap(); // TODO
|
||||
let uuid: Result<Uuid, uuid::Error> = Uuid::try_parse(id);
|
||||
let uuid: Result<Uuid, Error> = uuid.map_err(|e| e.into());
|
||||
uuid
|
||||
|
||||
Reference in New Issue
Block a user