all: Set steps to be a single Vec & fix 'ingerdient' typo
This commit is contained in:
parent
b01f08ba2f
commit
0c7e52bd8b
9 changed files with 78 additions and 81 deletions
|
|
@ -32,10 +32,10 @@ impl Related<super::household::Entity> for Entity {
|
|||
|
||||
impl Related<super::recipe::Entity> for Entity {
|
||||
fn to() -> RelationDef {
|
||||
super::recipe_ingerdients::Relation::Recipe.def()
|
||||
super::recipe_ingredients::Relation::Recipe.def()
|
||||
}
|
||||
fn via() -> Option<RelationDef> {
|
||||
Some(super::recipe_ingerdients::Relation::Ingredient.def().rev())
|
||||
Some(super::recipe_ingredients::Relation::Ingredient.def().rev())
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -6,5 +6,5 @@ pub mod household;
|
|||
pub mod household_members;
|
||||
pub mod ingredient;
|
||||
pub mod recipe;
|
||||
pub mod recipe_ingerdients;
|
||||
pub mod recipe_ingredients;
|
||||
pub mod user;
|
||||
|
|
|
|||
|
|
@ -4,5 +4,5 @@ pub use super::household::Entity as Household;
|
|||
pub use super::household_members::Entity as HouseholdMembers;
|
||||
pub use super::ingredient::Entity as Ingredient;
|
||||
pub use super::recipe::Entity as Recipe;
|
||||
pub use super::recipe_ingerdients::Entity as RecipeIngerdients;
|
||||
pub use super::recipe_ingredients::Entity as RecipeIngredients;
|
||||
pub use super::user::Entity as User;
|
||||
|
|
|
|||
|
|
@ -35,10 +35,10 @@ impl Related<super::household::Entity> for Entity {
|
|||
|
||||
impl Related<super::ingredient::Entity> for Entity {
|
||||
fn to() -> RelationDef {
|
||||
super::recipe_ingerdients::Relation::Ingredient.def()
|
||||
super::recipe_ingredients::Relation::Ingredient.def()
|
||||
}
|
||||
fn via() -> Option<RelationDef> {
|
||||
Some(super::recipe_ingerdients::Relation::Recipe.def().rev())
|
||||
Some(super::recipe_ingredients::Relation::Recipe.def().rev())
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
48
src/entity/recipe_ingredients.rs
Normal file
48
src/entity/recipe_ingredients.rs
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.11.3
|
||||
|
||||
use sea_orm::entity::prelude::*;
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, DeriveEntityModel)]
|
||||
#[sea_orm(table_name = "recipe_ingredients")]
|
||||
pub struct Model {
|
||||
#[sea_orm(primary_key, auto_increment = false)]
|
||||
pub recipe_id: i64,
|
||||
#[sea_orm(primary_key, auto_increment = false)]
|
||||
pub ingredient_id: i64,
|
||||
#[sea_orm(column_type = "Double")]
|
||||
pub amount: f64,
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
|
||||
pub enum Relation {
|
||||
#[sea_orm(
|
||||
belongs_to = "super::ingredient::Entity",
|
||||
from = "Column::IngredientId",
|
||||
to = "super::ingredient::Column::Id",
|
||||
on_update = "Restrict",
|
||||
on_delete = "Restrict"
|
||||
)]
|
||||
Ingredient,
|
||||
#[sea_orm(
|
||||
belongs_to = "super::recipe::Entity",
|
||||
from = "Column::RecipeId",
|
||||
to = "super::recipe::Column::Id",
|
||||
on_update = "Restrict",
|
||||
on_delete = "Restrict"
|
||||
)]
|
||||
Recipe,
|
||||
}
|
||||
|
||||
impl Related<super::ingredient::Entity> for Entity {
|
||||
fn to() -> RelationDef {
|
||||
Relation::Ingredient.def()
|
||||
}
|
||||
}
|
||||
|
||||
impl Related<super::recipe::Entity> for Entity {
|
||||
fn to() -> RelationDef {
|
||||
Relation::Recipe.def()
|
||||
}
|
||||
}
|
||||
|
||||
impl ActiveModelBehavior for ActiveModel {}
|
||||
|
|
@ -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),
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue