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
|
|
@ -89,7 +89,7 @@ pub struct CreateRecipeRequest {
|
|||
pub name: String,
|
||||
pub rating: u8,
|
||||
pub ingredients: Vec<(i64, f64)>,
|
||||
pub steps: Vec<String>,
|
||||
pub steps: String,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Clone, Debug)]
|
||||
|
|
@ -105,7 +105,7 @@ pub struct ListRecipesResponse {
|
|||
#[derive(Serialize, Deserialize, Clone, Debug)]
|
||||
pub struct RecipeInfo {
|
||||
pub name: String,
|
||||
pub steps: Vec<String>,
|
||||
pub steps: String,
|
||||
pub ingredients: Vec<(i64, IngredientInfo, f64)>,
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -421,7 +421,7 @@ fn AddIngredientInner(props: &AddIngredientProps) -> HtmlResult {
|
|||
}
|
||||
Err(e) => {
|
||||
err.set(Some(format!("Could not add ingredient: {e}")));
|
||||
},
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
@ -622,7 +622,7 @@ fn RecipeViewerInner(props: &RecipeViewerInnerProps) -> HtmlResult {
|
|||
<div class="text-start">
|
||||
<h2>{"Steps"}</h2>
|
||||
<ul class="list-group list-group-flush">
|
||||
{for r.steps.iter().map(|text| html!{
|
||||
{for r.steps.split('\n').map(|text| html!{
|
||||
<li class="list-group-item">{text}</li>
|
||||
})}
|
||||
</ul>
|
||||
|
|
|
|||
|
|
@ -290,26 +290,9 @@ pub fn RecipeCreator() -> Html {
|
|||
})
|
||||
};
|
||||
|
||||
let steps = use_state(im::Vector::new);
|
||||
|
||||
let s = steps.clone();
|
||||
let on_add_step = Callback::from(move |_| {
|
||||
let mut steps = (*s).clone();
|
||||
steps.push_back(String::new());
|
||||
s.set(steps);
|
||||
});
|
||||
|
||||
let mk_step_del = |idx| {
|
||||
let s = steps.clone();
|
||||
Callback::from(move |_| {
|
||||
let mut steps = (*s).clone();
|
||||
steps.remove(idx);
|
||||
s.set(steps);
|
||||
})
|
||||
};
|
||||
|
||||
let mk_step_change = |idx| {
|
||||
let s = steps.clone();
|
||||
let steps = use_state(String::new);
|
||||
let on_step_change = {
|
||||
let steps = steps.clone();
|
||||
Callback::from(move |e: Event| {
|
||||
let Some(target) = e.target() else {
|
||||
return;
|
||||
|
|
@ -319,9 +302,7 @@ pub fn RecipeCreator() -> Html {
|
|||
return;
|
||||
};
|
||||
|
||||
let mut steps = (*s).clone();
|
||||
steps[idx] = target.value();
|
||||
s.set(steps);
|
||||
steps.set(target.value());
|
||||
})
|
||||
};
|
||||
|
||||
|
|
@ -376,7 +357,7 @@ pub fn RecipeCreator() -> Html {
|
|||
.iter()
|
||||
.map(|rcp_ig: &RecipeIngredient| (rcp_ig.id, rcp_ig.amount / (*pc) as f64))
|
||||
.collect(),
|
||||
steps: (*s).iter().cloned().collect(),
|
||||
steps: (*s).clone(),
|
||||
},
|
||||
)
|
||||
.await
|
||||
|
|
@ -519,27 +500,11 @@ pub fn RecipeCreator() -> Html {
|
|||
</div>
|
||||
<div>
|
||||
<h2>{"Steps"}</h2>
|
||||
<ul class="list-group list-group-flush">
|
||||
{for (*steps).iter().enumerate().map(|(idx, step)| {
|
||||
html!{
|
||||
<li class="list-group-item">
|
||||
<textarea
|
||||
class="form-control"
|
||||
value={step.clone()}
|
||||
onchange={mk_step_change(idx)}
|
||||
value={(*steps).clone()}
|
||||
onchange={on_step_change}
|
||||
/>
|
||||
<button
|
||||
class="btn btn-danger mt-1"
|
||||
onclick={mk_step_del(idx)}
|
||||
>
|
||||
{"Remove"}
|
||||
</button>
|
||||
</li>
|
||||
}
|
||||
})
|
||||
}
|
||||
</ul>
|
||||
<button class="btn btn-primary" onclick={on_add_step}>{"Add Step"}</button>
|
||||
</div>
|
||||
<hr />
|
||||
<ModalToggleButton classes={classes!("btn", "btn-lg", "btn-primary")} modal_id="newRcpModal">
|
||||
|
|
|
|||
|
|
@ -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