Skip to content

Commit

Permalink
Shopping!
Browse files Browse the repository at this point in the history
  • Loading branch information
isiko committed May 29, 2024
1 parent efaa33d commit 62b9cf6
Show file tree
Hide file tree
Showing 4 changed files with 176 additions and 20 deletions.
18 changes: 9 additions & 9 deletions cli-client/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,20 +139,20 @@ This Project aims to creat a simple to use Commandline application that interfac
* [ ] start <start_time>
* [ ] end <end_time>
* [ ] comment <comment>
* [ ] shopping
* [x] shopping
* [x] add
* [x] tour <date> <store>
* [x] source_override <source_id>
* [x] food_prep <recipe_id> <prep_date> <use_until> <use_from>
* [ ] delete
* [x] delete
* [x] tour (<tour_id>|<date> <store>)
* [x] source_override <ingredient_id>
* [x] food_prep (<prep_id>|<recipe_id> <prep_date>)
* [ ] edit
* [x] edit
* [x] tour <tour_id> [--date <date>] [--store <store>]
* [ ] source_override <source_id>
* [ ] food_prep <prep_id>
* [ ] [--recipe_id <new_recipe_id>
* [ ] [--prep_date <new_date>
* [ ] [--start <start_date>]
* [ ] [--end <end_date>]
* [x] source_override <source_id>
* [x] food_prep <prep_id>
* [x] [--recipe_id <new_recipe_id>
* [x] [--prep_date <new_date>
* [x] [--start <start_date>]
* [x] [--end <end_date>]
12 changes: 8 additions & 4 deletions cli-client/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -928,6 +928,7 @@ pub struct EditEventShoppingEdit {
#[derive(Debug, Subcommand)]
pub enum EditEventShoppingEditType {
Tour(EditEventShoppingEditTour),
/// Note: There is no check if the two Ingredient Sources are for the same Ingredient
SourceOverride(EditEventShoppingEditSourceOverride),
FoodPrep(EditEventShoppingEditFoodPrep),
}
Expand All @@ -947,7 +948,10 @@ pub struct EditEventShoppingEditTour {

#[derive(Debug, Args)]
pub struct EditEventShoppingEditSourceOverride {
pub ingredient_id: i32,
/// The Source ID to override
pub old_source_id: i32,
/// The new Source ID used to Override
pub new_source_id: i32,
}

#[derive(Debug, Args)]
Expand All @@ -960,13 +964,13 @@ pub struct EditEventShoppingEditFoodPrep {

#[clap(long, short)]
/// New Reciep
pub prep_date: Option<String>,
pub prep_date: Option<NaiveDateTime>,

#[clap(long, short)]
/// New start
pub start: Option<String>,
pub start: Option<NaiveDateTime>,

#[clap(long, short)]
/// New end
pub end: Option<String>,
pub end: Option<NaiveDateTime>,
}
59 changes: 52 additions & 7 deletions cli-client/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1355,21 +1355,66 @@ async fn main() {
if let Some(store) = &tour_data.store {
let store =
food_base.get_store_by_ref((&store).to_string()).await;
if store.is_err() {
if store.is_ok() {
let store = store.unwrap();

let _ = food_base
.update_event_shopping_tour_store(
tour_id,
store.store_id,
)
.await;

println!("Updated tour destination")
} else {
println!("Could not find Store");
return;
}
let store = store.unwrap();
}
}
EditEventShoppingEditType::SourceOverride(source_edit_data) => {
let _ = food_base
.update_event_ingredient_source_override(
event_id.event_id,
source_edit_data.old_source_id,
source_edit_data.new_source_id,
)
.await;
}
EditEventShoppingEditType::FoodPrep(prep_edit_data) => {
let prep_id = prep_edit_data.prep_id;

if let Some(recipe_ref) = prep_edit_data.recipe.clone() {
let recipe_query = food_base
.get_recipe_from_string_reference(recipe_ref)
.await;
if let Some(recipe) = recipe_query {
let _ = food_base.update_event_food_prep_recipe_id(
prep_id,
recipe.recipe_id,
);
} else {
println!("Could not find Recipe")
}
}

if let Some(prep_date) = prep_edit_data.prep_date.clone() {
let _ = food_base
.update_event_shopping_tour_store(tour_id, store.store_id)
.update_event_food_prep_prep_date(prep_id, prep_date)
.await;
}

println!("Updated tour destination")
if let Some(use_from) = prep_edit_data.start {
let _ = food_base
.update_event_food_prep_use_from(prep_id, use_from)
.await;
}

if let Some(use_until) = prep_edit_data.end {
let _ = food_base
.update_event_food_prep_use_until(prep_id, use_until)
.await;
}
}
EditEventShoppingEditType::SourceOverride(_) => todo!(),
EditEventShoppingEditType::FoodPrep(_) => todo!(),
},
},
}
Expand Down
107 changes: 107 additions & 0 deletions foodlib/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,29 @@ impl FoodBase {
Ok(source_override)
}

pub async fn update_event_ingredient_source_override(
&self,
event_id: i32,
old_source_id: i32,
new_source_id: i32,
) -> eyre::Result<SourceOverride> {
let result = sqlx::query_as!(
SourceOverride,
r#"
UPDATE event_source_overrides
SET ingredient_source_id = $3
WHERE ingredient_source_id = $1 AND ingredient_source_id = $2
RETURNING *
"#,
event_id,
old_source_id,
new_source_id,
)
.fetch_one(&*self.pg_pool)
.await?;
Ok(result)
}

pub async fn delete_event_source_override(&self, source_id: i32) -> eyre::Result<()> {
let _ = sqlx::query!(
"DELETE FROM event_source_overrides WHERE ingredient_source_id = $1",
Expand All @@ -459,6 +482,90 @@ impl FoodBase {
.await?;
Ok(())
}

pub async fn update_event_food_prep_recipe_id(
&self,
prep_id: i32,
recipe_id: i32,
) -> eyre::Result<FoodPrep> {
let result = sqlx::query_as!(
FoodPrep,
r#"
UPDATE food_prep
SET recipe_id = $2
WHERE prep_id = $1
RETURNING *
"#,
prep_id,
recipe_id,
)
.fetch_one(&*self.pg_pool)
.await?;
Ok(result)
}

pub async fn update_event_food_prep_prep_date(
&self,
prep_id: i32,
prep_date: NaiveDateTime,
) -> eyre::Result<FoodPrep> {
let result = sqlx::query_as!(
FoodPrep,
r#"
UPDATE food_prep
SET prep_date = $2
WHERE prep_id = $1
RETURNING *
"#,
prep_id,
prep_date
)
.fetch_one(&*self.pg_pool)
.await?;
Ok(result)
}

pub async fn update_event_food_prep_use_from(
&self,
prep_id: i32,
use_from: NaiveDateTime,
) -> eyre::Result<FoodPrep> {
let result = sqlx::query_as!(
FoodPrep,
r#"
UPDATE food_prep
SET use_from = $2
WHERE prep_id = $1
RETURNING *
"#,
prep_id,
use_from
)
.fetch_one(&*self.pg_pool)
.await?;
Ok(result)
}

pub async fn update_event_food_prep_use_until(
&self,
prep_id: i32,
use_until: NaiveDateTime,
) -> eyre::Result<FoodPrep> {
let result = sqlx::query_as!(
FoodPrep,
r#"
UPDATE food_prep
SET use_until = $2
WHERE prep_id = $1
RETURNING *
"#,
prep_id,
use_until
)
.fetch_one(&*self.pg_pool)
.await?;
Ok(result)
}
}

#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize, Tabled)]
Expand Down

0 comments on commit 62b9cf6

Please sign in to comment.