refactor: use update_response helper in update handlers

Replace duplicated datastar/form/json response branching in brew, cup,
and roast update handlers with the shared update_response helper.
This commit is contained in:
Jon Seager 2026-02-13 13:05:50 +00:00
parent 167af65943
commit a1aefda860
No known key found for this signature in database
3 changed files with 36 additions and 39 deletions

View file

@ -12,7 +12,7 @@ use crate::application::routes::api::images::save_deferred_image;
use crate::application::routes::api::macros::{define_delete_handler, define_enriched_get_handler};
use crate::application::routes::support::{
FlexiblePayload, ListQuery, PayloadSource, impl_has_changes, is_datastar_request,
validate_update,
update_response, validate_update,
};
use crate::application::state::AppState;
use crate::domain::bags::BagFilter;
@ -64,10 +64,11 @@ pub(crate) async fn load_brew_form_data(state: &AppState) -> Result<BrewFormData
let gear_request = ListRequest::show_all(GearSortKey::Make, SortDirection::Asc);
let grinder_options = load_gear_options(state, GearCategory::Grinder, &gear_request).await?;
let brewer_options = load_gear_options(state, GearCategory::Brewer, &gear_request).await?;
let filter_paper_options =
load_gear_options(state, GearCategory::FilterPaper, &gear_request).await?;
let (grinder_options, brewer_options, filter_paper_options) = tokio::try_join!(
load_gear_options(state, GearCategory::Grinder, &gear_request),
load_gear_options(state, GearCategory::Brewer, &gear_request),
load_gear_options(state, GearCategory::FilterPaper, &gear_request),
)?;
let last_brew_request = ListRequest::new(
1,
@ -419,20 +420,18 @@ pub(crate) async fn update_brew(
save_deferred_image(&state, "brew", i64::from(id), image_data_url.as_deref()).await;
let detail_url = format!("/brews/{id}");
let enriched = state
.brew_repo
.get_with_details(id)
.await
.map_err(AppError::from)?;
if is_datastar_request(&headers) {
crate::application::routes::support::render_redirect_script(&detail_url)
.map_err(ApiError::from)
} else if matches!(source, PayloadSource::Form) {
Ok(Redirect::to(&detail_url).into_response())
} else {
let enriched = state
.brew_repo
.get_with_details(id)
.await
.map_err(AppError::from)?;
Ok(Json(enriched).into_response())
}
update_response(
&headers,
source,
&detail_url,
Json(enriched).into_response(),
)
}
define_delete_handler!(

View file

@ -14,7 +14,7 @@ use crate::application::routes::api::macros::{
use crate::application::routes::support::impl_has_changes;
use crate::application::routes::support::{
FlexiblePayload, ListQuery, PayloadSource, is_datastar_request, render_redirect_script,
validate_update,
update_response, validate_update,
};
use crate::application::state::AppState;
use crate::domain::cups::{CupFilter, CupSortKey, CupWithDetails, NewCup, UpdateCup};
@ -146,19 +146,18 @@ pub(crate) async fn update_cup(
save_deferred_image(&state, "cup", i64::from(cup.id), image_data_url.as_deref()).await;
let detail_url = format!("/cups/{id}");
let enriched = state
.cup_repo
.get_with_details(id)
.await
.map_err(AppError::from)?;
if is_datastar_request(&headers) {
render_redirect_script(&detail_url).map_err(ApiError::from)
} else if matches!(source, PayloadSource::Form) {
Ok(Redirect::to(&detail_url).into_response())
} else {
let enriched = state
.cup_repo
.get_with_details(id)
.await
.map_err(AppError::from)?;
Ok(Json(enriched).into_response())
}
update_response(
&headers,
source,
&detail_url,
Json(enriched).into_response(),
)
}
define_delete_handler!(

View file

@ -13,7 +13,7 @@ use crate::application::routes::api::macros::{
};
use crate::application::routes::support::{
FlexiblePayload, ListQuery, PayloadSource, impl_has_changes, is_datastar_request,
render_redirect_script, validate_update,
render_redirect_script, update_response, validate_update,
};
use crate::application::state::AppState;
use crate::domain::ids::{RoastId, RoasterId};
@ -262,13 +262,12 @@ pub(crate) async fn update_roast(
enriched.roaster_slug, enriched.roast.slug
);
if is_datastar_request(&headers) {
render_redirect_script(&detail_url).map_err(ApiError::from)
} else if matches!(source, PayloadSource::Form) {
Ok(Redirect::to(&detail_url).into_response())
} else {
Ok(Json(enriched).into_response())
}
update_response(
&headers,
source,
&detail_url,
Json(enriched).into_response(),
)
}
#[derive(Debug, Deserialize)]