Skip to content

Commit

Permalink
Add delete func to Event Ingredient Overrides
Browse files Browse the repository at this point in the history
  • Loading branch information
T3C42 committed Jun 2, 2024
1 parent 37aeb73 commit 2d90d3d
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 3 deletions.
45 changes: 43 additions & 2 deletions backend/src/frontend/events_tab/event_detail_tab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use axum::{
extract::{Form, Path, State},
http::StatusCode,
response::IntoResponse,
routing::{get, post},
routing::{delete, get, post},
};
use axum_login::RequireAuthorizationLayer;
use bigdecimal::ToPrimitive;
Expand All @@ -24,6 +24,11 @@ pub(crate) fn event_detail_router() -> axum::Router<MyAppState> {
axum::Router::new()
.route("/:event_id", post(update_event))
.route("/:event_id/overrides/:source_id", post(update_override))
.route("/:event_id/overrides/:source_id", delete(delete_override))
.route(
"/:event_id/overrides/:source_id/delete_dialog",
get(delete_override_dialog),
)
.route_layer(RequireAuthorizationLayer::<i64, User>::login_or_redirect(
Arc::new(LOGIN_URL.into()),
None,
Expand All @@ -40,6 +45,40 @@ pub(crate) fn event_detail_router() -> axum::Router<MyAppState> {
)
}

pub async fn delete_override_dialog(
state: State<MyAppState>,
Path((event_id, source_id)): Path<(i32, i32)>,
) -> Markup {
let source = state
.get_event_source_override(event_id, source_id)
.await
.unwrap_or_default();
html! {
dialog open="true" class="dialog" id="delete" {
p class="text-2xl" { (format!("Do you really want to delete Source Override: {} from {}", source.ingredient, source.store)) }
div class="flex justify-between w-full m-2 gap-2" {
button class="btn btn-abort" hx-get=(format!("/events/edit/{}", event_id)) hx-target="#content" { "Abort" }
button class="btn btn-cancel mx-4" hx-target="#content" hx-delete=(format!("/events/edit/{}/overrides/{}", event_id, source_id)) { "Confirm Delete" }
}
}
}
}

pub async fn delete_override(
state: State<MyAppState>,
Path((event_id, source_id)): Path<(i32, i32)>,
) -> Markup {
match state
.delete_event_source_override_with_event_id(event_id, source_id)
.await
{
Ok(_) => event_form(state, Path(event_id))
.await
.unwrap_or_else(|e| e),
Err(_) => html_error("Failed to delete source override", "/events"),
}
}

pub async fn delete_meal_dialog(
state: State<MyAppState>,
Path((event_id, meal_id)): Path<(i32, i32)>,
Expand Down Expand Up @@ -120,7 +159,7 @@ async fn event_form(state: State<MyAppState>, Path(event_id): Path<i32>) -> Resu
p class="text-2xl" { "Ingredient Sources Overrides" }
}
table class="w-full text-inherit table-auto object-center table-fixed" {
thead { tr { th { "Ingredient" } th {"Store"} th {} } }
thead { tr { th class="w-1/3" { "Ingredient" } th class="w-1/3" {"Store"} th {} th {} } }
tbody {
(format_event_source_override(&dummy_source, &stores))
@for over in overrides {
Expand Down Expand Up @@ -167,6 +206,8 @@ fn format_event_source_override(source_override: &SourceOverrideView, stores: &[
}
}
td { (button) }
td { @if source_override.ingredient_id != -1 {
button class="btn btn-cancel" hx-get=(format!("/events/edit/{}/overrides/{}/delete_dialog", source_override.event_id, source_override.ingredient_source_id)) hx-target="this" hx-swap="outerHTML" { "Delete" } }}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use axum::{
use bigdecimal::BigDecimal;
use chrono::DateTime;
use maud::{html, Markup};
use serde::{de, Deserialize};
use serde::Deserialize;

pub(crate) fn event_edit_meal_router() -> axum::Router<MyAppState> {
axum::Router::new()
Expand Down
40 changes: 40 additions & 0 deletions foodlib/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,30 @@ impl FoodBase {
Ok(overrides)
}

pub async fn get_event_source_override(
&self,
event_id: i32,
ingredient_source_id: i32,
) -> eyre::Result<SourceOverrideView> {
let ingr_override = sqlx::query_as!(
SourceOverrideView,
r#"
SELECT event_id, ingredient_id, ingredient_sources.ingredient_source_id, ingredients.name as ingredient, store_id, stores.name as store
FROM event_source_overrides
INNER JOIN ingredient_sources USING (ingredient_source_id)
INNER JOIN ingredients USING (ingredient_id)
INNER JOIN stores USING (store_id)
WHERE event_id = $1 AND ingredient_source_id = $2
"#,
event_id,
ingredient_source_id
)
.fetch_one(&*self.pg_pool)
.await?;

Ok(ingr_override)
}

pub async fn add_event_source_override(
&self,
event_id: i32,
Expand Down Expand Up @@ -558,6 +582,22 @@ impl FoodBase {
Ok(())
}

//TODO: is the event_id needed? If not remove function and use above
pub async fn delete_event_source_override_with_event_id(
&self,
event_id: i32,
source_id: i32,
) -> eyre::Result<()> {
let _ = sqlx::query!(
"DELETE FROM event_source_overrides WHERE event_id = $1 AND ingredient_source_id = $2",
event_id,
source_id
)
.fetch_optional(&*self.pg_pool)
.await?;
Ok(())
}

pub async fn update_event_food_prep_recipe_id(
&self,
prep_id: i32,
Expand Down

0 comments on commit 2d90d3d

Please sign in to comment.