Skip to content

Commit

Permalink
Add new Foodlib functions for new Tables
Browse files Browse the repository at this point in the history
  • Loading branch information
isiko committed May 29, 2024
1 parent ed7353f commit eebac3c
Showing 1 changed file with 146 additions and 0 deletions.
146 changes: 146 additions & 0 deletions foodlib/src/events.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use chrono::NaiveDateTime;
use core::fmt::Display;
use serde::{Deserialize, Serialize};
use sqlx::postgres::types::PgMoney;
Expand Down Expand Up @@ -61,6 +62,31 @@ impl Display for Place {
}
}

#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize, Tabled)]
pub struct FoodPrep {
pub prep_id: i32,
pub event_id: i32,
pub recipe_id: i32,
pub prep_date: NaiveDateTime,
#[tabled(display_with = "crate::util::display_optional")]
pub use_from: Option<NaiveDateTime>,
pub use_until: NaiveDateTime,
}

#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize, Tabled)]
pub struct ShoppingTour {
pub tour_id: i32,
pub event_id: i32,
pub tour_date: NaiveDateTime,
pub store_id: i32,
}

#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize, Tabled)]
pub struct SourceOverride {
event_id: i32,
ingredient_source_id: i32,
}

impl FoodBase {
//TODO Merge this with function below, as this currently excludes events without meals
pub async fn get_events(&self) -> eyre::Result<Vec<Event>> {
Expand Down Expand Up @@ -255,4 +281,124 @@ impl FoodBase {
.await?;
Ok(records.price.unwrap_or(PgMoney(0)))
}

pub async fn get_event_shopping_tours(&self, event_id: i32) -> eyre::Result<Vec<ShoppingTour>> {
let records = sqlx::query_as!(
ShoppingTour,
"SELECT * FROM shopping_tours WHERE event_id=$1",
event_id
)
.fetch_all(&*self.pg_pool)
.await?;
Ok(records)
}

pub async fn add_event_shopping_tour(
&self,
event_id: i32,
store_id: i32,
date: NaiveDateTime,
) -> eyre::Result<ShoppingTour> {
let tour = sqlx::query_as!(
ShoppingTour,
r#"
INSERT INTO public.shopping_tours (tour_id, event_id, tour_date, store_id)
VALUES (DEFAULT, $1, $2, $3)
RETURNING *
"#,
event_id,
date,
store_id
)
.fetch_one(&*self.pg_pool)
.await?;
Ok(tour)
}

pub async fn update_event_shopping_tour_date(
&self,
tour_id: i32,
date: NaiveDateTime,
) -> eyre::Result<ShoppingTour> {
todo!()
}

pub async fn update_event_shopping_tour_store(
&self,
tour_id: i32,
store_id: i32,
) -> eyre::Result<ShoppingTour> {
todo!()
}

pub async fn get_event_food_prep(&self, event_id: i32) -> eyre::Result<Vec<FoodPrep>> {
let records = sqlx::query_as!(
FoodPrep,
"SELECT * FROM food_prep WHERE event_id=$1",
event_id
)
.fetch_all(&*self.pg_pool)
.await?;
Ok(records)
}

pub async fn add_event_food_prep(
&self,
event_id: i32,
recipe_id: i32,
prep_date: NaiveDateTime,
use_from: Option<NaiveDateTime>,
use_til: NaiveDateTime,
) -> eyre::Result<FoodPrep> {
let query = sqlx::query_as!(
FoodPrep,
r#"
INSERT INTO public.food_prep (event_id, recipe_id, prep_date, use_from, use_until)
VALUES ($1, $2, $3, $4, $5)
RETURNING *
"#,
event_id,
recipe_id,
prep_date,
use_from,
use_til
)
.fetch_one(&*self.pg_pool)
.await?;
Ok(query)
}

pub async fn get_event_source_overrides(
&self,
event_id: i32,
) -> eyre::Result<Vec<SourceOverride>> {
let records = sqlx::query_as!(
SourceOverride,
"SELECT * FROM event_source_overrides WHERE event_id=$1",
event_id
)
.fetch_all(&*self.pg_pool)
.await?;
Ok(records)
}

pub async fn add_event_source_override(
&self,
event_id: i32,
source_id: i32,
) -> eyre::Result<SourceOverride> {
let source_override = sqlx::query_as!(
SourceOverride,
r#"
INSERT INTO public.event_source_overrides (event_id, ingredient_source_id)
VALUES ($1, $2)
RETURNING *
"#,
event_id,
source_id
)
.fetch_one(&*self.pg_pool)
.await?;
Ok(source_override)
}
}

0 comments on commit eebac3c

Please sign in to comment.