done with inventory

This commit is contained in:
2023-08-29 21:34:00 +02:00
parent db4d61570a
commit ef4ef00d2c
15 changed files with 198 additions and 95 deletions

View File

@@ -1,4 +1,5 @@
use super::Error;
use crate::Context;
use futures::{TryFutureExt, TryStreamExt};
use uuid::Uuid;
@@ -8,10 +9,17 @@ pub struct Inventory {
}
impl Inventory {
pub async fn load(pool: &sqlx::Pool<sqlx::Sqlite>) -> Result<Self, Error> {
pub async fn load(ctx: &Context, pool: &sqlx::Pool<sqlx::Sqlite>) -> Result<Self, Error> {
let user_id = ctx.user.id.to_string();
let mut categories = sqlx::query_as!(
DbCategoryRow,
"SELECT id,name,description FROM inventory_items_categories"
"SELECT
id,
name,
description
FROM inventory_items_categories
WHERE user_id = ?",
user_id,
)
.fetch(pool)
.map_ok(|row: DbCategoryRow| row.try_into())
@@ -21,7 +29,7 @@ impl Inventory {
.collect::<Result<Vec<Category>, Error>>()?;
for category in &mut categories {
category.populate_items(pool).await?;
category.populate_items(&ctx, &pool).await?;
}
Ok(Self { categories })
@@ -57,10 +65,12 @@ impl TryFrom<DbCategoryRow> for Category {
impl Category {
pub async fn _find(
ctx: &Context,
pool: &sqlx::Pool<sqlx::Sqlite>,
id: Uuid,
) -> Result<Option<Category>, Error> {
let id_param = id.to_string();
let user_id = ctx.user.id.to_string();
sqlx::query_as!(
DbCategoryRow,
"SELECT
@@ -68,8 +78,11 @@ impl Category {
name,
description
FROM inventory_items_categories AS category
WHERE category.id = ?",
WHERE
category.id = ?
AND category.user_id = ?",
id_param,
user_id,
)
.fetch_optional(pool)
.await?
@@ -77,16 +90,22 @@ impl Category {
.transpose()
}
pub async fn save(pool: &sqlx::Pool<sqlx::Sqlite>, name: &str) -> Result<Uuid, Error> {
pub async fn save(
ctx: &Context,
pool: &sqlx::Pool<sqlx::Sqlite>,
name: &str,
) -> Result<Uuid, Error> {
let id = Uuid::new_v4();
let id_param = id.to_string();
let user_id = ctx.user.id.to_string();
sqlx::query!(
"INSERT INTO inventory_items_categories
(id, name)
(id, name, user_id)
VALUES
(?, ?)",
(?, ?, ?)",
id_param,
name,
user_id,
)
.execute(pool)
.await?;
@@ -104,8 +123,13 @@ impl Category {
self.items().iter().map(|item| item.weight).sum()
}
pub async fn populate_items(&mut self, pool: &sqlx::Pool<sqlx::Sqlite>) -> Result<(), Error> {
pub async fn populate_items(
&mut self,
ctx: &Context,
pool: &sqlx::Pool<sqlx::Sqlite>,
) -> Result<(), Error> {
let id = self.id.to_string();
let user_id = ctx.user.id.to_string();
let items = sqlx::query_as!(
DbInventoryItemsRow,
"SELECT
@@ -115,8 +139,11 @@ impl Category {
description,
category_id
FROM inventory_items
WHERE category_id = ?",
id
WHERE
category_id = ?
AND user_id = ?",
id,
user_id,
)
.fetch(pool)
.map_ok(|row| row.try_into())
@@ -191,8 +218,13 @@ impl TryFrom<DbInventoryItemRow> for InventoryItem {
}
impl InventoryItem {
pub async fn find(pool: &sqlx::Pool<sqlx::Sqlite>, id: Uuid) -> Result<Option<Self>, Error> {
pub async fn find(
ctx: &Context,
pool: &sqlx::Pool<sqlx::Sqlite>,
id: Uuid,
) -> Result<Option<Self>, Error> {
let id_param = id.to_string();
let user_id = ctx.user.id.to_string();
sqlx::query_as!(
DbInventoryItemRow,
@@ -213,8 +245,11 @@ impl InventoryItem {
ON item.category_id = category.id
LEFT JOIN inventory_products AS product
ON item.product_id = product.id
WHERE item.id = ?",
WHERE
item.id = ?
AND item.user_id = ?",
id_param,
user_id,
)
.fetch_optional(pool)
.await?
@@ -222,12 +257,20 @@ impl InventoryItem {
.transpose()
}
pub async fn name_exists(pool: &sqlx::Pool<sqlx::Sqlite>, name: &str) -> Result<bool, Error> {
pub async fn name_exists(
ctx: &Context,
pool: &sqlx::Pool<sqlx::Sqlite>,
name: &str,
) -> Result<bool, Error> {
let user_id = ctx.user.id.to_string();
Ok(sqlx::query!(
"SELECT id
FROM inventory_items
WHERE name = ?",
WHERE
name = ?
AND user_id = ?",
name,
user_id
)
.fetch_optional(pool)
.await?
@@ -235,12 +278,20 @@ impl InventoryItem {
.is_some())
}
pub async fn delete(pool: &sqlx::Pool<sqlx::Sqlite>, id: Uuid) -> Result<bool, Error> {
pub async fn delete(
ctx: &Context,
pool: &sqlx::Pool<sqlx::Sqlite>,
id: Uuid,
) -> Result<bool, Error> {
let id_param = id.to_string();
let user_id = ctx.user.id.to_string();
let results = sqlx::query!(
"DELETE FROM inventory_items
WHERE id = ?",
id_param
WHERE
id = ?
AND user_id = ?",
id_param,
user_id,
)
.execute(pool)
.await?;
@@ -249,11 +300,13 @@ impl InventoryItem {
}
pub async fn update(
ctx: &Context,
pool: &sqlx::Pool<sqlx::Sqlite>,
id: Uuid,
name: &str,
weight: u32,
) -> Result<Uuid, Error> {
let user_id = ctx.user.id.to_string();
let weight = i64::try_from(weight).unwrap();
let id_param = id.to_string();
@@ -262,12 +315,15 @@ impl InventoryItem {
SET
name = ?,
weight = ?
WHERE item.id = ?
WHERE
item.id = ?
AND item.user_id = ?
RETURNING inventory_items.category_id AS id
",
name,
weight,
id_param,
user_id,
)
.fetch_one(pool)
.map_ok(|row| Uuid::try_parse(&row.id))
@@ -275,6 +331,7 @@ impl InventoryItem {
}
pub async fn save(
ctx: &Context,
pool: &sqlx::Pool<sqlx::Sqlite>,
name: &str,
category_id: Uuid,
@@ -282,18 +339,20 @@ impl InventoryItem {
) -> Result<Uuid, Error> {
let id = Uuid::new_v4();
let id_param = id.to_string();
let user_id = ctx.user.id.to_string();
let category_id_param = category_id.to_string();
sqlx::query!(
"INSERT INTO inventory_items
(id, name, description, weight, category_id)
(id, name, description, weight, category_id, user_id)
VALUES
(?, ?, ?, ?, ?)",
(?, ?, ?, ?, ?, ?)",
id_param,
name,
"",
weight,
category_id_param
category_id_param,
user_id,
)
.execute(pool)
.await?;
@@ -302,9 +361,11 @@ impl InventoryItem {
}
pub async fn get_category_max_weight(
ctx: &Context,
pool: &sqlx::Pool<sqlx::Sqlite>,
category_id: Uuid,
) -> Result<i64, Error> {
let user_id = ctx.user.id.to_string();
let category_id_param = category_id.to_string();
let weight = sqlx::query!(
"
@@ -312,9 +373,12 @@ impl InventoryItem {
FROM inventory_items_categories as category
INNER JOIN inventory_items as i_item
ON i_item.category_id = category.id
WHERE category_id = ?
WHERE
category_id = ?
AND category.user_id = ?
",
category_id_param
category_id_param,
user_id,
)
.fetch_one(pool)
.map_ok(|row| {
@@ -361,9 +425,11 @@ impl TryFrom<DbInventoryItemsRow> for Item {
impl Item {
pub async fn _get_category_total_picked_weight(
ctx: &Context,
pool: &sqlx::Pool<sqlx::Sqlite>,
category_id: Uuid,
) -> Result<i64, Error> {
let user_id = ctx.user.id.to_string();
let category_id_param = category_id.to_string();
Ok(sqlx::query!(
"
@@ -373,10 +439,13 @@ impl Item {
ON i_item.category_id = category.id
INNER JOIN trips_items as t_item
ON i_item.id = t_item.item_id
WHERE category_id = ?
AND t_item.pick = 1
WHERE
category_id = ?
AND category.user_id = ?
AND t_item.pick = 1
",
category_id_param
category_id_param,
user_id,
)
.fetch_one(pool)
.map_ok(|row| {

View File

@@ -128,10 +128,11 @@ pub async fn inventory_active(
Path(id): Path<Uuid>,
Query(inventory_query): Query<InventoryQuery>,
) -> Result<impl IntoResponse, Error> {
let ctx = Context::build(current_user);
state.client_state.edit_item = inventory_query.edit_item;
state.client_state.active_category_id = Some(id);
let inventory = models::inventory::Inventory::load(&state.database_pool).await?;
let inventory = models::inventory::Inventory::load(&ctx, &state.database_pool).await?;
let active_category: Option<&models::inventory::Category> = state
.client_state
@@ -148,7 +149,7 @@ pub async fn inventory_active(
.transpose()?;
Ok(view::Root::build(
&Context::build(current_user),
&ctx,
&view::inventory::Inventory::build(
active_category,
&inventory.categories,
@@ -163,13 +164,14 @@ pub async fn inventory_inactive(
State(mut state): State<AppState>,
Query(inventory_query): Query<InventoryQuery>,
) -> Result<impl IntoResponse, Error> {
let ctx = Context::build(current_user);
state.client_state.edit_item = inventory_query.edit_item;
state.client_state.active_category_id = None;
let inventory = models::inventory::Inventory::load(&state.database_pool).await?;
let inventory = models::inventory::Inventory::load(&ctx, &state.database_pool).await?;
Ok(view::Root::build(
&Context::build(current_user),
&ctx,
&view::inventory::Inventory::build(
None,
&inventory.categories,
@@ -180,11 +182,14 @@ pub async fn inventory_inactive(
}
pub async fn inventory_item_validate_name(
Extension(current_user): Extension<models::user::User>,
State(state): State<AppState>,
Form(new_item): Form<NewItemName>,
) -> Result<impl IntoResponse, Error> {
let ctx = Context::build(current_user);
let exists =
models::inventory::InventoryItem::name_exists(&state.database_pool, &new_item.name).await?;
models::inventory::InventoryItem::name_exists(&ctx, &state.database_pool, &new_item.name)
.await?;
Ok(view::inventory::InventoryNewItemFormName::build(
Some(&new_item.name),
@@ -193,10 +198,12 @@ pub async fn inventory_item_validate_name(
}
pub async fn inventory_item_create(
Extension(current_user): Extension<models::user::User>,
State(state): State<AppState>,
headers: HeaderMap,
Form(new_item): Form<NewItem>,
) -> Result<impl IntoResponse, Error> {
let ctx = Context::build(current_user);
if new_item.name.is_empty() {
return Err(Error::Request(RequestError::EmptyFormElement {
name: "name".to_string(),
@@ -204,6 +211,7 @@ pub async fn inventory_item_create(
}
let _new_id = models::inventory::InventoryItem::save(
&ctx,
&state.database_pool,
&new_item.name,
new_item.category_id,
@@ -212,7 +220,7 @@ pub async fn inventory_item_create(
.await?;
if htmx::is_htmx(&headers) {
let inventory = models::inventory::Inventory::load(&state.database_pool).await?;
let inventory = models::inventory::Inventory::load(&ctx, &state.database_pool).await?;
// it's impossible to NOT find the item here, as we literally just added
// it.
@@ -239,11 +247,13 @@ pub async fn inventory_item_create(
}
}
pub async fn inventory_item_delete(
Extension(current_user): Extension<models::user::User>,
State(state): State<AppState>,
headers: HeaderMap,
Path(id): Path<Uuid>,
) -> Result<Redirect, Error> {
let deleted = models::inventory::InventoryItem::delete(&state.database_pool, id).await?;
let ctx = Context::build(current_user);
let deleted = models::inventory::InventoryItem::delete(&ctx, &state.database_pool, id).await?;
if !deleted {
Err(Error::Request(RequestError::NotFound {
@@ -255,10 +265,12 @@ pub async fn inventory_item_delete(
}
pub async fn inventory_item_edit(
Extension(current_user): Extension<models::user::User>,
State(state): State<AppState>,
Path(id): Path<Uuid>,
Form(edit_item): Form<EditItem>,
) -> Result<Redirect, Error> {
let ctx = Context::build(current_user);
if edit_item.name.is_empty() {
return Err(Error::Request(RequestError::EmptyFormElement {
name: "name".to_string(),
@@ -266,6 +278,7 @@ pub async fn inventory_item_edit(
}
let id = models::inventory::InventoryItem::update(
&ctx,
&state.database_pool,
id,
&edit_item.name,
@@ -277,10 +290,12 @@ pub async fn inventory_item_edit(
}
pub async fn inventory_item_cancel(
Extension(current_user): Extension<models::user::User>,
State(state): State<AppState>,
Path(id): Path<Uuid>,
) -> Result<Redirect, Error> {
let id = models::inventory::InventoryItem::find(&state.database_pool, id)
let ctx = Context::build(current_user);
let id = models::inventory::InventoryItem::find(&ctx, &state.database_pool, id)
.await?
.ok_or(Error::Request(RequestError::NotFound {
message: format!("item with id {id} not found"),
@@ -453,6 +468,7 @@ pub async fn trip_item_set_state(
}
pub async fn trip_row(
ctx: &Context,
state: &AppState,
trip_id: Uuid,
item_id: Uuid,
@@ -469,6 +485,7 @@ pub async fn trip_row(
trip_id,
&item,
models::inventory::InventoryItem::get_category_max_weight(
&ctx,
&state.database_pool,
item.item.category_id,
)
@@ -509,9 +526,11 @@ pub async fn trip_item_set_pick(
}
pub async fn trip_item_set_pick_htmx(
Extension(current_user): Extension<models::user::User>,
State(state): State<AppState>,
Path((trip_id, item_id)): Path<(Uuid, Uuid)>,
) -> Result<impl IntoResponse, Error> {
let ctx = Context::build(current_user);
trip_item_set_state(
&state,
trip_id,
@@ -525,7 +544,7 @@ pub async fn trip_item_set_pick_htmx(
htmx::ResponseHeaders::Trigger.into(),
htmx::Event::TripItemEdited.into(),
);
Ok((headers, trip_row(&state, trip_id, item_id).await?))
Ok((headers, trip_row(&ctx, &state, trip_id, item_id).await?))
}
pub async fn trip_item_set_unpick(
@@ -547,9 +566,11 @@ pub async fn trip_item_set_unpick(
}
pub async fn trip_item_set_unpick_htmx(
Extension(current_user): Extension<models::user::User>,
State(state): State<AppState>,
Path((trip_id, item_id)): Path<(Uuid, Uuid)>,
) -> Result<impl IntoResponse, Error> {
let ctx = Context::build(current_user);
trip_item_set_state(
&state,
trip_id,
@@ -563,7 +584,7 @@ pub async fn trip_item_set_unpick_htmx(
htmx::ResponseHeaders::Trigger.into(),
htmx::Event::TripItemEdited.into(),
);
Ok((headers, trip_row(&state, trip_id, item_id).await?))
Ok((headers, trip_row(&ctx, &state, trip_id, item_id).await?))
}
pub async fn trip_item_set_pack(
@@ -585,9 +606,11 @@ pub async fn trip_item_set_pack(
}
pub async fn trip_item_set_pack_htmx(
Extension(current_user): Extension<models::user::User>,
State(state): State<AppState>,
Path((trip_id, item_id)): Path<(Uuid, Uuid)>,
) -> Result<impl IntoResponse, Error> {
let ctx = Context::build(current_user);
trip_item_set_state(
&state,
trip_id,
@@ -601,7 +624,7 @@ pub async fn trip_item_set_pack_htmx(
htmx::ResponseHeaders::Trigger.into(),
htmx::Event::TripItemEdited.into(),
);
Ok((headers, trip_row(&state, trip_id, item_id).await?))
Ok((headers, trip_row(&ctx, &state, trip_id, item_id).await?))
}
pub async fn trip_item_set_unpack(
@@ -623,9 +646,11 @@ pub async fn trip_item_set_unpack(
}
pub async fn trip_item_set_unpack_htmx(
Extension(current_user): Extension<models::user::User>,
State(state): State<AppState>,
Path((trip_id, item_id)): Path<(Uuid, Uuid)>,
) -> Result<impl IntoResponse, Error> {
let ctx = Context::build(current_user);
trip_item_set_state(
&state,
trip_id,
@@ -639,7 +664,7 @@ pub async fn trip_item_set_unpack_htmx(
htmx::ResponseHeaders::Trigger.into(),
htmx::Event::TripItemEdited.into(),
);
Ok((headers, trip_row(&state, trip_id, item_id).await?))
Ok((headers, trip_row(&ctx, &state, trip_id, item_id).await?))
}
pub async fn trip_item_set_ready(
@@ -661,9 +686,11 @@ pub async fn trip_item_set_ready(
}
pub async fn trip_item_set_ready_htmx(
Extension(current_user): Extension<models::user::User>,
State(state): State<AppState>,
Path((trip_id, item_id)): Path<(Uuid, Uuid)>,
) -> Result<impl IntoResponse, Error> {
let ctx = Context::build(current_user);
trip_item_set_state(
&state,
trip_id,
@@ -677,7 +704,7 @@ pub async fn trip_item_set_ready_htmx(
htmx::ResponseHeaders::Trigger.into(),
htmx::Event::TripItemEdited.into(),
);
Ok((headers, trip_row(&state, trip_id, item_id).await?))
Ok((headers, trip_row(&ctx, &state, trip_id, item_id).await?))
}
pub async fn trip_item_set_unready(
@@ -699,9 +726,11 @@ pub async fn trip_item_set_unready(
}
pub async fn trip_item_set_unready_htmx(
Extension(current_user): Extension<models::user::User>,
State(state): State<AppState>,
Path((trip_id, item_id)): Path<(Uuid, Uuid)>,
) -> Result<impl IntoResponse, Error> {
let ctx = Context::build(current_user);
trip_item_set_state(
&state,
trip_id,
@@ -715,7 +744,7 @@ pub async fn trip_item_set_unready_htmx(
htmx::ResponseHeaders::Trigger.into(),
htmx::Event::TripItemEdited.into(),
);
Ok((headers, trip_row(&state, trip_id, item_id).await?))
Ok((headers, trip_row(&ctx, &state, trip_id, item_id).await?))
}
pub async fn trip_total_weight_htmx(
@@ -731,9 +760,11 @@ pub async fn trip_total_weight_htmx(
}
pub async fn inventory_category_create(
Extension(current_user): Extension<models::user::User>,
State(state): State<AppState>,
Form(new_category): Form<NewCategory>,
) -> Result<Redirect, Error> {
let ctx = Context::build(current_user);
if new_category.name.is_empty() {
return Err(Error::Request(RequestError::EmptyFormElement {
name: "name".to_string(),
@@ -741,7 +772,7 @@ pub async fn inventory_category_create(
}
let _new_id =
models::inventory::Category::save(&state.database_pool, &new_category.name).await?;
models::inventory::Category::save(&ctx, &state.database_pool, &new_category.name).await?;
Ok(Redirect::to("/inventory/"))
}
@@ -827,14 +858,15 @@ pub async fn inventory_item(
State(state): State<AppState>,
Path(id): Path<Uuid>,
) -> Result<impl IntoResponse, Error> {
let item = models::inventory::InventoryItem::find(&state.database_pool, id)
let ctx = Context::build(current_user);
let item = models::inventory::InventoryItem::find(&ctx, &state.database_pool, id)
.await?
.ok_or(Error::Request(RequestError::NotFound {
message: format!("inventory item with id {id} not found"),
}))?;
Ok(view::Root::build(
&Context::build(current_user),
&ctx,
&view::inventory::InventoryItem::build(&state.client_state, &item),
Some(&TopLevelPage::Inventory),
))
@@ -873,10 +905,12 @@ pub async fn trip_category_select(
}
pub async fn inventory_category_select(
Extension(current_user): Extension<models::user::User>,
State(state): State<AppState>,
Path(category_id): Path<Uuid>,
) -> Result<impl IntoResponse, Error> {
let inventory = models::inventory::Inventory::load(&state.database_pool).await?;
let ctx = Context::build(current_user);
let inventory = models::inventory::Inventory::load(&ctx, &state.database_pool).await?;
let active_category: Option<&models::inventory::Category> = Some(
inventory