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,6 +1,6 @@
{ {
"db_name": "SQLite", "db_name": "SQLite",
"query": "\n SELECT COALESCE(MAX(i_item.weight), 0) as weight\n FROM inventory_items_categories as category\n INNER JOIN inventory_items as i_item\n ON i_item.category_id = category.id\n WHERE category_id = ?\n ", "query": "\n SELECT COALESCE(MAX(i_item.weight), 0) as weight\n FROM inventory_items_categories as category\n INNER JOIN inventory_items as i_item\n ON i_item.category_id = category.id\n WHERE \n category_id = ?\n AND category.user_id = ?\n ",
"describe": { "describe": {
"columns": [ "columns": [
{ {
@@ -10,11 +10,11 @@
} }
], ],
"parameters": { "parameters": {
"Right": 1 "Right": 2
}, },
"nullable": [ "nullable": [
false false
] ]
}, },
"hash": "5fd2b986fcaafe93e816f9ed665b6319d120a3987dc5adca1e3c8634203f7533" "hash": "1899fd9182e8791104be2c369298b3f1731b34404ec5ac3d1312a8d84dbf5c7d"
} }

View File

@@ -1,6 +1,6 @@
{ {
"db_name": "SQLite", "db_name": "SQLite",
"query": "SELECT\n id,\n name,\n weight,\n description,\n category_id\n FROM inventory_items\n WHERE category_id = ?", "query": "SELECT\n id,\n name,\n weight,\n description,\n category_id\n FROM inventory_items\n WHERE \n category_id = ?\n AND user_id = ?",
"describe": { "describe": {
"columns": [ "columns": [
{ {
@@ -30,7 +30,7 @@
} }
], ],
"parameters": { "parameters": {
"Right": 1 "Right": 2
}, },
"nullable": [ "nullable": [
false, false,
@@ -40,5 +40,5 @@
false false
] ]
}, },
"hash": "6973cceeb5499216475136b320b25e1355974e1213829d931abdd6b7a1448a87" "hash": "2be306559191f0e14e04c33d42b7e5c2aca78e794465ead29273172204534e73"
} }

View File

@@ -1,12 +1,12 @@
{ {
"db_name": "SQLite", "db_name": "SQLite",
"query": "INSERT INTO inventory_items_categories\n (id, name)\n VALUES\n (?, ?)", "query": "INSERT INTO inventory_items_categories\n (id, name, user_id)\n VALUES\n (?, ?, ?)",
"describe": { "describe": {
"columns": [], "columns": [],
"parameters": { "parameters": {
"Right": 2 "Right": 3
}, },
"nullable": [] "nullable": []
}, },
"hash": "68f936e138283635c5e7bde3313f036aae4167143200ea20fb89e1e99ada9ad1" "hash": "31bd853345496b0680c337f7b9e3c5e5ee04a315af737507001d9a128e981c52"
} }

View File

@@ -1,6 +1,6 @@
{ {
"db_name": "SQLite", "db_name": "SQLite",
"query": "SELECT\n id,\n name,\n description\n FROM inventory_items_categories AS category\n WHERE category.id = ?", "query": "SELECT\n id,\n name,\n description\n FROM inventory_items_categories AS category\n WHERE \n category.id = ?\n AND category.user_id = ?",
"describe": { "describe": {
"columns": [ "columns": [
{ {
@@ -20,7 +20,7 @@
} }
], ],
"parameters": { "parameters": {
"Right": 1 "Right": 2
}, },
"nullable": [ "nullable": [
false, false,
@@ -28,5 +28,5 @@
true true
] ]
}, },
"hash": "cc1ad49669cff7f89975abfab3d0a8caef2e3978c826e1877db91c05a7f9d00d" "hash": "4118e44ba436f4ad7de970613fbaa3f88762d37b4e7dbd7b72a76298dad00fd6"
} }

View File

@@ -1,12 +0,0 @@
{
"db_name": "SQLite",
"query": "DELETE FROM inventory_items\n WHERE id = ?",
"describe": {
"columns": [],
"parameters": {
"Right": 1
},
"nullable": []
},
"hash": "452cb08b3b46bda9cb62d390d9f518d97626270a26465e55793b0a4b05432e50"
}

View File

@@ -1,6 +1,6 @@
{ {
"db_name": "SQLite", "db_name": "SQLite",
"query": "SELECT id,name,description FROM inventory_items_categories", "query": "SELECT \n id,\n name,\n description \n FROM inventory_items_categories \n WHERE user_id = ?",
"describe": { "describe": {
"columns": [ "columns": [
{ {
@@ -20,7 +20,7 @@
} }
], ],
"parameters": { "parameters": {
"Right": 0 "Right": 1
}, },
"nullable": [ "nullable": [
false, false,
@@ -28,5 +28,5 @@
true true
] ]
}, },
"hash": "a81bcbeb11260e3b4363e19c26b71b489e326b08bfacb6e11b4c4fc068dc7806" "hash": "6e8fbd5a7269c6a2021b688b58f74f8235554a414f77af5c2524d51920f0e33c"
} }

View File

@@ -1,6 +1,6 @@
{ {
"db_name": "SQLite", "db_name": "SQLite",
"query": "\n SELECT COALESCE(SUM(i_item.weight), 0) as weight\n FROM inventory_items_categories as category\n INNER JOIN inventory_items as i_item\n ON i_item.category_id = category.id\n INNER JOIN trips_items as t_item\n ON i_item.id = t_item.item_id\n WHERE category_id = ?\n AND t_item.pick = 1\n ", "query": "\n SELECT COALESCE(SUM(i_item.weight), 0) as weight\n FROM inventory_items_categories as category\n INNER JOIN inventory_items as i_item\n ON i_item.category_id = category.id\n INNER JOIN trips_items as t_item\n ON i_item.id = t_item.item_id\n WHERE \n category_id = ?\n AND category.user_id = ?\n AND t_item.pick = 1\n ",
"describe": { "describe": {
"columns": [ "columns": [
{ {
@@ -10,11 +10,11 @@
} }
], ],
"parameters": { "parameters": {
"Right": 1 "Right": 2
}, },
"nullable": [ "nullable": [
false false
] ]
}, },
"hash": "f9d080a5b8710c7d6a497bb1f5cf4839ad1589fd7d6a06d3faf1163d6981d8a0" "hash": "a65f6e3cfb2261d37ce3e806ec8d091314c0b206aee3bfe995d6295f4e0cc911"
} }

View File

@@ -1,6 +1,6 @@
{ {
"db_name": "SQLite", "db_name": "SQLite",
"query": "SELECT\n item.id AS id,\n item.name AS name,\n item.description AS description,\n weight,\n category.id AS category_id,\n category.name AS category_name,\n category.description AS category_description,\n product.id AS product_id,\n product.name AS product_name,\n product.description AS product_description,\n product.comment AS product_comment\n FROM inventory_items AS item\n INNER JOIN inventory_items_categories as category\n ON item.category_id = category.id\n LEFT JOIN inventory_products AS product\n ON item.product_id = product.id\n WHERE item.id = ?", "query": "SELECT\n item.id AS id,\n item.name AS name,\n item.description AS description,\n weight,\n category.id AS category_id,\n category.name AS category_name,\n category.description AS category_description,\n product.id AS product_id,\n product.name AS product_name,\n product.description AS product_description,\n product.comment AS product_comment\n FROM inventory_items AS item\n INNER JOIN inventory_items_categories as category\n ON item.category_id = category.id\n LEFT JOIN inventory_products AS product\n ON item.product_id = product.id\n WHERE \n item.id = ?\n AND item.user_id = ?",
"describe": { "describe": {
"columns": [ "columns": [
{ {
@@ -60,7 +60,7 @@
} }
], ],
"parameters": { "parameters": {
"Right": 1 "Right": 2
}, },
"nullable": [ "nullable": [
false, false,
@@ -76,5 +76,5 @@
true true
] ]
}, },
"hash": "629f34ef9bd0afa39123758cc182468edb7475f6c08cb4b5febb3ce931f2b547" "hash": "b4d2e5f57ee95dd449ab0d69450d471d9fd80b9fc943436185dcd25c7402271e"
} }

View File

@@ -1,6 +1,6 @@
{ {
"db_name": "SQLite", "db_name": "SQLite",
"query": "UPDATE inventory_items AS item\n SET\n name = ?,\n weight = ?\n WHERE item.id = ?\n RETURNING inventory_items.category_id AS id\n ", "query": "UPDATE inventory_items AS item\n SET\n name = ?,\n weight = ?\n WHERE \n item.id = ?\n AND item.user_id = ?\n RETURNING inventory_items.category_id AS id\n ",
"describe": { "describe": {
"columns": [ "columns": [
{ {
@@ -10,11 +10,11 @@
} }
], ],
"parameters": { "parameters": {
"Right": 3 "Right": 4
}, },
"nullable": [ "nullable": [
false false
] ]
}, },
"hash": "0d341935886c28710302aec9d5d085b535ad54949b87793e98cbf3bd5d828a41" "hash": "c1afeeec7f57f4af47d9a99f807b7be6fe2441af601a77d462a99ca7a22ca7c4"
} }

View File

@@ -1,6 +1,6 @@
{ {
"db_name": "SQLite", "db_name": "SQLite",
"query": "SELECT id\n FROM inventory_items\n WHERE name = ?", "query": "SELECT id\n FROM inventory_items\n WHERE \n name = ?\n AND user_id = ?",
"describe": { "describe": {
"columns": [ "columns": [
{ {
@@ -10,11 +10,11 @@
} }
], ],
"parameters": { "parameters": {
"Right": 1 "Right": 2
}, },
"nullable": [ "nullable": [
false false
] ]
}, },
"hash": "918fc9cf50097d4210b212255ef49335ebedbe81002ce9a418b4dab4fbb29aa3" "hash": "c34c9c567a98937e043424db17246ddf23dbc8b618dca6c637f2d71d03841920"
} }

View File

@@ -1,12 +0,0 @@
{
"db_name": "SQLite",
"query": "INSERT INTO inventory_items\n (id, name, description, weight, category_id)\n VALUES\n (?, ?, ?, ?, ?)",
"describe": {
"columns": [],
"parameters": {
"Right": 5
},
"nullable": []
},
"hash": "d7c6ae3c6e00c6c99b0bedee87ff237b01007e7001584c82ae896b91833b807b"
}

View File

@@ -0,0 +1,12 @@
{
"db_name": "SQLite",
"query": "DELETE FROM inventory_items\n WHERE \n id = ?\n AND user_id = ?",
"describe": {
"columns": [],
"parameters": {
"Right": 2
},
"nullable": []
},
"hash": "ec968e7d97f30547cca37cb1834a054e1ba4ea5461e202cf329b929953c00894"
}

View File

@@ -0,0 +1,12 @@
{
"db_name": "SQLite",
"query": "INSERT INTO inventory_items\n (id, name, description, weight, category_id, user_id)\n VALUES\n (?, ?, ?, ?, ?, ?)",
"describe": {
"columns": [],
"parameters": {
"Right": 6
},
"nullable": []
},
"hash": "f6a5ccd2290b8c3d12cddd7c80899b551fa40dbec71f1498974f9a07cdf5f70e"
}

View File

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

View File

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