From 261606fc66894b1156d039a2c687276a95e7f47a Mon Sep 17 00:00:00 2001 From: isiko Date: Wed, 29 May 2024 17:23:16 +0200 Subject: [PATCH] Add Update Methods for Shopping Tours --- foodlib/src/events.rs | 30 ++++++++++++++++++++++++++++-- foodlib/src/ingredients.rs | 28 ++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 2 deletions(-) diff --git a/foodlib/src/events.rs b/foodlib/src/events.rs index 1066631..f5ad099 100644 --- a/foodlib/src/events.rs +++ b/foodlib/src/events.rs @@ -320,7 +320,20 @@ impl FoodBase { tour_id: i32, date: NaiveDateTime, ) -> eyre::Result { - todo!() + let result = sqlx::query_as!( + ShoppingTour, + r#" + UPDATE shopping_tours + SET tour_date = $2 + WHERE tour_id = $1 + RETURNING * + "#, + tour_id, + date, + ) + .fetch_one(&*self.pg_pool) + .await?; + Ok(result) } pub async fn update_event_shopping_tour_store( @@ -328,7 +341,20 @@ impl FoodBase { tour_id: i32, store_id: i32, ) -> eyre::Result { - todo!() + let result = sqlx::query_as!( + ShoppingTour, + r#" + UPDATE shopping_tours + SET store_id = $2 + WHERE tour_id = $1 + RETURNING * + "#, + tour_id, + store_id + ) + .fetch_one(&*self.pg_pool) + .await?; + Ok(result) } pub async fn get_event_food_prep(&self, event_id: i32) -> eyre::Result> { diff --git a/foodlib/src/ingredients.rs b/foodlib/src/ingredients.rs index 1e9691b..bb4210f 100644 --- a/foodlib/src/ingredients.rs +++ b/foodlib/src/ingredients.rs @@ -596,6 +596,34 @@ impl FoodBase { .await .map_err(Into::into) } + + pub async fn get_store_by_ref(&self, store_ref: String) -> eyre::Result { + if let Ok(store_id) = store_ref.parse::() { + let result = sqlx::query_as!( + Store, + r#" + SELECT * FROM stores + WHERE store_id = $1 + "#, + store_id + ) + .fetch_one(&*self.pg_pool) + .await; + Ok(result?) + } else { + let result = sqlx::query_as!( + Store, + r#" + SELECT * FROM stores + WHERE name = $1 + "#, + store_ref + ) + .fetch_one(&*self.pg_pool) + .await; + Ok(result?) + } + } } #[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] pub struct Store {