Skip to content

Commit

Permalink
Replace state.db_connection with state
Browse files Browse the repository at this point in the history
  • Loading branch information
T3C42 committed May 29, 2024
1 parent 826048c commit f98bf7d
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 17 deletions.
4 changes: 2 additions & 2 deletions backend/src/frontend/events_tab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub struct SearchParameters {
}

pub async fn event_list(State(state): State<MyAppState>) -> Markup {
let events = state.db_connection.get_events().await.unwrap_or_default();
let events = state.get_events().await.unwrap_or_default();

html! {
div class="
Expand Down Expand Up @@ -51,7 +51,7 @@ pub async fn event_list(State(state): State<MyAppState>) -> Markup {

pub async fn search(State(state): State<MyAppState>, query: Form<SearchParameters>) -> Markup {
let query = query.search.to_lowercase();
let events = state.db_connection.get_events().await.unwrap_or_default();
let events = state.get_events().await.unwrap_or_default();

let filtered_events = events
.iter()
Expand Down
4 changes: 2 additions & 2 deletions backend/src/frontend/events_tab/event_detail_tab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub(crate) fn event_detail_router() -> axum::Router<MyAppState> {
}

async fn event_form(state: State<MyAppState>, event_id: Path<i32>) -> Markup {
let Ok(meals) = state.db_connection.get_event_meals(event_id.0).await else {
let Ok(meals) = state.get_event_meals(event_id.0).await else {
return html_error("Failed to fetch meals", "/events");
};
html! {
Expand Down Expand Up @@ -112,7 +112,7 @@ async fn update_event(
budget,
};

if let Ok(result) = state.db_connection.update_event(&event).await {
if let Ok(result) = state.update_event(&event).await {
(StatusCode::OK, event_form(state, event_id).await).into_response()
} else {
StatusCode::INTERNAL_SERVER_ERROR.into_response()
Expand Down
4 changes: 2 additions & 2 deletions backend/src/frontend/inventories_tab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,13 +264,13 @@ pub async fn commit_inventory(
) -> Markup {
let inventory = form.0;
if inventory.inventory_id < 0 {
let Ok(inventory_id) = state.db_connection.add_inventory(inventory.name).await else {
let Ok(inventory_id) = state.add_inventory(inventory.name).await else {
return return_to_inv_selection_error();
};
manage_inventory_form(State(state), inventory_id).await
} else {
let id = inventory.inventory_id;
let Ok(inventory_id) = state.db_connection.update_inventory(inventory).await else {
let Ok(inventory_id) = state.update_inventory(inventory).await else {
return return_to_inv_overview_error(id);
};
manage_inventory_form(State(state), inventory_id).await
Expand Down
8 changes: 4 additions & 4 deletions backend/src/frontend/recipes_tab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub struct SearchParameters {

pub async fn search(State(state): State<MyAppState>, query: Form<SearchParameters>) -> Markup {
let query = query.search.to_lowercase();
let Ok(recipes) = state.db_connection.get_recipes().await else {
let Ok(recipes) = state.get_recipes().await else {
return html_error("Failed to fetch recipes");
};

Expand Down Expand Up @@ -174,7 +174,7 @@ pub async fn delete_recipe_nqa(
State(state): State<MyAppState>,
Path(recipe_id): Path<i32>,
) -> Markup {
if let Err(error) = state.db_connection.delete_recipe(recipe_id).await {
if let Err(error) = state.delete_recipe(recipe_id).await {
log::error!("Failed to delete recipe: {}", error);
return html_error("Failed to delete recipe");
};
Expand Down Expand Up @@ -203,7 +203,7 @@ pub async fn delete_recipe(Path(recipe_id): Path<i32>) -> Markup {
}

pub async fn recipes_view(State(state): State<MyAppState>) -> Markup {
let Ok(recipes) = state.db_connection.get_recipes().await else {
let Ok(recipes) = state.get_recipes().await else {
return html_error("Failed to fetch recipes");
};

Expand Down Expand Up @@ -261,7 +261,7 @@ async fn add_recipe(state: State<MyAppState>, Form(recipe): Form<NewRecipe>) ->
comment: recipe.comment,
recipe_id: -1,
};
match state.db_connection.insert_recipe(&recipe).await {
match state.insert_recipe(&recipe).await {
Ok(recipe) => recipes_edit_tab::recipe_edit_view(state, Path(recipe.recipe_id)).await,
Err(e) => html_error(&e.to_string()),
}
Expand Down
14 changes: 7 additions & 7 deletions backend/src/frontend/recipes_tab/recipes_edit_tab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ pub async fn handle_subrecipe_change(
comment: None,
}),
amount: data.subrecipe_amount,
unit: state.db_connection.get_unit(0).await.unwrap_or_default(),
unit: state.get_unit(0).await.unwrap_or_default(),
};
sub_recipe.format_for_subrecipe_table(data.recipe_id)
}
Expand Down Expand Up @@ -406,7 +406,7 @@ pub async fn handle_step_add(
duration_per_kg: pg_interval_from_minutes(data.duration_per_kg_minutes),
recipe_id: data.recipe_id,
};
let Ok(res) = state.db_connection.add_recipe_step(&step).await else {
let Ok(res) = state.add_recipe_step(&step).await else {
return return_to_recipe_edit_error(data.recipe_id);
};
dbg!(res);
Expand All @@ -422,7 +422,7 @@ pub async fn add_ingredient_form(
.get_ingredients()
.await
.unwrap_or_default();
let unit_types = state.db_connection.get_units().await.unwrap_or_default();
let unit_types = state.get_units().await.unwrap_or_default();
html! {
form hx-put="recipes/edit/commit-ingredient" hx-swap="outerHTML" hx-target="#contents" {
datalist id=("ingredient_data_list") {
Expand Down Expand Up @@ -451,7 +451,7 @@ pub async fn add_subrecipe_form(
State(state): State<MyAppState>,
Path(recipe_id): Path<i32>,
) -> Markup {
let subrecipes = state.db_connection.get_recipes().await.unwrap_or_default();
let subrecipes = state.get_recipes().await.unwrap_or_default();
html! {
form hx-put="recipes/edit/commit-subrecipe" hx-swap="outerHTML" hx-target="#contents" {
datalist id=("subrecipe_data_list") {
Expand Down Expand Up @@ -504,7 +504,7 @@ pub async fn recipe_edit_view(
.get_recipe_ingredients(recipe_id)
.await
.unwrap_or_default();
let unit_types = state.db_connection.get_units().await.unwrap_or_default();
let unit_types = state.get_units().await.unwrap_or_default();

let steps = state
.db_connection
Expand All @@ -519,8 +519,8 @@ pub async fn recipe_edit_view(
div id=("recipe-information") class="w-3/4" {
form hx-put="recipes/edit/change-name" hx-swap="none" class="w-full flex flex-col mb-4 pb-4 gap-2" {
input type="hidden" name=("recipe_id") value=(recipe_id);
input class="text" type="text" name="name" value=(state.db_connection.get_recipe(recipe_id).await.unwrap_or_default().name) required="required";
textarea class="text" name="comment" required="required" { (state.db_connection.get_recipe(recipe_id).await.unwrap_or_default().comment.unwrap_or_default()) }
input class="text" type="text" name="name" value=(state.get_recipe(recipe_id).await.unwrap_or_default().name) required="required";
textarea class="text" name="comment" required="required" { (state.get_recipe(recipe_id).await.unwrap_or_default().comment.unwrap_or_default()) }
button type="submit" class="btn btn-primary" { "Change Name and Comment" }}
}

Expand Down

0 comments on commit f98bf7d

Please sign in to comment.