all: Set steps to be a single Vec & fix 'ingerdient' typo

This commit is contained in:
traxys 2023-06-25 21:37:28 +02:00
parent b01f08ba2f
commit 0c7e52bd8b
9 changed files with 78 additions and 81 deletions

View file

@ -11,7 +11,7 @@ use axum::{
};
use sea_orm::{prelude::*, ActiveValue, TransactionTrait};
use crate::entity::{ingredient, prelude::*, recipe, recipe_ingerdients, recipe_steps};
use crate::entity::{ingredient, prelude::*, recipe, recipe_ingredients};
use super::{
household::AuthorizedHousehold, ingredients::IngredientExtractor, AppState, JsonResult,
@ -31,19 +31,11 @@ pub(super) async fn create_recipe(
name: ActiveValue::Set(request.name),
ranking: ActiveValue::Set(request.rating as i32),
household: ActiveValue::Set(household.id),
steps: ActiveValue::Set(request.steps),
..Default::default()
};
let recipe = model.insert(txn).await?;
for (num, text) in request.steps.into_iter().enumerate() {
let model = recipe_steps::ActiveModel {
num: ActiveValue::Set(num as _),
recipe_id: ActiveValue::Set(recipe.id),
text: ActiveValue::Set(text),
};
model.insert(txn).await?;
}
for (ig, amount) in request.ingredients {
if 0 == household
@ -57,7 +49,7 @@ pub(super) async fn create_recipe(
)))?;
}
let model = recipe_ingerdients::ActiveModel {
let model = recipe_ingredients::ActiveModel {
recipe_id: ActiveValue::Set(recipe.id),
ingredient_id: ActiveValue::Set(ig),
amount: ActiveValue::Set(amount),
@ -130,14 +122,6 @@ pub(super) async fn fetch_recipe(
State(state): State<AppState>,
RecipeExtractor(recipe): RecipeExtractor,
) -> JsonResult<RecipeInfo> {
let steps = recipe
.find_related(RecipeSteps)
.all(&state.db)
.await?
.into_iter()
.map(|m| m.text)
.collect();
let recipe_ingredients = recipe.find_related(Ingredient).all(&state.db).await?;
let mut ingredients = Vec::new();
@ -149,7 +133,7 @@ pub(super) async fn fetch_recipe(
name: ingredient.name,
unit: ingredient.unit,
},
RecipeIngerdients::find_by_id((recipe.id, ingredient.id))
RecipeIngredients::find_by_id((recipe.id, ingredient.id))
.one(&state.db)
.await?
.expect("Ingredient should exist as it was fetched")
@ -159,7 +143,7 @@ pub(super) async fn fetch_recipe(
Ok(RecipeInfo {
name: recipe.name,
steps,
steps: recipe.steps,
ingredients,
}
.into())
@ -187,7 +171,7 @@ pub(super) async fn edit_ig_amount(
IngredientExtractor(ingredient): IngredientExtractor,
Json(req): Json<RecipeIngredientEditRequest>,
) -> JsonResult<EmptyResponse> {
let active_model = recipe_ingerdients::ActiveModel {
let active_model = recipe_ingredients::ActiveModel {
recipe_id: ActiveValue::Set(recipe.id),
ingredient_id: ActiveValue::Set(ingredient.id),
amount: ActiveValue::Set(req.amount),
@ -203,7 +187,7 @@ pub(super) async fn delete_ig(
RecipeExtractor(recipe): RecipeExtractor,
IngredientExtractor(ingredient): IngredientExtractor,
) -> JsonResult<EmptyResponse> {
RecipeIngerdients::delete_by_id((recipe.id, ingredient.id))
RecipeIngredients::delete_by_id((recipe.id, ingredient.id))
.exec(&state.db)
.await?;
@ -216,7 +200,7 @@ pub(super) async fn add_ig_request(
IngredientExtractor(ingredient): IngredientExtractor,
Json(req): Json<AddRecipeIngredientRequest>,
) -> JsonResult<EmptyResponse> {
let model = recipe_ingerdients::ActiveModel {
let model = recipe_ingredients::ActiveModel {
recipe_id: ActiveValue::Set(recipe.id),
ingredient_id: ActiveValue::Set(ingredient.id),
amount: ActiveValue::Set(req.amount),