app,server: Allow to edit recipe name
This commit is contained in:
parent
1a0ffb2d89
commit
47b547caf4
4 changed files with 203 additions and 19 deletions
|
|
@ -224,6 +224,8 @@ pub(crate) fn router(api_allowed: Option<HeaderValue>) -> Router<AppState> {
|
|||
)
|
||||
.route(
|
||||
"/household/:house_id/recipe/:recipe_id",
|
||||
get(recipe::fetch_recipe).layer(mk_service(vec![Method::GET])),
|
||||
get(recipe::fetch_recipe)
|
||||
.patch(recipe::edit_name)
|
||||
.layer(mk_service(vec![Method::GET, Method::PATCH])),
|
||||
)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,8 +1,11 @@
|
|||
use api::{
|
||||
CreateRecipeRequest, CreateRecipeResponse, IngredientInfo, ListRecipesResponse, RecipeInfo,
|
||||
CreateRecipeRequest, CreateRecipeResponse, EmptyResponse, IngredientInfo, ListRecipesResponse,
|
||||
RecipeInfo, RecipeRenameRequest,
|
||||
};
|
||||
use axum::{
|
||||
extract::{Path, State},
|
||||
async_trait,
|
||||
extract::{FromRef, FromRequestParts, Path, State},
|
||||
http::request::Parts,
|
||||
Json,
|
||||
};
|
||||
use sea_orm::{prelude::*, ActiveValue, TransactionTrait};
|
||||
|
|
@ -83,23 +86,46 @@ pub(super) async fn list_recipes(
|
|||
.into())
|
||||
}
|
||||
|
||||
#[derive(serde::Deserialize)]
|
||||
pub(super) struct RecipeId {
|
||||
recipe_id: i64,
|
||||
pub(super) struct RecipeExtractor(recipe::Model);
|
||||
|
||||
#[async_trait]
|
||||
impl<S> FromRequestParts<S> for RecipeExtractor
|
||||
where
|
||||
S: Send + Sync,
|
||||
AppState: FromRef<S>,
|
||||
{
|
||||
type Rejection = RouteError;
|
||||
|
||||
async fn from_request_parts(parts: &mut Parts, state: &S) -> Result<Self, Self::Rejection> {
|
||||
let State(app_state): State<AppState> = State::from_request_parts(parts, state)
|
||||
.await
|
||||
.expect("No state");
|
||||
|
||||
#[derive(serde::Deserialize)]
|
||||
pub(super) struct RecipeId {
|
||||
recipe_id: i64,
|
||||
}
|
||||
|
||||
let household = AuthorizedHousehold::from_request_parts(parts, state).await?;
|
||||
let Path(recipe): Path<RecipeId> = Path::from_request_parts(parts, state).await?;
|
||||
|
||||
match household
|
||||
.0
|
||||
.find_related(Recipe)
|
||||
.filter(recipe::Column::Id.eq(recipe.recipe_id))
|
||||
.one(&app_state.db)
|
||||
.await?
|
||||
{
|
||||
None => Err(RouteError::RessourceNotFound),
|
||||
Some(r) => Ok(Self(r)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) async fn fetch_recipe(
|
||||
AuthorizedHousehold(household): AuthorizedHousehold,
|
||||
State(state): State<AppState>,
|
||||
Path(RecipeId { recipe_id }): Path<RecipeId>,
|
||||
RecipeExtractor(recipe): RecipeExtractor,
|
||||
) -> JsonResult<RecipeInfo> {
|
||||
let Some(recipe) = household
|
||||
.find_related(Recipe)
|
||||
.filter(recipe::Column::Id.eq(recipe_id))
|
||||
.one(&state.db).await? else {
|
||||
return Err(RouteError::RessourceNotFound);
|
||||
};
|
||||
|
||||
let steps = recipe
|
||||
.find_related(RecipeSteps)
|
||||
.all(&state.db)
|
||||
|
|
@ -134,3 +160,19 @@ pub(super) async fn fetch_recipe(
|
|||
}
|
||||
.into())
|
||||
}
|
||||
|
||||
pub(super) async fn edit_name(
|
||||
State(state): State<AppState>,
|
||||
RecipeExtractor(recipe): RecipeExtractor,
|
||||
Json(req): Json<RecipeRenameRequest>,
|
||||
) -> JsonResult<EmptyResponse> {
|
||||
let active_model = recipe::ActiveModel {
|
||||
name: ActiveValue::Set(req.name),
|
||||
id: ActiveValue::Set(recipe.id),
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
active_model.update(&state.db).await?;
|
||||
|
||||
Ok(EmptyResponse {}.into())
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue