diff --git a/rust/.sqlx/query-5fd2b986fcaafe93e816f9ed665b6319d120a3987dc5adca1e3c8634203f7533.json b/rust/.sqlx/query-1899fd9182e8791104be2c369298b3f1731b34404ec5ac3d1312a8d84dbf5c7d.json similarity index 69% rename from rust/.sqlx/query-5fd2b986fcaafe93e816f9ed665b6319d120a3987dc5adca1e3c8634203f7533.json rename to rust/.sqlx/query-1899fd9182e8791104be2c369298b3f1731b34404ec5ac3d1312a8d84dbf5c7d.json index c3b4716..a6e6cc3 100644 --- a/rust/.sqlx/query-5fd2b986fcaafe93e816f9ed665b6319d120a3987dc5adca1e3c8634203f7533.json +++ b/rust/.sqlx/query-1899fd9182e8791104be2c369298b3f1731b34404ec5ac3d1312a8d84dbf5c7d.json @@ -1,6 +1,6 @@ { "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": { "columns": [ { @@ -10,11 +10,11 @@ } ], "parameters": { - "Right": 1 + "Right": 2 }, "nullable": [ false ] }, - "hash": "5fd2b986fcaafe93e816f9ed665b6319d120a3987dc5adca1e3c8634203f7533" + "hash": "1899fd9182e8791104be2c369298b3f1731b34404ec5ac3d1312a8d84dbf5c7d" } diff --git a/rust/.sqlx/query-6973cceeb5499216475136b320b25e1355974e1213829d931abdd6b7a1448a87.json b/rust/.sqlx/query-2be306559191f0e14e04c33d42b7e5c2aca78e794465ead29273172204534e73.json similarity index 83% rename from rust/.sqlx/query-6973cceeb5499216475136b320b25e1355974e1213829d931abdd6b7a1448a87.json rename to rust/.sqlx/query-2be306559191f0e14e04c33d42b7e5c2aca78e794465ead29273172204534e73.json index c2dc589..565c80f 100644 --- a/rust/.sqlx/query-6973cceeb5499216475136b320b25e1355974e1213829d931abdd6b7a1448a87.json +++ b/rust/.sqlx/query-2be306559191f0e14e04c33d42b7e5c2aca78e794465ead29273172204534e73.json @@ -1,6 +1,6 @@ { "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": { "columns": [ { @@ -30,7 +30,7 @@ } ], "parameters": { - "Right": 1 + "Right": 2 }, "nullable": [ false, @@ -40,5 +40,5 @@ false ] }, - "hash": "6973cceeb5499216475136b320b25e1355974e1213829d931abdd6b7a1448a87" + "hash": "2be306559191f0e14e04c33d42b7e5c2aca78e794465ead29273172204534e73" } diff --git a/rust/.sqlx/query-68f936e138283635c5e7bde3313f036aae4167143200ea20fb89e1e99ada9ad1.json b/rust/.sqlx/query-31bd853345496b0680c337f7b9e3c5e5ee04a315af737507001d9a128e981c52.json similarity index 51% rename from rust/.sqlx/query-68f936e138283635c5e7bde3313f036aae4167143200ea20fb89e1e99ada9ad1.json rename to rust/.sqlx/query-31bd853345496b0680c337f7b9e3c5e5ee04a315af737507001d9a128e981c52.json index 7829ab9..3b54601 100644 --- a/rust/.sqlx/query-68f936e138283635c5e7bde3313f036aae4167143200ea20fb89e1e99ada9ad1.json +++ b/rust/.sqlx/query-31bd853345496b0680c337f7b9e3c5e5ee04a315af737507001d9a128e981c52.json @@ -1,12 +1,12 @@ { "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": { "columns": [], "parameters": { - "Right": 2 + "Right": 3 }, "nullable": [] }, - "hash": "68f936e138283635c5e7bde3313f036aae4167143200ea20fb89e1e99ada9ad1" + "hash": "31bd853345496b0680c337f7b9e3c5e5ee04a315af737507001d9a128e981c52" } diff --git a/rust/.sqlx/query-cc1ad49669cff7f89975abfab3d0a8caef2e3978c826e1877db91c05a7f9d00d.json b/rust/.sqlx/query-4118e44ba436f4ad7de970613fbaa3f88762d37b4e7dbd7b72a76298dad00fd6.json similarity index 72% rename from rust/.sqlx/query-cc1ad49669cff7f89975abfab3d0a8caef2e3978c826e1877db91c05a7f9d00d.json rename to rust/.sqlx/query-4118e44ba436f4ad7de970613fbaa3f88762d37b4e7dbd7b72a76298dad00fd6.json index a4e2ef1..c5acfbd 100644 --- a/rust/.sqlx/query-cc1ad49669cff7f89975abfab3d0a8caef2e3978c826e1877db91c05a7f9d00d.json +++ b/rust/.sqlx/query-4118e44ba436f4ad7de970613fbaa3f88762d37b4e7dbd7b72a76298dad00fd6.json @@ -1,6 +1,6 @@ { "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": { "columns": [ { @@ -20,7 +20,7 @@ } ], "parameters": { - "Right": 1 + "Right": 2 }, "nullable": [ false, @@ -28,5 +28,5 @@ true ] }, - "hash": "cc1ad49669cff7f89975abfab3d0a8caef2e3978c826e1877db91c05a7f9d00d" + "hash": "4118e44ba436f4ad7de970613fbaa3f88762d37b4e7dbd7b72a76298dad00fd6" } diff --git a/rust/.sqlx/query-452cb08b3b46bda9cb62d390d9f518d97626270a26465e55793b0a4b05432e50.json b/rust/.sqlx/query-452cb08b3b46bda9cb62d390d9f518d97626270a26465e55793b0a4b05432e50.json deleted file mode 100644 index 168b60c..0000000 --- a/rust/.sqlx/query-452cb08b3b46bda9cb62d390d9f518d97626270a26465e55793b0a4b05432e50.json +++ /dev/null @@ -1,12 +0,0 @@ -{ - "db_name": "SQLite", - "query": "DELETE FROM inventory_items\n WHERE id = ?", - "describe": { - "columns": [], - "parameters": { - "Right": 1 - }, - "nullable": [] - }, - "hash": "452cb08b3b46bda9cb62d390d9f518d97626270a26465e55793b0a4b05432e50" -} diff --git a/rust/.sqlx/query-a81bcbeb11260e3b4363e19c26b71b489e326b08bfacb6e11b4c4fc068dc7806.json b/rust/.sqlx/query-6e8fbd5a7269c6a2021b688b58f74f8235554a414f77af5c2524d51920f0e33c.json similarity index 62% rename from rust/.sqlx/query-a81bcbeb11260e3b4363e19c26b71b489e326b08bfacb6e11b4c4fc068dc7806.json rename to rust/.sqlx/query-6e8fbd5a7269c6a2021b688b58f74f8235554a414f77af5c2524d51920f0e33c.json index 50ca4a7..4167939 100644 --- a/rust/.sqlx/query-a81bcbeb11260e3b4363e19c26b71b489e326b08bfacb6e11b4c4fc068dc7806.json +++ b/rust/.sqlx/query-6e8fbd5a7269c6a2021b688b58f74f8235554a414f77af5c2524d51920f0e33c.json @@ -1,6 +1,6 @@ { "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": { "columns": [ { @@ -20,7 +20,7 @@ } ], "parameters": { - "Right": 0 + "Right": 1 }, "nullable": [ false, @@ -28,5 +28,5 @@ true ] }, - "hash": "a81bcbeb11260e3b4363e19c26b71b489e326b08bfacb6e11b4c4fc068dc7806" + "hash": "6e8fbd5a7269c6a2021b688b58f74f8235554a414f77af5c2524d51920f0e33c" } diff --git a/rust/.sqlx/query-f9d080a5b8710c7d6a497bb1f5cf4839ad1589fd7d6a06d3faf1163d6981d8a0.json b/rust/.sqlx/query-a65f6e3cfb2261d37ce3e806ec8d091314c0b206aee3bfe995d6295f4e0cc911.json similarity index 65% rename from rust/.sqlx/query-f9d080a5b8710c7d6a497bb1f5cf4839ad1589fd7d6a06d3faf1163d6981d8a0.json rename to rust/.sqlx/query-a65f6e3cfb2261d37ce3e806ec8d091314c0b206aee3bfe995d6295f4e0cc911.json index 06321ec..acb84a9 100644 --- a/rust/.sqlx/query-f9d080a5b8710c7d6a497bb1f5cf4839ad1589fd7d6a06d3faf1163d6981d8a0.json +++ b/rust/.sqlx/query-a65f6e3cfb2261d37ce3e806ec8d091314c0b206aee3bfe995d6295f4e0cc911.json @@ -1,6 +1,6 @@ { "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": { "columns": [ { @@ -10,11 +10,11 @@ } ], "parameters": { - "Right": 1 + "Right": 2 }, "nullable": [ false ] }, - "hash": "f9d080a5b8710c7d6a497bb1f5cf4839ad1589fd7d6a06d3faf1163d6981d8a0" + "hash": "a65f6e3cfb2261d37ce3e806ec8d091314c0b206aee3bfe995d6295f4e0cc911" } diff --git a/rust/.sqlx/query-629f34ef9bd0afa39123758cc182468edb7475f6c08cb4b5febb3ce931f2b547.json b/rust/.sqlx/query-b4d2e5f57ee95dd449ab0d69450d471d9fd80b9fc943436185dcd25c7402271e.json similarity index 91% rename from rust/.sqlx/query-629f34ef9bd0afa39123758cc182468edb7475f6c08cb4b5febb3ce931f2b547.json rename to rust/.sqlx/query-b4d2e5f57ee95dd449ab0d69450d471d9fd80b9fc943436185dcd25c7402271e.json index dcc13f2..a437fba 100644 --- a/rust/.sqlx/query-629f34ef9bd0afa39123758cc182468edb7475f6c08cb4b5febb3ce931f2b547.json +++ b/rust/.sqlx/query-b4d2e5f57ee95dd449ab0d69450d471d9fd80b9fc943436185dcd25c7402271e.json @@ -1,6 +1,6 @@ { "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": { "columns": [ { @@ -60,7 +60,7 @@ } ], "parameters": { - "Right": 1 + "Right": 2 }, "nullable": [ false, @@ -76,5 +76,5 @@ true ] }, - "hash": "629f34ef9bd0afa39123758cc182468edb7475f6c08cb4b5febb3ce931f2b547" + "hash": "b4d2e5f57ee95dd449ab0d69450d471d9fd80b9fc943436185dcd25c7402271e" } diff --git a/rust/.sqlx/query-0d341935886c28710302aec9d5d085b535ad54949b87793e98cbf3bd5d828a41.json b/rust/.sqlx/query-c1afeeec7f57f4af47d9a99f807b7be6fe2441af601a77d462a99ca7a22ca7c4.json similarity index 59% rename from rust/.sqlx/query-0d341935886c28710302aec9d5d085b535ad54949b87793e98cbf3bd5d828a41.json rename to rust/.sqlx/query-c1afeeec7f57f4af47d9a99f807b7be6fe2441af601a77d462a99ca7a22ca7c4.json index e48a6f8..3bb2763 100644 --- a/rust/.sqlx/query-0d341935886c28710302aec9d5d085b535ad54949b87793e98cbf3bd5d828a41.json +++ b/rust/.sqlx/query-c1afeeec7f57f4af47d9a99f807b7be6fe2441af601a77d462a99ca7a22ca7c4.json @@ -1,6 +1,6 @@ { "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": { "columns": [ { @@ -10,11 +10,11 @@ } ], "parameters": { - "Right": 3 + "Right": 4 }, "nullable": [ false ] }, - "hash": "0d341935886c28710302aec9d5d085b535ad54949b87793e98cbf3bd5d828a41" + "hash": "c1afeeec7f57f4af47d9a99f807b7be6fe2441af601a77d462a99ca7a22ca7c4" } diff --git a/rust/.sqlx/query-918fc9cf50097d4210b212255ef49335ebedbe81002ce9a418b4dab4fbb29aa3.json b/rust/.sqlx/query-c34c9c567a98937e043424db17246ddf23dbc8b618dca6c637f2d71d03841920.json similarity index 63% rename from rust/.sqlx/query-918fc9cf50097d4210b212255ef49335ebedbe81002ce9a418b4dab4fbb29aa3.json rename to rust/.sqlx/query-c34c9c567a98937e043424db17246ddf23dbc8b618dca6c637f2d71d03841920.json index fb056bb..985e5b5 100644 --- a/rust/.sqlx/query-918fc9cf50097d4210b212255ef49335ebedbe81002ce9a418b4dab4fbb29aa3.json +++ b/rust/.sqlx/query-c34c9c567a98937e043424db17246ddf23dbc8b618dca6c637f2d71d03841920.json @@ -1,6 +1,6 @@ { "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": { "columns": [ { @@ -10,11 +10,11 @@ } ], "parameters": { - "Right": 1 + "Right": 2 }, "nullable": [ false ] }, - "hash": "918fc9cf50097d4210b212255ef49335ebedbe81002ce9a418b4dab4fbb29aa3" + "hash": "c34c9c567a98937e043424db17246ddf23dbc8b618dca6c637f2d71d03841920" } diff --git a/rust/.sqlx/query-d7c6ae3c6e00c6c99b0bedee87ff237b01007e7001584c82ae896b91833b807b.json b/rust/.sqlx/query-d7c6ae3c6e00c6c99b0bedee87ff237b01007e7001584c82ae896b91833b807b.json deleted file mode 100644 index de57e5a..0000000 --- a/rust/.sqlx/query-d7c6ae3c6e00c6c99b0bedee87ff237b01007e7001584c82ae896b91833b807b.json +++ /dev/null @@ -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" -} diff --git a/rust/.sqlx/query-ec968e7d97f30547cca37cb1834a054e1ba4ea5461e202cf329b929953c00894.json b/rust/.sqlx/query-ec968e7d97f30547cca37cb1834a054e1ba4ea5461e202cf329b929953c00894.json new file mode 100644 index 0000000..247f902 --- /dev/null +++ b/rust/.sqlx/query-ec968e7d97f30547cca37cb1834a054e1ba4ea5461e202cf329b929953c00894.json @@ -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" +} diff --git a/rust/.sqlx/query-f6a5ccd2290b8c3d12cddd7c80899b551fa40dbec71f1498974f9a07cdf5f70e.json b/rust/.sqlx/query-f6a5ccd2290b8c3d12cddd7c80899b551fa40dbec71f1498974f9a07cdf5f70e.json new file mode 100644 index 0000000..db91b96 --- /dev/null +++ b/rust/.sqlx/query-f6a5ccd2290b8c3d12cddd7c80899b551fa40dbec71f1498974f9a07cdf5f70e.json @@ -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" +} diff --git a/rust/src/models/inventory.rs b/rust/src/models/inventory.rs index 4b2da74..a383925 100644 --- a/rust/src/models/inventory.rs +++ b/rust/src/models/inventory.rs @@ -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) -> Result { + pub async fn load(ctx: &Context, pool: &sqlx::Pool) -> Result { + 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::, 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 for Category { impl Category { pub async fn _find( + ctx: &Context, pool: &sqlx::Pool, id: Uuid, ) -> Result, 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, name: &str) -> Result { + pub async fn save( + ctx: &Context, + pool: &sqlx::Pool, + name: &str, + ) -> Result { 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) -> Result<(), Error> { + pub async fn populate_items( + &mut self, + ctx: &Context, + pool: &sqlx::Pool, + ) -> 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 for InventoryItem { } impl InventoryItem { - pub async fn find(pool: &sqlx::Pool, id: Uuid) -> Result, Error> { + pub async fn find( + ctx: &Context, + pool: &sqlx::Pool, + id: Uuid, + ) -> Result, 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, name: &str) -> Result { + pub async fn name_exists( + ctx: &Context, + pool: &sqlx::Pool, + name: &str, + ) -> Result { + 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, id: Uuid) -> Result { + pub async fn delete( + ctx: &Context, + pool: &sqlx::Pool, + id: Uuid, + ) -> Result { 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, id: Uuid, name: &str, weight: u32, ) -> Result { + 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, name: &str, category_id: Uuid, @@ -282,18 +339,20 @@ impl InventoryItem { ) -> Result { 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, category_id: Uuid, ) -> Result { + 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 for Item { impl Item { pub async fn _get_category_total_picked_weight( + ctx: &Context, pool: &sqlx::Pool, category_id: Uuid, ) -> Result { + 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| { diff --git a/rust/src/routing/routes.rs b/rust/src/routing/routes.rs index 682c466..2618d1b 100644 --- a/rust/src/routing/routes.rs +++ b/rust/src/routing/routes.rs @@ -128,10 +128,11 @@ pub async fn inventory_active( Path(id): Path, Query(inventory_query): Query, ) -> Result { + 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, Query(inventory_query): Query, ) -> Result { + 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, State(state): State, Form(new_item): Form, ) -> Result { + 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, State(state): State, headers: HeaderMap, Form(new_item): Form, ) -> Result { + 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, State(state): State, headers: HeaderMap, Path(id): Path, ) -> Result { - 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, State(state): State, Path(id): Path, Form(edit_item): Form, ) -> Result { + 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, State(state): State, Path(id): Path, ) -> Result { - 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, State(state): State, Path((trip_id, item_id)): Path<(Uuid, Uuid)>, ) -> Result { + 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, State(state): State, Path((trip_id, item_id)): Path<(Uuid, Uuid)>, ) -> Result { + 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, State(state): State, Path((trip_id, item_id)): Path<(Uuid, Uuid)>, ) -> Result { + 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, State(state): State, Path((trip_id, item_id)): Path<(Uuid, Uuid)>, ) -> Result { + 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, State(state): State, Path((trip_id, item_id)): Path<(Uuid, Uuid)>, ) -> Result { + 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, State(state): State, Path((trip_id, item_id)): Path<(Uuid, Uuid)>, ) -> Result { + 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, State(state): State, Form(new_category): Form, ) -> Result { + 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, Path(id): Path, ) -> Result { - 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, State(state): State, Path(category_id): Path, ) -> Result { - 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