From be9be4c9dac24cf63613f16251f1d62df9fbee41 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hannes=20K=C3=B6rber?= Date: Tue, 29 Aug 2023 21:34:00 +0200 Subject: [PATCH] implement user handling for trips --- ...6ef5dc39339291572005e2a9278fb28113a2b.json | 12 - ...a5196039e6845598b8b22852b7665bcd5c0c.json} | 6 +- ...1c9cab8a25598b19e1c9038dae9092fe18f1f.json | 20 -- ...b8555b878290b8a5a68564ec3e8b3d8e7ed01.json | 12 - ...a53cee248ac310fa620c272af1514f9ae951.json} | 6 +- ...30a1592512178477d210acfce662c857cf65.json} | 4 +- ...4b1ad5d05976eec3432406a3d8ddf92d8b94.json} | 6 +- ...18780c6c416eda63b246daa75c5ff318331d.json} | 6 +- ...ec187d7ad3742cb85371550bdaed2b780d7f5.json | 12 + ...2dbc3712ac4c2dcf48b90b6ec4ee8900ec2ef.json | 12 + ...9827bcf2b75fa75af1ce238530377e25d7bd.json} | 6 +- ...227a77ef1a24c8fcf722bf8e49beb8c7ba83.json} | 6 +- ...78e7e7a312e101bdd9846123ee4de7c4751a.json} | 6 +- ...386435fbebc83f18db53ab8650b29414e40fb.json | 12 - ...613b8117a437f07e22b0e8204687f8485b872.json | 12 + ...e7c3d8202d3876a26505cd26be7ceeb10d06d.json | 20 ++ ...1e470a0d80493e13478393f357da417a0208b.json | 12 + ...ca6f63b79252471ce4621d8692771e6fd3cc.json} | 6 +- ...ed355fab4bb37a56b9061bc37f3f053703cb.json} | 6 +- ...e792afe635b41eea4d32db30ac9775794520.json} | 6 +- ...e1aff32eae0c645e9c544b48926f7ca16a66.json} | 6 +- ...b7261d9224abb8a54da980db8262733d08205.json | 12 - .../20230820193229_add_user_to_trips.sql | 38 +++ .../20230820194457_add_user_to_trips2.sql | 21 ++ ...20230820200039_add_user_to_trips_types.sql | 13 ++ rust/query.sql | 10 + rust/src/models/trips.rs | 221 ++++++++++++------ rust/src/routing/routes.rs | 138 ++++++++--- 28 files changed, 445 insertions(+), 202 deletions(-) delete mode 100644 rust/.sqlx/query-0e8a12e168d8ff41005b2b5a1356ef5dc39339291572005e2a9278fb28113a2b.json rename rust/.sqlx/{query-86eb9fd598fbbb50b32ea7eae3151bfd8a83639c8b70301f5d5aeef19ae9462c.json => query-12167f7d6f8b1bfc5f37c214b089a5196039e6845598b8b22852b7665bcd5c0c.json} (61%) delete mode 100644 rust/.sqlx/query-1994305e1521fe1f5f927ad28e21c9cab8a25598b19e1c9038dae9092fe18f1f.json delete mode 100644 rust/.sqlx/query-259757f1bb08f0c366371202c75b8555b878290b8a5a68564ec3e8b3d8e7ed01.json rename rust/.sqlx/{query-a417bf136b8f79339a21fda10518c781d0d2bfc8c3fc99353d1956dfad9f5ac8.json => query-2793ec31c3345d77a824314bffa8a53cee248ac310fa620c272af1514f9ae951.json} (80%) rename rust/.sqlx/{query-999fe09a6a095ac0ee7b3e3c38a6f2008641e03f9344f31bf9f8eb16a47403da.json => query-4753028574932a6b8d118deb65a930a1592512178477d210acfce662c857cf65.json} (92%) rename rust/.sqlx/{query-343addb4024192688590b6b1228b9fdc554e2bbcc7e7300d8ca04cc4f23087d0.json => query-4878a57b32697b1f18e3d2bf58d34b1ad5d05976eec3432406a3d8ddf92d8b94.json} (90%) rename rust/.sqlx/{query-cc70d7a392a0283fec1896acba805f5c2a527537b8faa22d1c69306017b9c465.json => query-48c563fdbc8ca0dbe14726eda25a18780c6c416eda63b246daa75c5ff318331d.json} (75%) create mode 100644 rust/.sqlx/query-4e24e535cc7c0dc0de572be4295ec187d7ad3742cb85371550bdaed2b780d7f5.json create mode 100644 rust/.sqlx/query-5b1ddabd48e5407dae74a2f01eb2dbc3712ac4c2dcf48b90b6ec4ee8900ec2ef.json rename rust/.sqlx/{query-92ca05be21a4c05bf26f8a1655bbb8d9f1ece322abff5395ecacde3f6e28fdf7.json => query-5d27eb20c0f6c80bec63192b2ab89827bcf2b75fa75af1ce238530377e25d7bd.json} (57%) rename rust/.sqlx/{query-3356a9ab2f217e3daf101644667c9d84f1547e0c72ab779e2f3aebb628a78034.json => query-6aa1ef65325521e8b2655445f8bf227a77ef1a24c8fcf722bf8e49beb8c7ba83.json} (58%) rename rust/.sqlx/{query-16c1a8c70c0778528502dcf1067f5b23d8fc8aaa6d61385b79bac4224a78d703.json => query-6fd3243228d59a7ac2266f0c181a78e7e7a312e101bdd9846123ee4de7c4751a.json} (89%) delete mode 100644 rust/.sqlx/query-82395724cd85d8b57ce50a64f49386435fbebc83f18db53ab8650b29414e40fb.json create mode 100644 rust/.sqlx/query-85c45093fb8d01ea07cb2f9ec94613b8117a437f07e22b0e8204687f8485b872.json create mode 100644 rust/.sqlx/query-a5bedc046d5a4fd900903675794e7c3d8202d3876a26505cd26be7ceeb10d06d.json create mode 100644 rust/.sqlx/query-b22e5d91f70638d4ed3b1592c661e470a0d80493e13478393f357da417a0208b.json rename rust/.sqlx/{query-1eb9a8fdb9412753592f48a9267c97042aeaf6691546efae894e5d34ee92f34b.json => query-b36287d1b8c7905fb5fe6f8e9c73ca6f63b79252471ce4621d8692771e6fd3cc.json} (71%) rename rust/.sqlx/{query-a5cdb8a6d5850326815efd460c0b42dda02a4ea32713ec89beceb38cd24321d5.json => query-baea696b94b2f73ec46c645ed4f7ed355fab4bb37a56b9061bc37f3f053703cb.json} (58%) rename rust/.sqlx/{query-6df16058d577c24a2aaaae71b2c3fd94ddf24e1ced343f3ea20872f0692a9ada.json => query-c1c2a52d3de684ac473206fa348ee792afe635b41eea4d32db30ac9775794520.json} (90%) rename rust/.sqlx/{query-1f08e9bebf51aab9cabff2a5c79211233a686e9ef9f96ea5c036fbba8f6b06d5.json => query-dd3f7d321b59961e4ab8a8c0946de1aff32eae0c645e9c544b48926f7ca16a66.json} (55%) delete mode 100644 rust/.sqlx/query-ded3be1c8894a64e3b5f749461db7261d9224abb8a54da980db8262733d08205.json create mode 100644 rust/migrations/20230820193229_add_user_to_trips.sql create mode 100644 rust/migrations/20230820194457_add_user_to_trips2.sql create mode 100644 rust/migrations/20230820200039_add_user_to_trips_types.sql create mode 100644 rust/query.sql diff --git a/rust/.sqlx/query-0e8a12e168d8ff41005b2b5a1356ef5dc39339291572005e2a9278fb28113a2b.json b/rust/.sqlx/query-0e8a12e168d8ff41005b2b5a1356ef5dc39339291572005e2a9278fb28113a2b.json deleted file mode 100644 index 85d5825..0000000 --- a/rust/.sqlx/query-0e8a12e168d8ff41005b2b5a1356ef5dc39339291572005e2a9278fb28113a2b.json +++ /dev/null @@ -1,12 +0,0 @@ -{ - "db_name": "SQLite", - "query": "INSERT INTO trips_to_trips_types\n (trip_id, trip_type_id)\n VALUES\n (?, ?)\n ", - "describe": { - "columns": [], - "parameters": { - "Right": 2 - }, - "nullable": [] - }, - "hash": "0e8a12e168d8ff41005b2b5a1356ef5dc39339291572005e2a9278fb28113a2b" -} diff --git a/rust/.sqlx/query-86eb9fd598fbbb50b32ea7eae3151bfd8a83639c8b70301f5d5aeef19ae9462c.json b/rust/.sqlx/query-12167f7d6f8b1bfc5f37c214b089a5196039e6845598b8b22852b7665bcd5c0c.json similarity index 61% rename from rust/.sqlx/query-86eb9fd598fbbb50b32ea7eae3151bfd8a83639c8b70301f5d5aeef19ae9462c.json rename to rust/.sqlx/query-12167f7d6f8b1bfc5f37c214b089a5196039e6845598b8b22852b7665bcd5c0c.json index 89eafdc..2775275 100644 --- a/rust/.sqlx/query-86eb9fd598fbbb50b32ea7eae3151bfd8a83639c8b70301f5d5aeef19ae9462c.json +++ b/rust/.sqlx/query-12167f7d6f8b1bfc5f37c214b089a5196039e6845598b8b22852b7665bcd5c0c.json @@ -1,12 +1,12 @@ { "db_name": "SQLite", - "query": "\n INSERT INTO trips_items\n (\n item_id,\n trip_id,\n pick,\n pack,\n ready,\n new\n )\n VALUES (?, ?, ?, ?, ?, ?)\n ", + "query": "\n INSERT INTO trips_items\n (\n item_id,\n trip_id,\n pick,\n pack,\n ready,\n new,\n user_id\n )\n VALUES (?, ?, ?, ?, ?, ?, ?)\n ", "describe": { "columns": [], "parameters": { - "Right": 6 + "Right": 7 }, "nullable": [] }, - "hash": "86eb9fd598fbbb50b32ea7eae3151bfd8a83639c8b70301f5d5aeef19ae9462c" + "hash": "12167f7d6f8b1bfc5f37c214b089a5196039e6845598b8b22852b7665bcd5c0c" } diff --git a/rust/.sqlx/query-1994305e1521fe1f5f927ad28e21c9cab8a25598b19e1c9038dae9092fe18f1f.json b/rust/.sqlx/query-1994305e1521fe1f5f927ad28e21c9cab8a25598b19e1c9038dae9092fe18f1f.json deleted file mode 100644 index 1a01888..0000000 --- a/rust/.sqlx/query-1994305e1521fe1f5f927ad28e21c9cab8a25598b19e1c9038dae9092fe18f1f.json +++ /dev/null @@ -1,20 +0,0 @@ -{ - "db_name": "SQLite", - "query": "\n SELECT\n i_item.id AS item_id\n FROM inventory_items AS i_item\n LEFT JOIN (\n SELECT t_item.item_id as item_id\n FROM trips_items AS t_item\n WHERE t_item.trip_id = ?\n ) AS t_item\n ON t_item.item_id = i_item.id\n WHERE t_item.item_id IS NULL\n ", - "describe": { - "columns": [ - { - "name": "item_id", - "ordinal": 0, - "type_info": "Text" - } - ], - "parameters": { - "Right": 1 - }, - "nullable": [ - false - ] - }, - "hash": "1994305e1521fe1f5f927ad28e21c9cab8a25598b19e1c9038dae9092fe18f1f" -} diff --git a/rust/.sqlx/query-259757f1bb08f0c366371202c75b8555b878290b8a5a68564ec3e8b3d8e7ed01.json b/rust/.sqlx/query-259757f1bb08f0c366371202c75b8555b878290b8a5a68564ec3e8b3d8e7ed01.json deleted file mode 100644 index cca0ddb..0000000 --- a/rust/.sqlx/query-259757f1bb08f0c366371202c75b8555b878290b8a5a68564ec3e8b3d8e7ed01.json +++ /dev/null @@ -1,12 +0,0 @@ -{ - "db_name": "SQLite", - "query": "INSERT INTO trips\n (id, name, date_start, date_end, state)\n VALUES\n (?, ?, ?, ?, ?)", - "describe": { - "columns": [], - "parameters": { - "Right": 5 - }, - "nullable": [] - }, - "hash": "259757f1bb08f0c366371202c75b8555b878290b8a5a68564ec3e8b3d8e7ed01" -} diff --git a/rust/.sqlx/query-a417bf136b8f79339a21fda10518c781d0d2bfc8c3fc99353d1956dfad9f5ac8.json b/rust/.sqlx/query-2793ec31c3345d77a824314bffa8a53cee248ac310fa620c272af1514f9ae951.json similarity index 80% rename from rust/.sqlx/query-a417bf136b8f79339a21fda10518c781d0d2bfc8c3fc99353d1956dfad9f5ac8.json rename to rust/.sqlx/query-2793ec31c3345d77a824314bffa8a53cee248ac310fa620c272af1514f9ae951.json index d2e2742..2876355 100644 --- a/rust/.sqlx/query-a417bf136b8f79339a21fda10518c781d0d2bfc8c3fc99353d1956dfad9f5ac8.json +++ b/rust/.sqlx/query-2793ec31c3345d77a824314bffa8a53cee248ac310fa620c272af1514f9ae951.json @@ -1,6 +1,6 @@ { "db_name": "SQLite", - "query": "\n SELECT\n category.id as category_id,\n category.name as category_name,\n category.description AS category_description,\n inner.trip_id AS trip_id,\n inner.item_id AS item_id,\n inner.item_name AS item_name,\n inner.item_description AS item_description,\n inner.item_weight AS item_weight,\n inner.item_is_picked AS item_is_picked,\n inner.item_is_packed AS item_is_packed,\n inner.item_is_ready AS item_is_ready,\n inner.item_is_new AS item_is_new\n FROM inventory_items_categories AS category\n LEFT JOIN (\n SELECT\n trip.trip_id AS trip_id,\n category.id as category_id,\n category.name as category_name,\n category.description as category_description,\n item.id as item_id,\n item.name as item_name,\n item.description as item_description,\n item.weight as item_weight,\n trip.pick as item_is_picked,\n trip.pack as item_is_packed,\n trip.ready as item_is_ready,\n trip.new as item_is_new\n FROM trips_items as trip\n INNER JOIN inventory_items as item\n ON item.id = trip.item_id\n INNER JOIN inventory_items_categories as category\n ON category.id = item.category_id\n WHERE trip.trip_id = ?\n ) AS inner\n ON inner.category_id = category.id\n ", + "query": "\n SELECT\n category.id as category_id,\n category.name as category_name,\n category.description AS category_description,\n inner.trip_id AS trip_id,\n inner.item_id AS item_id,\n inner.item_name AS item_name,\n inner.item_description AS item_description,\n inner.item_weight AS item_weight,\n inner.item_is_picked AS item_is_picked,\n inner.item_is_packed AS item_is_packed,\n inner.item_is_ready AS item_is_ready,\n inner.item_is_new AS item_is_new\n FROM inventory_items_categories AS category\n LEFT JOIN (\n SELECT\n trip.trip_id AS trip_id,\n category.id as category_id,\n category.name as category_name,\n category.description as category_description,\n item.id as item_id,\n item.name as item_name,\n item.description as item_description,\n item.weight as item_weight,\n trip.pick as item_is_picked,\n trip.pack as item_is_packed,\n trip.ready as item_is_ready,\n trip.new as item_is_new,\n trip.user_id as user_id\n FROM trips_items as trip\n INNER JOIN inventory_items as item\n ON item.id = trip.item_id\n INNER JOIN inventory_items_categories as category\n ON category.id = item.category_id\n WHERE trip.trip_id = ? AND trip.user_id = ?\n ) AS inner\n ON inner.category_id = category.id\n WHERE category.user_id = ?\n ", "describe": { "columns": [ { @@ -65,7 +65,7 @@ } ], "parameters": { - "Right": 1 + "Right": 3 }, "nullable": [ false, @@ -82,5 +82,5 @@ true ] }, - "hash": "a417bf136b8f79339a21fda10518c781d0d2bfc8c3fc99353d1956dfad9f5ac8" + "hash": "2793ec31c3345d77a824314bffa8a53cee248ac310fa620c272af1514f9ae951" } diff --git a/rust/.sqlx/query-999fe09a6a095ac0ee7b3e3c38a6f2008641e03f9344f31bf9f8eb16a47403da.json b/rust/.sqlx/query-4753028574932a6b8d118deb65a930a1592512178477d210acfce662c857cf65.json similarity index 92% rename from rust/.sqlx/query-999fe09a6a095ac0ee7b3e3c38a6f2008641e03f9344f31bf9f8eb16a47403da.json rename to rust/.sqlx/query-4753028574932a6b8d118deb65a930a1592512178477d210acfce662c857cf65.json index 36cc609..f48c44b 100644 --- a/rust/.sqlx/query-999fe09a6a095ac0ee7b3e3c38a6f2008641e03f9344f31bf9f8eb16a47403da.json +++ b/rust/.sqlx/query-4753028574932a6b8d118deb65a930a1592512178477d210acfce662c857cf65.json @@ -1,6 +1,6 @@ { "db_name": "SQLite", - "query": "SELECT\n id,\n name,\n CAST (date_start AS TEXT) date_start,\n CAST (date_end AS TEXT) date_end,\n state,\n location,\n temp_min,\n temp_max,\n comment\n FROM trips\n WHERE id = ?", + "query": "SELECT\n id,\n name,\n CAST (date_start AS TEXT) date_start,\n CAST (date_end AS TEXT) date_end,\n state,\n location,\n temp_min,\n temp_max,\n comment\n FROM trips\n WHERE user_id = ?", "describe": { "columns": [ { @@ -64,5 +64,5 @@ true ] }, - "hash": "999fe09a6a095ac0ee7b3e3c38a6f2008641e03f9344f31bf9f8eb16a47403da" + "hash": "4753028574932a6b8d118deb65a930a1592512178477d210acfce662c857cf65" } diff --git a/rust/.sqlx/query-343addb4024192688590b6b1228b9fdc554e2bbcc7e7300d8ca04cc4f23087d0.json b/rust/.sqlx/query-4878a57b32697b1f18e3d2bf58d34b1ad5d05976eec3432406a3d8ddf92d8b94.json similarity index 90% rename from rust/.sqlx/query-343addb4024192688590b6b1228b9fdc554e2bbcc7e7300d8ca04cc4f23087d0.json rename to rust/.sqlx/query-4878a57b32697b1f18e3d2bf58d34b1ad5d05976eec3432406a3d8ddf92d8b94.json index 65fff01..519fbf1 100644 --- a/rust/.sqlx/query-343addb4024192688590b6b1228b9fdc554e2bbcc7e7300d8ca04cc4f23087d0.json +++ b/rust/.sqlx/query-4878a57b32697b1f18e3d2bf58d34b1ad5d05976eec3432406a3d8ddf92d8b94.json @@ -1,6 +1,6 @@ { "db_name": "SQLite", - "query": "\n SELECT\n category.id as category_id,\n category.name as category_name,\n category.description AS category_description,\n inner.trip_id AS trip_id,\n inner.item_id AS item_id,\n inner.item_name AS item_name,\n inner.item_description AS item_description,\n inner.item_weight AS item_weight,\n inner.item_is_picked AS item_is_picked,\n inner.item_is_packed AS item_is_packed,\n inner.item_is_ready AS item_is_ready,\n inner.item_is_new AS item_is_new\n FROM inventory_items_categories AS category\n LEFT JOIN (\n SELECT\n trip.trip_id AS trip_id,\n category.id as category_id,\n category.name as category_name,\n category.description as category_description,\n item.id as item_id,\n item.name as item_name,\n item.description as item_description,\n item.weight as item_weight,\n trip.pick as item_is_picked,\n trip.pack as item_is_packed,\n trip.ready as item_is_ready,\n trip.new as item_is_new\n FROM trips_items as trip\n INNER JOIN inventory_items as item\n ON item.id = trip.item_id\n INNER JOIN inventory_items_categories as category\n ON category.id = item.category_id\n WHERE trip.trip_id = ?\n ) AS inner\n ON inner.category_id = category.id\n WHERE category.id = ?\n ", + "query": "\n SELECT\n category.id as category_id,\n category.name as category_name,\n category.description AS category_description,\n inner.trip_id AS trip_id,\n inner.item_id AS item_id,\n inner.item_name AS item_name,\n inner.item_description AS item_description,\n inner.item_weight AS item_weight,\n inner.item_is_picked AS item_is_picked,\n inner.item_is_packed AS item_is_packed,\n inner.item_is_ready AS item_is_ready,\n inner.item_is_new AS item_is_new\n FROM inventory_items_categories AS category\n LEFT JOIN (\n SELECT\n trip.trip_id AS trip_id,\n category.id as category_id,\n category.name as category_name,\n category.description as category_description,\n item.id as item_id,\n item.name as item_name,\n item.description as item_description,\n item.weight as item_weight,\n trip.pick as item_is_picked,\n trip.pack as item_is_packed,\n trip.ready as item_is_ready,\n trip.new as item_is_new\n FROM trips_items as trip\n INNER JOIN inventory_items as item\n ON item.id = trip.item_id\n INNER JOIN inventory_items_categories as category\n ON category.id = item.category_id\n WHERE \n trip.trip_id = ?\n AND trip.user_id = ?\n ) AS inner\n ON inner.category_id = category.id\n WHERE category.id = ?\n ", "describe": { "columns": [ { @@ -65,7 +65,7 @@ } ], "parameters": { - "Right": 2 + "Right": 3 }, "nullable": [ false, @@ -82,5 +82,5 @@ true ] }, - "hash": "343addb4024192688590b6b1228b9fdc554e2bbcc7e7300d8ca04cc4f23087d0" + "hash": "4878a57b32697b1f18e3d2bf58d34b1ad5d05976eec3432406a3d8ddf92d8b94" } diff --git a/rust/.sqlx/query-cc70d7a392a0283fec1896acba805f5c2a527537b8faa22d1c69306017b9c465.json b/rust/.sqlx/query-48c563fdbc8ca0dbe14726eda25a18780c6c416eda63b246daa75c5ff318331d.json similarity index 75% rename from rust/.sqlx/query-cc70d7a392a0283fec1896acba805f5c2a527537b8faa22d1c69306017b9c465.json rename to rust/.sqlx/query-48c563fdbc8ca0dbe14726eda25a18780c6c416eda63b246daa75c5ff318331d.json index 9f371ea..d182075 100644 --- a/rust/.sqlx/query-cc70d7a392a0283fec1896acba805f5c2a527537b8faa22d1c69306017b9c465.json +++ b/rust/.sqlx/query-48c563fdbc8ca0dbe14726eda25a18780c6c416eda63b246daa75c5ff318331d.json @@ -1,6 +1,6 @@ { "db_name": "SQLite", - "query": "\n SELECT\n CAST(IFNULL(SUM(i_item.weight), 0) AS INTEGER) AS total_weight\n FROM trips AS trip\n INNER JOIN trips_items AS t_item\n ON t_item.trip_id = trip.id\n INNER JOIN inventory_items AS i_item\n ON t_item.item_id = i_item.id\n WHERE\n trip.id = ?\n AND t_item.pick = true\n ", + "query": "\n SELECT\n CAST(IFNULL(SUM(i_item.weight), 0) AS INTEGER) AS total_weight\n FROM trips AS trip\n INNER JOIN trips_items AS t_item\n ON t_item.trip_id = trip.id\n INNER JOIN inventory_items AS i_item\n ON t_item.item_id = i_item.id\n WHERE\n trip.id = ? AND trip.user_id = ?\n AND t_item.pick = true\n ", "describe": { "columns": [ { @@ -10,11 +10,11 @@ } ], "parameters": { - "Right": 1 + "Right": 2 }, "nullable": [ false ] }, - "hash": "cc70d7a392a0283fec1896acba805f5c2a527537b8faa22d1c69306017b9c465" + "hash": "48c563fdbc8ca0dbe14726eda25a18780c6c416eda63b246daa75c5ff318331d" } diff --git a/rust/.sqlx/query-4e24e535cc7c0dc0de572be4295ec187d7ad3742cb85371550bdaed2b780d7f5.json b/rust/.sqlx/query-4e24e535cc7c0dc0de572be4295ec187d7ad3742cb85371550bdaed2b780d7f5.json new file mode 100644 index 0000000..15746f5 --- /dev/null +++ b/rust/.sqlx/query-4e24e535cc7c0dc0de572be4295ec187d7ad3742cb85371550bdaed2b780d7f5.json @@ -0,0 +1,12 @@ +{ + "db_name": "SQLite", + "query": "INSERT INTO trips_types\n (id, name, user_id)\n VALUES\n (?, ?, ?)", + "describe": { + "columns": [], + "parameters": { + "Right": 3 + }, + "nullable": [] + }, + "hash": "4e24e535cc7c0dc0de572be4295ec187d7ad3742cb85371550bdaed2b780d7f5" +} diff --git a/rust/.sqlx/query-5b1ddabd48e5407dae74a2f01eb2dbc3712ac4c2dcf48b90b6ec4ee8900ec2ef.json b/rust/.sqlx/query-5b1ddabd48e5407dae74a2f01eb2dbc3712ac4c2dcf48b90b6ec4ee8900ec2ef.json new file mode 100644 index 0000000..36523a1 --- /dev/null +++ b/rust/.sqlx/query-5b1ddabd48e5407dae74a2f01eb2dbc3712ac4c2dcf48b90b6ec4ee8900ec2ef.json @@ -0,0 +1,12 @@ +{ + "db_name": "SQLite", + "query": "INSERT INTO\n trips_to_trips_types (trip_id, trip_type_id)\n SELECT trips.id as trip_id, trips_types.id as trip_type_id\n FROM trips\n INNER JOIN trips_types\n WHERE\n trips.id = ?\n AND trips.user_id = ?\n AND trips_types.id = ?\n AND trips_types.user_id = ?", + "describe": { + "columns": [], + "parameters": { + "Right": 4 + }, + "nullable": [] + }, + "hash": "5b1ddabd48e5407dae74a2f01eb2dbc3712ac4c2dcf48b90b6ec4ee8900ec2ef" +} diff --git a/rust/.sqlx/query-92ca05be21a4c05bf26f8a1655bbb8d9f1ece322abff5395ecacde3f6e28fdf7.json b/rust/.sqlx/query-5d27eb20c0f6c80bec63192b2ab89827bcf2b75fa75af1ce238530377e25d7bd.json similarity index 57% rename from rust/.sqlx/query-92ca05be21a4c05bf26f8a1655bbb8d9f1ece322abff5395ecacde3f6e28fdf7.json rename to rust/.sqlx/query-5d27eb20c0f6c80bec63192b2ab89827bcf2b75fa75af1ce238530377e25d7bd.json index 8106f0f..ee61339 100644 --- a/rust/.sqlx/query-92ca05be21a4c05bf26f8a1655bbb8d9f1ece322abff5395ecacde3f6e28fdf7.json +++ b/rust/.sqlx/query-5d27eb20c0f6c80bec63192b2ab89827bcf2b75fa75af1ce238530377e25d7bd.json @@ -1,12 +1,12 @@ { "db_name": "SQLite", - "query": "UPDATE trips_types\n SET name = ?\n WHERE id = ?", + "query": "UPDATE trips_types\n SET name = ?\n WHERE id = ? and user_id = ?", "describe": { "columns": [], "parameters": { - "Right": 2 + "Right": 3 }, "nullable": [] }, - "hash": "92ca05be21a4c05bf26f8a1655bbb8d9f1ece322abff5395ecacde3f6e28fdf7" + "hash": "5d27eb20c0f6c80bec63192b2ab89827bcf2b75fa75af1ce238530377e25d7bd" } diff --git a/rust/.sqlx/query-3356a9ab2f217e3daf101644667c9d84f1547e0c72ab779e2f3aebb628a78034.json b/rust/.sqlx/query-6aa1ef65325521e8b2655445f8bf227a77ef1a24c8fcf722bf8e49beb8c7ba83.json similarity index 58% rename from rust/.sqlx/query-3356a9ab2f217e3daf101644667c9d84f1547e0c72ab779e2f3aebb628a78034.json rename to rust/.sqlx/query-6aa1ef65325521e8b2655445f8bf227a77ef1a24c8fcf722bf8e49beb8c7ba83.json index afe1ff9..ad16800 100644 --- a/rust/.sqlx/query-3356a9ab2f217e3daf101644667c9d84f1547e0c72ab779e2f3aebb628a78034.json +++ b/rust/.sqlx/query-6aa1ef65325521e8b2655445f8bf227a77ef1a24c8fcf722bf8e49beb8c7ba83.json @@ -1,12 +1,12 @@ { "db_name": "SQLite", - "query": "UPDATE trips\n SET state = ?\n WHERE id = ?", + "query": "UPDATE trips\n SET state = ?\n WHERE id = ? and user_id = ?", "describe": { "columns": [], "parameters": { - "Right": 2 + "Right": 3 }, "nullable": [] }, - "hash": "3356a9ab2f217e3daf101644667c9d84f1547e0c72ab779e2f3aebb628a78034" + "hash": "6aa1ef65325521e8b2655445f8bf227a77ef1a24c8fcf722bf8e49beb8c7ba83" } diff --git a/rust/.sqlx/query-16c1a8c70c0778528502dcf1067f5b23d8fc8aaa6d61385b79bac4224a78d703.json b/rust/.sqlx/query-6fd3243228d59a7ac2266f0c181a78e7e7a312e101bdd9846123ee4de7c4751a.json similarity index 89% rename from rust/.sqlx/query-16c1a8c70c0778528502dcf1067f5b23d8fc8aaa6d61385b79bac4224a78d703.json rename to rust/.sqlx/query-6fd3243228d59a7ac2266f0c181a78e7e7a312e101bdd9846123ee4de7c4751a.json index 63c9ae5..e215caf 100644 --- a/rust/.sqlx/query-16c1a8c70c0778528502dcf1067f5b23d8fc8aaa6d61385b79bac4224a78d703.json +++ b/rust/.sqlx/query-6fd3243228d59a7ac2266f0c181a78e7e7a312e101bdd9846123ee4de7c4751a.json @@ -1,6 +1,6 @@ { "db_name": "SQLite", - "query": "\n SELECT\n t_item.item_id AS id,\n t_item.pick AS picked,\n t_item.pack AS packed,\n t_item.ready AS ready,\n t_item.new AS new,\n i_item.name AS name,\n i_item.description AS description,\n i_item.weight AS weight,\n i_item.category_id AS category_id\n FROM trips_items AS t_item\n INNER JOIN inventory_items AS i_item\n ON i_item.id = t_item.item_id\n WHERE t_item.item_id = ?\n AND t_item.trip_id = ?\n ", + "query": "\n SELECT\n t_item.item_id AS id,\n t_item.pick AS picked,\n t_item.pack AS packed,\n t_item.ready AS ready,\n t_item.new AS new,\n i_item.name AS name,\n i_item.description AS description,\n i_item.weight AS weight,\n i_item.category_id AS category_id\n FROM trips_items AS t_item\n INNER JOIN inventory_items AS i_item\n ON i_item.id = t_item.item_id\n WHERE t_item.item_id = ?\n AND t_item.trip_id = ?\n AND t_item.user_id = ?\n ", "describe": { "columns": [ { @@ -50,7 +50,7 @@ } ], "parameters": { - "Right": 2 + "Right": 3 }, "nullable": [ false, @@ -64,5 +64,5 @@ false ] }, - "hash": "16c1a8c70c0778528502dcf1067f5b23d8fc8aaa6d61385b79bac4224a78d703" + "hash": "6fd3243228d59a7ac2266f0c181a78e7e7a312e101bdd9846123ee4de7c4751a" } diff --git a/rust/.sqlx/query-82395724cd85d8b57ce50a64f49386435fbebc83f18db53ab8650b29414e40fb.json b/rust/.sqlx/query-82395724cd85d8b57ce50a64f49386435fbebc83f18db53ab8650b29414e40fb.json deleted file mode 100644 index 908042f..0000000 --- a/rust/.sqlx/query-82395724cd85d8b57ce50a64f49386435fbebc83f18db53ab8650b29414e40fb.json +++ /dev/null @@ -1,12 +0,0 @@ -{ - "db_name": "SQLite", - "query": "DELETE FROM trips_to_trips_types AS ttt\n WHERE ttt.trip_id = ?\n AND ttt.trip_type_id = ?\n ", - "describe": { - "columns": [], - "parameters": { - "Right": 2 - }, - "nullable": [] - }, - "hash": "82395724cd85d8b57ce50a64f49386435fbebc83f18db53ab8650b29414e40fb" -} diff --git a/rust/.sqlx/query-85c45093fb8d01ea07cb2f9ec94613b8117a437f07e22b0e8204687f8485b872.json b/rust/.sqlx/query-85c45093fb8d01ea07cb2f9ec94613b8117a437f07e22b0e8204687f8485b872.json new file mode 100644 index 0000000..dbfabaf --- /dev/null +++ b/rust/.sqlx/query-85c45093fb8d01ea07cb2f9ec94613b8117a437f07e22b0e8204687f8485b872.json @@ -0,0 +1,12 @@ +{ + "db_name": "SQLite", + "query": "INSERT INTO trips\n (id, name, date_start, date_end, state, user_id)\n VALUES\n (?, ?, ?, ?, ?, ?)", + "describe": { + "columns": [], + "parameters": { + "Right": 6 + }, + "nullable": [] + }, + "hash": "85c45093fb8d01ea07cb2f9ec94613b8117a437f07e22b0e8204687f8485b872" +} diff --git a/rust/.sqlx/query-a5bedc046d5a4fd900903675794e7c3d8202d3876a26505cd26be7ceeb10d06d.json b/rust/.sqlx/query-a5bedc046d5a4fd900903675794e7c3d8202d3876a26505cd26be7ceeb10d06d.json new file mode 100644 index 0000000..56e5d11 --- /dev/null +++ b/rust/.sqlx/query-a5bedc046d5a4fd900903675794e7c3d8202d3876a26505cd26be7ceeb10d06d.json @@ -0,0 +1,20 @@ +{ + "db_name": "SQLite", + "query": "\n SELECT\n i_item.id AS item_id\n FROM inventory_items AS i_item\n LEFT JOIN (\n SELECT t_item.item_id AS item_id, t_item.user_id AS user_id\n FROM trips_items AS t_item\n WHERE t_item.trip_id = ? AND t_item.user_id = ?\n ) AS t_item\n ON t_item.item_id = i_item.id\n WHERE t_item.item_id IS NULL AND i_item.user_id = ?", + "describe": { + "columns": [ + { + "name": "item_id", + "ordinal": 0, + "type_info": "Text" + } + ], + "parameters": { + "Right": 3 + }, + "nullable": [ + false + ] + }, + "hash": "a5bedc046d5a4fd900903675794e7c3d8202d3876a26505cd26be7ceeb10d06d" +} diff --git a/rust/.sqlx/query-b22e5d91f70638d4ed3b1592c661e470a0d80493e13478393f357da417a0208b.json b/rust/.sqlx/query-b22e5d91f70638d4ed3b1592c661e470a0d80493e13478393f357da417a0208b.json new file mode 100644 index 0000000..46b634b --- /dev/null +++ b/rust/.sqlx/query-b22e5d91f70638d4ed3b1592c661e470a0d80493e13478393f357da417a0208b.json @@ -0,0 +1,12 @@ +{ + "db_name": "SQLite", + "query": "DELETE FROM trips_to_trips_types AS ttt\n WHERE ttt.trip_id = ?\n AND ttt.trip_type_id = ?\n AND EXISTS(SELECT * FROM trips WHERE id = ? AND user_id = ?)\n AND EXISTS(SELECT * FROM trips_types WHERE id = ? AND user_id = ?)\n ", + "describe": { + "columns": [], + "parameters": { + "Right": 6 + }, + "nullable": [] + }, + "hash": "b22e5d91f70638d4ed3b1592c661e470a0d80493e13478393f357da417a0208b" +} diff --git a/rust/.sqlx/query-1eb9a8fdb9412753592f48a9267c97042aeaf6691546efae894e5d34ee92f34b.json b/rust/.sqlx/query-b36287d1b8c7905fb5fe6f8e9c73ca6f63b79252471ce4621d8692771e6fd3cc.json similarity index 71% rename from rust/.sqlx/query-1eb9a8fdb9412753592f48a9267c97042aeaf6691546efae894e5d34ee92f34b.json rename to rust/.sqlx/query-b36287d1b8c7905fb5fe6f8e9c73ca6f63b79252471ce4621d8692771e6fd3cc.json index 2213893..9cef858 100644 --- a/rust/.sqlx/query-1eb9a8fdb9412753592f48a9267c97042aeaf6691546efae894e5d34ee92f34b.json +++ b/rust/.sqlx/query-b36287d1b8c7905fb5fe6f8e9c73ca6f63b79252471ce4621d8692771e6fd3cc.json @@ -1,6 +1,6 @@ { "db_name": "SQLite", - "query": "SELECT\n id,\n name\n FROM trips_types", + "query": "SELECT\n id,\n name\n FROM trips_types\n WHERE user_id = ?", "describe": { "columns": [ { @@ -15,12 +15,12 @@ } ], "parameters": { - "Right": 0 + "Right": 1 }, "nullable": [ false, false ] }, - "hash": "1eb9a8fdb9412753592f48a9267c97042aeaf6691546efae894e5d34ee92f34b" + "hash": "b36287d1b8c7905fb5fe6f8e9c73ca6f63b79252471ce4621d8692771e6fd3cc" } diff --git a/rust/.sqlx/query-a5cdb8a6d5850326815efd460c0b42dda02a4ea32713ec89beceb38cd24321d5.json b/rust/.sqlx/query-baea696b94b2f73ec46c645ed4f7ed355fab4bb37a56b9061bc37f3f053703cb.json similarity index 58% rename from rust/.sqlx/query-a5cdb8a6d5850326815efd460c0b42dda02a4ea32713ec89beceb38cd24321d5.json rename to rust/.sqlx/query-baea696b94b2f73ec46c645ed4f7ed355fab4bb37a56b9061bc37f3f053703cb.json index 7c4e64f..cba3a67 100644 --- a/rust/.sqlx/query-a5cdb8a6d5850326815efd460c0b42dda02a4ea32713ec89beceb38cd24321d5.json +++ b/rust/.sqlx/query-baea696b94b2f73ec46c645ed4f7ed355fab4bb37a56b9061bc37f3f053703cb.json @@ -1,12 +1,12 @@ { "db_name": "SQLite", - "query": "UPDATE trips\n SET comment = ?\n WHERE id = ?", + "query": "UPDATE trips\n SET comment = ?\n WHERE id = ? AND user_id = ?", "describe": { "columns": [], "parameters": { - "Right": 2 + "Right": 3 }, "nullable": [] }, - "hash": "a5cdb8a6d5850326815efd460c0b42dda02a4ea32713ec89beceb38cd24321d5" + "hash": "baea696b94b2f73ec46c645ed4f7ed355fab4bb37a56b9061bc37f3f053703cb" } diff --git a/rust/.sqlx/query-6df16058d577c24a2aaaae71b2c3fd94ddf24e1ced343f3ea20872f0692a9ada.json b/rust/.sqlx/query-c1c2a52d3de684ac473206fa348ee792afe635b41eea4d32db30ac9775794520.json similarity index 90% rename from rust/.sqlx/query-6df16058d577c24a2aaaae71b2c3fd94ddf24e1ced343f3ea20872f0692a9ada.json rename to rust/.sqlx/query-c1c2a52d3de684ac473206fa348ee792afe635b41eea4d32db30ac9775794520.json index 8c969f6..f5200c1 100644 --- a/rust/.sqlx/query-6df16058d577c24a2aaaae71b2c3fd94ddf24e1ced343f3ea20872f0692a9ada.json +++ b/rust/.sqlx/query-c1c2a52d3de684ac473206fa348ee792afe635b41eea4d32db30ac9775794520.json @@ -1,6 +1,6 @@ { "db_name": "SQLite", - "query": "SELECT\n id,\n name,\n CAST (date_start AS TEXT) date_start,\n CAST (date_end AS TEXT) date_end,\n state,\n location,\n temp_min,\n temp_max,\n comment\n FROM trips", + "query": "SELECT\n id,\n name,\n CAST (date_start AS TEXT) date_start,\n CAST (date_end AS TEXT) date_end,\n state,\n location,\n temp_min,\n temp_max,\n comment\n FROM trips\n WHERE id = ? and user_id = ?", "describe": { "columns": [ { @@ -50,7 +50,7 @@ } ], "parameters": { - "Right": 0 + "Right": 2 }, "nullable": [ false, @@ -64,5 +64,5 @@ true ] }, - "hash": "6df16058d577c24a2aaaae71b2c3fd94ddf24e1ced343f3ea20872f0692a9ada" + "hash": "c1c2a52d3de684ac473206fa348ee792afe635b41eea4d32db30ac9775794520" } diff --git a/rust/.sqlx/query-1f08e9bebf51aab9cabff2a5c79211233a686e9ef9f96ea5c036fbba8f6b06d5.json b/rust/.sqlx/query-dd3f7d321b59961e4ab8a8c0946de1aff32eae0c645e9c544b48926f7ca16a66.json similarity index 55% rename from rust/.sqlx/query-1f08e9bebf51aab9cabff2a5c79211233a686e9ef9f96ea5c036fbba8f6b06d5.json rename to rust/.sqlx/query-dd3f7d321b59961e4ab8a8c0946de1aff32eae0c645e9c544b48926f7ca16a66.json index ed93be3..20159b6 100644 --- a/rust/.sqlx/query-1f08e9bebf51aab9cabff2a5c79211233a686e9ef9f96ea5c036fbba8f6b06d5.json +++ b/rust/.sqlx/query-dd3f7d321b59961e4ab8a8c0946de1aff32eae0c645e9c544b48926f7ca16a66.json @@ -1,6 +1,6 @@ { "db_name": "SQLite", - "query": "\n SELECT\n type.id as id,\n type.name as name,\n inner.id IS NOT NULL AS active\n FROM trips_types AS type\n LEFT JOIN (\n SELECT type.id as id, type.name as name\n FROM trips as trip\n INNER JOIN trips_to_trips_types as ttt\n ON ttt.trip_id = trip.id\n INNER JOIN trips_types AS type\n ON type.id == ttt.trip_type_id\n WHERE trip.id = ?\n ) AS inner\n ON inner.id = type.id\n ", + "query": "\n SELECT\n type.id as id,\n type.name as name,\n inner.id IS NOT NULL AS active\n FROM trips_types AS type\n LEFT JOIN (\n SELECT type.id as id, trip.user_id as user_id\n FROM trips as trip\n INNER JOIN trips_to_trips_types as ttt\n ON ttt.trip_id = trip.id\n INNER JOIN trips_types AS type\n ON type.id == ttt.trip_type_id\n WHERE trip.id = ? AND trip.user_id = ?\n ) AS inner\n ON inner.id = type.id\n WHERE type.user_id = ?\n ", "describe": { "columns": [ { @@ -20,7 +20,7 @@ } ], "parameters": { - "Right": 1 + "Right": 3 }, "nullable": [ false, @@ -28,5 +28,5 @@ false ] }, - "hash": "1f08e9bebf51aab9cabff2a5c79211233a686e9ef9f96ea5c036fbba8f6b06d5" + "hash": "dd3f7d321b59961e4ab8a8c0946de1aff32eae0c645e9c544b48926f7ca16a66" } diff --git a/rust/.sqlx/query-ded3be1c8894a64e3b5f749461db7261d9224abb8a54da980db8262733d08205.json b/rust/.sqlx/query-ded3be1c8894a64e3b5f749461db7261d9224abb8a54da980db8262733d08205.json deleted file mode 100644 index 2b6ed0b..0000000 --- a/rust/.sqlx/query-ded3be1c8894a64e3b5f749461db7261d9224abb8a54da980db8262733d08205.json +++ /dev/null @@ -1,12 +0,0 @@ -{ - "db_name": "SQLite", - "query": "INSERT INTO trips_types\n (id, name)\n VALUES\n (?, ?)", - "describe": { - "columns": [], - "parameters": { - "Right": 2 - }, - "nullable": [] - }, - "hash": "ded3be1c8894a64e3b5f749461db7261d9224abb8a54da980db8262733d08205" -} diff --git a/rust/migrations/20230820193229_add_user_to_trips.sql b/rust/migrations/20230820193229_add_user_to_trips.sql new file mode 100644 index 0000000..7a4120e --- /dev/null +++ b/rust/migrations/20230820193229_add_user_to_trips.sql @@ -0,0 +1,38 @@ +-- CREATE TABLE "trips_tmp" ( +-- id VARCHAR(36) NOT NULL, +-- name TEXT NOT NULL, +-- date_start DATE NOT NULL, +-- date_end DATE NOT NULL, +-- location TEXT, +-- state VARCHAR(8) NOT NULL, +-- user_id VARCHAR(36) NOT NULL, +-- comment TEXT, +-- temp_min INTEGER, +-- temp_max INTEGER, +-- PRIMARY KEY (id), +-- FOREIGN KEY (user_id) REFERENCES users(id), +-- UNIQUE (name) +-- ); + +CREATE TABLE "trips_items_tmp" ( + item_id VARCHAR(36) NOT NULL, + trip_id VARCHAR(36) NOT NULL, + pick BOOLEAN NOT NULL, + pack BOOLEAN NOT NULL, + ready BOOLEAN NOT NULL, + new BOOLEAN NOT NULL, + user_id VARCHAR(36) NOT NULL, + PRIMARY KEY (item_id, trip_id), + FOREIGN KEY(item_id) REFERENCES "inventory_items" (id), + FOREIGN KEY(trip_id) REFERENCES "trips" (id), + FOREIGN KEY (user_id) REFERENCES users(id) +); + +-- INSERT INTO trips_tmp SELECT *, (SELECT id FROM users LIMIT 1) as user_id FROM trips; +INSERT INTO trips_items_tmp SELECT *, (SELECT id FROM users LIMIT 1) as user_id FROM trips_items; + +-- DROP TABLE trips; +DROP TABLE trips_items; + +-- ALTER TABLE trips_tmp RENAME TO trips; +ALTER TABLE trips_items_tmp RENAME TO trips_items; diff --git a/rust/migrations/20230820194457_add_user_to_trips2.sql b/rust/migrations/20230820194457_add_user_to_trips2.sql new file mode 100644 index 0000000..5a55bb3 --- /dev/null +++ b/rust/migrations/20230820194457_add_user_to_trips2.sql @@ -0,0 +1,21 @@ +CREATE TABLE "trips_tmp" ( + id VARCHAR(36) NOT NULL, + name TEXT NOT NULL, + date_start DATE NOT NULL, + date_end DATE NOT NULL, + location TEXT, + state VARCHAR(8) NOT NULL, + comment TEXT, + temp_min INTEGER, + temp_max INTEGER, + user_id VARCHAR(36) NOT NULL, + PRIMARY KEY (id), + UNIQUE (name), + FOREIGN KEY (user_id) REFERENCES users(id) +); + +INSERT INTO trips_tmp SELECT *, (SELECT id FROM users LIMIT 1) as user_id FROM trips; + +DROP TABLE trips; + +ALTER TABLE trips_tmp RENAME TO trips; diff --git a/rust/migrations/20230820200039_add_user_to_trips_types.sql b/rust/migrations/20230820200039_add_user_to_trips_types.sql new file mode 100644 index 0000000..a6a2639 --- /dev/null +++ b/rust/migrations/20230820200039_add_user_to_trips_types.sql @@ -0,0 +1,13 @@ +CREATE TABLE "trips_types_tmp" ( + id VARCHAR(36) NOT NULL, + name TEXT NOT NULL, + user_id VARCHAR(36) NOT NULL, + PRIMARY KEY (id), + UNIQUE (name) +); + +INSERT INTO trips_types_tmp SELECT *, (SELECT id FROM users LIMIT 1) as user_id FROM trips_types; + +DROP TABLE trips_types; + +ALTER TABLE trips_types_tmp RENAME TO trips_types; diff --git a/rust/query.sql b/rust/query.sql new file mode 100644 index 0000000..59d1d52 --- /dev/null +++ b/rust/query.sql @@ -0,0 +1,10 @@ +INSERT INTO + trips_to_trips_types (trip_id, trip_type_id) +SELECT trips.id as trip_id, trips_types.id as trip_type_id + FROM trips + INNER JOIN trips_types + WHERE + trips.id = "629ac022-d715-4dfa-9a53-56ffd0be36f3" + AND trips.user_id = "f981b589-4ed4-42e7-85f0-453961849fc4" + AND trips_types.id = "bdcc58ee-0f09-4fea-ab76-50f3e118228e" + AND trips_types.user_id = "f981b589-4ed4-42e7-85f0-453961849fc4" diff --git a/rust/src/models/trips.rs b/rust/src/models/trips.rs index 695f56a..9acddf6 100644 --- a/rust/src/models/trips.rs +++ b/rust/src/models/trips.rs @@ -6,6 +6,8 @@ use super::{ inventory, }; +use crate::Context; + use futures::{TryFutureExt, TryStreamExt}; use serde::{Deserialize, Serialize}; use serde_variant::to_variant_name; @@ -126,12 +128,14 @@ impl TripCategory { } pub async fn find( + ctx: &Context, pool: &sqlx::Pool, trip_id: Uuid, category_id: Uuid, ) -> Result, Error> { let mut category: Option = None; + let user_id = ctx.user.id.to_string(); let trip_id_param = trip_id.to_string(); let category_id_param = category_id.to_string(); @@ -170,12 +174,15 @@ impl TripCategory { ON item.id = trip.item_id INNER JOIN inventory_items_categories as category ON category.id = item.category_id - WHERE trip.trip_id = ? + WHERE + trip.trip_id = ? + AND trip.user_id = ? ) AS inner ON inner.category_id = category.id WHERE category.id = ? ", trip_id_param, + user_id, category_id_param ) .fetch(pool) @@ -280,10 +287,12 @@ impl TryFrom for TripItem { impl TripItem { pub async fn find( + ctx: &Context, pool: &sqlx::Pool, trip_id: Uuid, item_id: Uuid, ) -> Result, Error> { + let user_id = ctx.user.id.to_string(); let item_id_param = item_id.to_string(); let trip_id_param = trip_id.to_string(); sqlx::query_as!( @@ -304,9 +313,11 @@ impl TripItem { ON i_item.id = t_item.item_id WHERE t_item.item_id = ? AND t_item.trip_id = ? + AND t_item.user_id = ? ", item_id_param, trip_id_param, + user_id, ) .fetch_optional(pool) .await? @@ -315,22 +326,26 @@ impl TripItem { } pub async fn set_state( + ctx: &Context, pool: &sqlx::Pool, trip_id: Uuid, item_id: Uuid, key: TripItemStateKey, value: bool, ) -> Result<(), Error> { + let user_id = ctx.user.id.to_string(); let result = sqlx::query(&format!( "UPDATE trips_items SET {key} = ? WHERE trip_id = ? - AND item_id = ?", + AND item_id = ? + AND user_id = ?", key = to_variant_name(&key).unwrap() )) .bind(value) .bind(trip_id.to_string()) .bind(item_id.to_string()) + .bind(user_id) .execute(pool) .await?; @@ -342,7 +357,7 @@ impl TripItem { } } -pub(crate) struct DbTripRow { +pub struct DbTripRow { pub id: String, pub name: String, pub date_start: String, @@ -409,34 +424,8 @@ pub(crate) struct DbTripWeightRow { } impl Trip { - pub async fn all(pool: &sqlx::Pool) -> Result, Error> { - sqlx::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(pool) - .map_ok(|row| row.try_into()) - .try_collect::>>() - .await? - .into_iter() - .collect::, Error>>() - } - - pub async fn find( - pool: &sqlx::Pool, - trip_id: Uuid, - ) -> Result, Error> { - let trip_id_param = trip_id.to_string(); + pub async fn all(ctx: &Context, pool: &sqlx::Pool) -> Result, Error> { + let user_id = ctx.user.id.to_string(); sqlx::query_as!( DbTripRow, "SELECT @@ -450,8 +439,40 @@ impl Trip { temp_max, comment FROM trips - WHERE id = ?", - trip_id_param + WHERE user_id = ?", + user_id + ) + .fetch(pool) + .map_ok(|row| row.try_into()) + .try_collect::>>() + .await? + .into_iter() + .collect::, Error>>() + } + + pub async fn find( + ctx: &Context, + pool: &sqlx::Pool, + trip_id: Uuid, + ) -> Result, Error> { + let trip_id_param = trip_id.to_string(); + let user_id = ctx.user.id.to_string(); + sqlx::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 + WHERE id = ? and user_id = ?", + trip_id_param, + user_id, ) .fetch_optional(pool) .await? @@ -460,10 +481,12 @@ impl Trip { } pub async fn trip_type_remove( + ctx: &Context, pool: &sqlx::Pool, id: Uuid, type_id: Uuid, ) -> Result { + let user_id = ctx.user.id.to_string(); let id_param = id.to_string(); let type_id_param = type_id.to_string(); @@ -471,9 +494,15 @@ impl Trip { "DELETE FROM trips_to_trips_types AS ttt WHERE ttt.trip_id = ? AND ttt.trip_type_id = ? + AND EXISTS(SELECT * FROM trips WHERE id = ? AND user_id = ?) + AND EXISTS(SELECT * FROM trips_types WHERE id = ? AND user_id = ?) ", id_param, type_id_param, + id_param, + user_id, + type_id_param, + user_id, ) .execute(pool) .await?; @@ -482,20 +511,31 @@ impl Trip { } pub async fn trip_type_add( + ctx: &Context, pool: &sqlx::Pool, id: Uuid, type_id: Uuid, ) -> Result<(), Error> { + let user_id = ctx.user.id.to_string(); + // TODO user handling? let trip_id_param = id.to_string(); let type_id_param = type_id.to_string(); + sqlx::query!( - "INSERT INTO trips_to_trips_types - (trip_id, trip_type_id) - VALUES - (?, ?) - ", + "INSERT INTO + trips_to_trips_types (trip_id, trip_type_id) + SELECT trips.id as trip_id, trips_types.id as trip_type_id + FROM trips + INNER JOIN trips_types + WHERE + trips.id = ? + AND trips.user_id = ? + AND trips_types.id = ? + AND trips_types.user_id = ?", trip_id_param, + user_id, type_id_param, + user_id, ) .execute(pool) .await?; @@ -504,17 +544,20 @@ impl Trip { } pub async fn set_state( + ctx: &Context, pool: &sqlx::Pool, id: Uuid, new_state: &TripState, ) -> Result { + let user_id = ctx.user.id.to_string(); let trip_id_param = id.to_string(); let result = sqlx::query!( "UPDATE trips SET state = ? - WHERE id = ?", + WHERE id = ? and user_id = ?", new_state, trip_id_param, + user_id, ) .execute(pool) .await?; @@ -523,17 +566,20 @@ impl Trip { } pub async fn set_comment( + ctx: &Context, pool: &sqlx::Pool, id: Uuid, new_comment: &str, ) -> Result { + let user_id = ctx.user.id.to_string(); let trip_id_param = id.to_string(); let result = sqlx::query!( "UPDATE trips SET comment = ? - WHERE id = ?", + WHERE id = ? AND user_id = ?", new_comment, trip_id_param, + user_id, ) .execute(pool) .await?; @@ -542,19 +588,22 @@ impl Trip { } pub async fn set_attribute( + ctx: &Context, pool: &sqlx::Pool, trip_id: Uuid, attribute: TripAttribute, value: &str, ) -> Result<(), Error> { + let user_id = ctx.user.id.to_string(); let result = sqlx::query(&format!( "UPDATE trips SET {attribute} = ? - WHERE id = ?", + WHERE id = ? AND user_id = ?", attribute = to_variant_name(&attribute).unwrap() )) .bind(value) .bind(trip_id.to_string()) + .bind(user_id) .execute(pool) .await?; @@ -566,11 +615,13 @@ impl Trip { } pub async fn save( + ctx: &Context, pool: &sqlx::Pool, name: &str, date_start: time::Date, date_end: time::Date, ) -> Result { + let user_id = ctx.user.id.to_string(); let id = Uuid::new_v4(); let id_param = id.to_string(); let date_start = date_start.format(consts::DATE_FORMAT)?; @@ -580,14 +631,15 @@ impl Trip { sqlx::query!( "INSERT INTO trips - (id, name, date_start, date_end, state) + (id, name, date_start, date_end, state, user_id) VALUES - (?, ?, ?, ?, ?)", + (?, ?, ?, ?, ?, ?)", id_param, name, date_start, date_end, trip_state, + user_id, ) .execute(pool) .await?; @@ -596,9 +648,11 @@ impl Trip { } pub async fn find_total_picked_weight( + ctx: &Context, pool: &sqlx::Pool, trip_id: Uuid, ) -> Result { + let user_id = ctx.user.id.to_string(); let trip_id_param = trip_id.to_string(); let weight = sqlx::query_as!( DbTripWeightRow, @@ -611,10 +665,11 @@ impl Trip { INNER JOIN inventory_items AS i_item ON t_item.item_id = i_item.id WHERE - trip.id = ? + trip.id = ? AND trip.user_id = ? AND t_item.pick = true ", - trip_id_param + trip_id_param, + user_id, ) .fetch_one(pool) .map_ok(|row| row.total_weight.unwrap() as i64) @@ -650,7 +705,12 @@ impl Trip { .sum::() } - pub async fn load_trips_types(&mut self, pool: &sqlx::Pool) -> Result<(), Error> { + pub async fn load_trips_types( + &mut self, + ctx: &Context, + pool: &sqlx::Pool, + ) -> Result<(), Error> { + let user_id = ctx.user.id.to_string(); let id = self.id.to_string(); let types = sqlx::query!( " @@ -660,17 +720,20 @@ impl Trip { inner.id IS NOT NULL AS active FROM trips_types AS type LEFT JOIN ( - SELECT type.id as id, type.name as name + SELECT type.id as id, trip.user_id as user_id FROM trips as trip INNER JOIN trips_to_trips_types as ttt ON ttt.trip_id = trip.id INNER JOIN trips_types AS type ON type.id == ttt.trip_type_id - WHERE trip.id = ? + WHERE trip.id = ? AND trip.user_id = ? ) AS inner ON inner.id = type.id + WHERE type.user_id = ? ", - id + id, + user_id, + user_id, ) .fetch(pool) .map_ok(|row| -> Result { @@ -695,6 +758,7 @@ impl Trip { pub async fn sync_trip_items_with_inventory( &mut self, + ctx: &Context, pool: &sqlx::Pool, ) -> Result<(), Error> { // we need to get all items that are part of the inventory but not @@ -708,6 +772,7 @@ impl Trip { // * if the trip is new, we have to make these new items prominently // visible so the user knows that there might be new items to // consider + let user_id = ctx.user.id.to_string(); let trip_id = self.id.to_string(); let unsynced_items: Vec = sqlx::query!( " @@ -715,14 +780,15 @@ impl Trip { i_item.id AS item_id FROM inventory_items AS i_item LEFT JOIN ( - SELECT t_item.item_id as item_id + SELECT t_item.item_id AS item_id, t_item.user_id AS user_id FROM trips_items AS t_item - WHERE t_item.trip_id = ? + WHERE t_item.trip_id = ? AND t_item.user_id = ? ) AS t_item ON t_item.item_id = i_item.id - WHERE t_item.item_id IS NULL - ", - trip_id + WHERE t_item.item_id IS NULL AND i_item.user_id = ?", + trip_id, + user_id, + user_id, ) .fetch(pool) .map_ok(|row| -> Result { Ok(Uuid::try_parse(&row.item_id)?) }) @@ -748,9 +814,10 @@ impl Trip { pick, pack, ready, - new + new, + user_id ) - VALUES (?, ?, ?, ?, ?, ?) + VALUES (?, ?, ?, ?, ?, ?, ?) ", item_id, trip_id, @@ -758,6 +825,7 @@ impl Trip { false, false, mark_as_new, + user_id, ) .execute(pool) .await?; @@ -768,11 +836,16 @@ impl Trip { Ok(()) } - pub async fn load_categories(&mut self, pool: &sqlx::Pool) -> Result<(), Error> { + pub async fn load_categories( + &mut self, + ctx: &Context, + pool: &sqlx::Pool, + ) -> Result<(), Error> { let mut categories: Vec = vec![]; // we can ignore the return type as we collect into `categories` // in the `map_ok()` closure let id = self.id.to_string(); + let user_id = ctx.user.id.to_string(); sqlx::query!( " SELECT @@ -802,17 +875,21 @@ impl Trip { trip.pick as item_is_picked, trip.pack as item_is_packed, trip.ready as item_is_ready, - trip.new as item_is_new + trip.new as item_is_new, + trip.user_id as user_id FROM trips_items as trip INNER JOIN inventory_items as item ON item.id = trip.item_id INNER JOIN inventory_items_categories as category ON category.id = item.category_id - WHERE trip.trip_id = ? + WHERE trip.trip_id = ? AND trip.user_id = ? ) AS inner ON inner.category_id = category.id + WHERE category.user_id = ? ", - id + id, + user_id, + user_id, ) .fetch(pool) .map_ok(|row| -> Result<(), Error> { @@ -883,13 +960,16 @@ pub struct TripType { } impl TripsType { - pub async fn all(pool: &sqlx::Pool) -> Result, Error> { + pub async fn all(ctx: &Context, pool: &sqlx::Pool) -> Result, Error> { + let user_id = ctx.user.id.to_string(); sqlx::query_as!( DbTripsTypesRow, "SELECT id, name - FROM trips_types", + FROM trips_types + WHERE user_id = ?", + user_id, ) .fetch(pool) .map_ok(|row| row.try_into()) @@ -899,16 +979,22 @@ impl TripsType { .collect::, Error>>() } - pub async fn save(pool: &sqlx::Pool, name: &str) -> Result { + pub async fn save( + ctx: &Context, + pool: &sqlx::Pool, + name: &str, + ) -> Result { + let user_id = ctx.user.id.to_string(); let id = Uuid::new_v4(); let id_param = id.to_string(); sqlx::query!( "INSERT INTO trips_types - (id, name) + (id, name, user_id) VALUES - (?, ?)", + (?, ?, ?)", id_param, name, + user_id, ) .execute(pool) .await?; @@ -917,18 +1003,21 @@ impl TripsType { } pub async fn set_name( + ctx: &Context, pool: &sqlx::Pool, id: Uuid, new_name: &str, ) -> Result { + let user_id = ctx.user.id.to_string(); let id_param = id.to_string(); let result = sqlx::query!( "UPDATE trips_types SET name = ? - WHERE id = ?", + WHERE id = ? and user_id = ?", new_name, id_param, + user_id, ) .execute(pool) .await?; diff --git a/rust/src/routing/routes.rs b/rust/src/routing/routes.rs index 2618d1b..71c2f04 100644 --- a/rust/src/routing/routes.rs +++ b/rust/src/routing/routes.rs @@ -308,9 +308,11 @@ pub async fn inventory_item_cancel( } pub async fn trip_create( + Extension(current_user): Extension, State(state): State, Form(new_trip): Form, ) -> Result { + let ctx = Context::build(current_user); if new_trip.name.is_empty() { return Err(Error::Request(RequestError::EmptyFormElement { name: "name".to_string(), @@ -318,6 +320,7 @@ pub async fn trip_create( } let new_id = models::trips::Trip::save( + &ctx, &state.database_pool, &new_trip.name, new_trip.date_start, @@ -332,10 +335,11 @@ pub async fn trips( Extension(current_user): Extension, State(state): State, ) -> Result { - let trips = models::trips::Trip::all(&state.database_pool).await?; + let ctx = Context::build(current_user); + let trips = models::trips::Trip::all(&ctx, &state.database_pool).await?; Ok(view::Root::build( - &Context::build(current_user), + &ctx, &view::trip::TripManager::build(trips), Some(&TopLevelPage::Trips), )) @@ -347,21 +351,22 @@ pub async fn trip( Path(id): Path, Query(trip_query): Query, ) -> Result { + let ctx = Context::build(current_user); state.client_state.trip_edit_attribute = trip_query.edit; state.client_state.active_category_id = trip_query.category; - let mut trip: models::trips::Trip = models::trips::Trip::find(&state.database_pool, id) + let mut trip: models::trips::Trip = models::trips::Trip::find(&ctx, &state.database_pool, id) .await? .ok_or(Error::Request(RequestError::NotFound { message: format!("trip with id {id} not found"), }))?; - trip.load_trips_types(&state.database_pool).await?; + trip.load_trips_types(&ctx, &state.database_pool).await?; - trip.sync_trip_items_with_inventory(&state.database_pool) + trip.sync_trip_items_with_inventory(&ctx, &state.database_pool) .await?; - trip.load_categories(&state.database_pool).await?; + trip.load_categories(&ctx, &state.database_pool).await?; let active_category: Option<&models::trips::TripCategory> = state .client_state @@ -377,7 +382,7 @@ pub async fn trip( .transpose()?; Ok(view::Root::build( - &Context::build(current_user), + &ctx, &view::trip::Trip::build( &trip, state.client_state.trip_edit_attribute, @@ -388,11 +393,13 @@ pub async fn trip( } pub async fn trip_type_remove( + Extension(current_user): Extension, State(state): State, Path((trip_id, type_id)): Path<(Uuid, Uuid)>, ) -> Result { + let ctx = Context::build(current_user); let found = - models::trips::Trip::trip_type_remove(&state.database_pool, trip_id, type_id).await?; + models::trips::Trip::trip_type_remove(&ctx, &state.database_pool, trip_id, type_id).await?; if !found { Err(Error::Request(RequestError::NotFound { @@ -404,20 +411,25 @@ pub async fn trip_type_remove( } pub async fn trip_type_add( + Extension(current_user): Extension, State(state): State, Path((trip_id, type_id)): Path<(Uuid, Uuid)>, ) -> Result { - models::trips::Trip::trip_type_add(&state.database_pool, trip_id, type_id).await?; + let ctx = Context::build(current_user); + models::trips::Trip::trip_type_add(&ctx, &state.database_pool, trip_id, type_id).await?; Ok(Redirect::to(&format!("/trips/{trip_id}/"))) } pub async fn trip_comment_set( + Extension(current_user): Extension, State(state): State, Path(trip_id): Path, Form(comment_update): Form, ) -> Result { + let ctx = Context::build(current_user); let found = models::trips::Trip::set_comment( + &ctx, &state.database_pool, trip_id, &comment_update.new_comment, @@ -434,10 +446,12 @@ pub async fn trip_comment_set( } pub async fn trip_edit_attribute( + Extension(current_user): Extension, State(state): State, Path((trip_id, attribute)): Path<(Uuid, models::trips::TripAttribute)>, Form(trip_update): Form, ) -> Result { + let ctx = Context::build(current_user); if attribute == models::trips::TripAttribute::Name { if trip_update.new_value.is_empty() { return Err(Error::Request(RequestError::EmptyFormElement { @@ -446,6 +460,7 @@ pub async fn trip_edit_attribute( } } models::trips::Trip::set_attribute( + &ctx, &state.database_pool, trip_id, attribute, @@ -457,13 +472,15 @@ pub async fn trip_edit_attribute( } pub async fn trip_item_set_state( + ctx: &Context, state: &AppState, trip_id: Uuid, item_id: Uuid, key: models::trips::TripItemStateKey, value: bool, ) -> Result<(), Error> { - models::trips::TripItem::set_state(&state.database_pool, trip_id, item_id, key, value).await?; + models::trips::TripItem::set_state(&ctx, &state.database_pool, trip_id, item_id, key, value) + .await?; Ok(()) } @@ -473,7 +490,7 @@ pub async fn trip_row( trip_id: Uuid, item_id: Uuid, ) -> Result { - let item = models::trips::TripItem::find(&state.database_pool, trip_id, item_id) + let item = models::trips::TripItem::find(ctx, &state.database_pool, trip_id, item_id) .await? .ok_or_else(|| { Error::Request(RequestError::NotFound { @@ -492,14 +509,18 @@ pub async fn trip_row( .await?, ); - let category = - models::trips::TripCategory::find(&state.database_pool, trip_id, item.item.category_id) - .await? - .ok_or_else(|| { - Error::Request(RequestError::NotFound { - message: format!("category with id {} not found", item.item.category_id), - }) - })?; + let category = models::trips::TripCategory::find( + ctx, + &state.database_pool, + trip_id, + item.item.category_id, + ) + .await? + .ok_or_else(|| { + Error::Request(RequestError::NotFound { + message: format!("category with id {} not found", item.item.category_id), + }) + })?; // TODO biggest_category_weight? let category_row = view::trip::TripCategoryListRow::build(trip_id, &category, true, 0, true); @@ -508,12 +529,15 @@ pub async fn trip_row( } pub async fn trip_item_set_pick( + Extension(current_user): Extension, State(state): State, Path((trip_id, item_id)): Path<(Uuid, Uuid)>, headers: HeaderMap, ) -> Result { + let ctx = Context::build(current_user); Ok::<_, Error>( trip_item_set_state( + &ctx, &state, trip_id, item_id, @@ -532,6 +556,7 @@ pub async fn trip_item_set_pick_htmx( ) -> Result { let ctx = Context::build(current_user); trip_item_set_state( + &ctx, &state, trip_id, item_id, @@ -548,12 +573,15 @@ pub async fn trip_item_set_pick_htmx( } pub async fn trip_item_set_unpick( + Extension(current_user): Extension, State(state): State, Path((trip_id, item_id)): Path<(Uuid, Uuid)>, headers: HeaderMap, ) -> Result { + let ctx = Context::build(current_user); Ok::<_, Error>( trip_item_set_state( + &ctx, &state, trip_id, item_id, @@ -572,6 +600,7 @@ pub async fn trip_item_set_unpick_htmx( ) -> Result { let ctx = Context::build(current_user); trip_item_set_state( + &ctx, &state, trip_id, item_id, @@ -588,12 +617,15 @@ pub async fn trip_item_set_unpick_htmx( } pub async fn trip_item_set_pack( + Extension(current_user): Extension, State(state): State, Path((trip_id, item_id)): Path<(Uuid, Uuid)>, headers: HeaderMap, ) -> Result { + let ctx = Context::build(current_user); Ok::<_, Error>( trip_item_set_state( + &ctx, &state, trip_id, item_id, @@ -612,6 +644,7 @@ pub async fn trip_item_set_pack_htmx( ) -> Result { let ctx = Context::build(current_user); trip_item_set_state( + &ctx, &state, trip_id, item_id, @@ -628,12 +661,15 @@ pub async fn trip_item_set_pack_htmx( } pub async fn trip_item_set_unpack( + Extension(current_user): Extension, State(state): State, Path((trip_id, item_id)): Path<(Uuid, Uuid)>, headers: HeaderMap, ) -> Result { + let ctx = Context::build(current_user); Ok::<_, Error>( trip_item_set_state( + &ctx, &state, trip_id, item_id, @@ -652,6 +688,7 @@ pub async fn trip_item_set_unpack_htmx( ) -> Result { let ctx = Context::build(current_user); trip_item_set_state( + &ctx, &state, trip_id, item_id, @@ -668,12 +705,15 @@ pub async fn trip_item_set_unpack_htmx( } pub async fn trip_item_set_ready( + Extension(current_user): Extension, State(state): State, Path((trip_id, item_id)): Path<(Uuid, Uuid)>, headers: HeaderMap, ) -> Result { + let ctx = Context::build(current_user); Ok::<_, Error>( trip_item_set_state( + &ctx, &state, trip_id, item_id, @@ -692,6 +732,7 @@ pub async fn trip_item_set_ready_htmx( ) -> Result { let ctx = Context::build(current_user); trip_item_set_state( + &ctx, &state, trip_id, item_id, @@ -708,12 +749,15 @@ pub async fn trip_item_set_ready_htmx( } pub async fn trip_item_set_unready( + Extension(current_user): Extension, State(state): State, Path((trip_id, item_id)): Path<(Uuid, Uuid)>, headers: HeaderMap, ) -> Result { + let ctx = Context::build(current_user); Ok::<_, Error>( trip_item_set_state( + &ctx, &state, trip_id, item_id, @@ -732,6 +776,7 @@ pub async fn trip_item_set_unready_htmx( ) -> Result { let ctx = Context::build(current_user); trip_item_set_state( + &ctx, &state, trip_id, item_id, @@ -748,11 +793,13 @@ pub async fn trip_item_set_unready_htmx( } pub async fn trip_total_weight_htmx( + Extension(current_user): Extension, State(state): State, Path(trip_id): Path, ) -> Result { + let ctx = Context::build(current_user); let total_weight = - models::trips::Trip::find_total_picked_weight(&state.database_pool, trip_id).await?; + models::trips::Trip::find_total_picked_weight(&ctx, &state.database_pool, trip_id).await?; Ok(view::trip::TripInfoTotalWeightRow::build( trip_id, total_weight, @@ -778,11 +825,14 @@ pub async fn inventory_category_create( } pub async fn trip_state_set( + Extension(current_user): Extension, State(state): State, headers: HeaderMap, Path((trip_id, new_state)): Path<(Uuid, models::trips::TripState)>, ) -> Result { - let exists = models::trips::Trip::set_state(&state.database_pool, trip_id, &new_state).await?; + let ctx = Context::build(current_user); + let exists = + models::trips::Trip::set_state(&ctx, &state.database_pool, trip_id, &new_state).await?; if !exists { return Err(Error::Request(RequestError::NotFound { @@ -801,36 +851,42 @@ pub async fn trips_types( State(mut state): State, Query(trip_type_query): Query, ) -> Result { + let ctx = Context::build(current_user); state.client_state.trip_type_edit = trip_type_query.edit; let trip_types: Vec = - models::trips::TripsType::all(&state.database_pool).await?; + models::trips::TripsType::all(&ctx, &state.database_pool).await?; Ok(view::Root::build( - &Context::build(current_user), + &ctx, &view::trip::types::TypeList::build(&state.client_state, trip_types), Some(&TopLevelPage::Trips), )) } pub async fn trip_type_create( + Extension(current_user): Extension, State(state): State, Form(new_trip_type): Form, ) -> Result { + let ctx = Context::build(current_user); if new_trip_type.name.is_empty() { return Err(Error::Request(RequestError::EmptyFormElement { name: "name".to_string(), })); } - let _new_id = models::trips::TripsType::save(&state.database_pool, &new_trip_type.name).await?; + let _new_id = + models::trips::TripsType::save(&ctx, &state.database_pool, &new_trip_type.name).await?; Ok(Redirect::to("/trips/types/")) } pub async fn trips_types_edit_name( + Extension(current_user): Extension, State(state): State, Path(trip_type_id): Path, Form(trip_update): Form, ) -> Result { + let ctx = Context::build(current_user); if trip_update.new_value.is_empty() { return Err(Error::Request(RequestError::EmptyFormElement { name: "name".to_string(), @@ -838,6 +894,7 @@ pub async fn trips_types_edit_name( } let exists = models::trips::TripsType::set_name( + &ctx, &state.database_pool, trip_type_id, &trip_update.new_value, @@ -873,16 +930,18 @@ pub async fn inventory_item( } pub async fn trip_category_select( + Extension(current_user): Extension, State(state): State, Path((trip_id, category_id)): Path<(Uuid, Uuid)>, ) -> Result { - let mut trip = models::trips::Trip::find(&state.database_pool, trip_id) + let ctx = Context::build(current_user); + let mut trip = models::trips::Trip::find(&ctx, &state.database_pool, trip_id) .await? .ok_or(Error::Request(RequestError::NotFound { message: format!("trip with id {trip_id} not found"), }))?; - trip.load_categories(&state.database_pool).await?; + trip.load_categories(&ctx, &state.database_pool).await?; let active_category = trip .categories() @@ -945,26 +1004,30 @@ pub async fn trip_packagelist( State(state): State, Path(trip_id): Path, ) -> Result { - let mut trip = models::trips::Trip::find(&state.database_pool, trip_id) + let ctx = Context::build(current_user); + let mut trip = models::trips::Trip::find(&ctx, &state.database_pool, trip_id) .await? .ok_or(Error::Request(RequestError::NotFound { message: format!("trip with id {trip_id} not found"), }))?; - trip.load_categories(&state.database_pool).await?; + trip.load_categories(&ctx, &state.database_pool).await?; Ok(view::Root::build( - &Context::build(current_user), + &ctx, &view::trip::packagelist::TripPackageList::build(&trip), Some(&TopLevelPage::Trips), )) } pub async fn trip_item_packagelist_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( + &ctx, &state, trip_id, item_id, @@ -973,7 +1036,7 @@ pub async fn trip_item_packagelist_set_pack_htmx( ) .await?; - let item = models::trips::TripItem::find(&state.database_pool, trip_id, item_id) + let item = models::trips::TripItem::find(&ctx, &state.database_pool, trip_id, item_id) .await? .ok_or(Error::Request(RequestError::NotFound { message: format!("an item with id {item_id} does not exist"), @@ -985,10 +1048,13 @@ pub async fn trip_item_packagelist_set_pack_htmx( } pub async fn trip_item_packagelist_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( + &ctx, &state, trip_id, item_id, @@ -999,7 +1065,7 @@ pub async fn trip_item_packagelist_set_unpack_htmx( // note that this cannot fail due to a missing item, as trip_item_set_state would already // return 404. but error handling cannot hurt ;) - let item = models::trips::TripItem::find(&state.database_pool, trip_id, item_id) + let item = models::trips::TripItem::find(&ctx, &state.database_pool, trip_id, item_id) .await? .ok_or(Error::Request(RequestError::NotFound { message: format!("an item with id {item_id} does not exist"), @@ -1011,10 +1077,13 @@ pub async fn trip_item_packagelist_set_unpack_htmx( } pub async fn trip_item_packagelist_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( + &ctx, &state, trip_id, item_id, @@ -1023,7 +1092,7 @@ pub async fn trip_item_packagelist_set_ready_htmx( ) .await?; - let item = models::trips::TripItem::find(&state.database_pool, trip_id, item_id) + let item = models::trips::TripItem::find(&ctx, &state.database_pool, trip_id, item_id) .await? .ok_or(Error::Request(RequestError::NotFound { message: format!("an item with id {item_id} does not exist"), @@ -1035,10 +1104,13 @@ pub async fn trip_item_packagelist_set_ready_htmx( } pub async fn trip_item_packagelist_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( + &ctx, &state, trip_id, item_id, @@ -1049,7 +1121,7 @@ pub async fn trip_item_packagelist_set_unready_htmx( // note that this cannot fail due to a missing item, as trip_item_set_state would already // return 404. but error handling cannot hurt ;) - let item = models::trips::TripItem::find(&state.database_pool, trip_id, item_id) + let item = models::trips::TripItem::find(&ctx, &state.database_pool, trip_id, item_id) .await? .ok_or(Error::Request(RequestError::NotFound { message: format!("an item with id {item_id} does not exist"),