mirror of https://github.com/kcal-app/kcal.git
				
				
				
			Add step support to recipe create form
This commit is contained in:
		
							parent
							
								
									f8c6f81e60
								
							
						
					
					
						commit
						198a32a915
					
				|  | @ -5,6 +5,8 @@ namespace App\Http\Controllers; | ||||||
| use App\Models\Ingredient; | use App\Models\Ingredient; | ||||||
| use App\Models\IngredientAmount; | use App\Models\IngredientAmount; | ||||||
| use App\Models\Recipe; | use App\Models\Recipe; | ||||||
|  | use App\Models\RecipeStep; | ||||||
|  | use App\Rules\ArrayNotEmpty; | ||||||
| use Illuminate\Contracts\View\View; | use Illuminate\Contracts\View\View; | ||||||
| use Illuminate\Http\RedirectResponse; | use Illuminate\Http\RedirectResponse; | ||||||
| use Illuminate\Http\Request; | use Illuminate\Http\Request; | ||||||
|  | @ -43,7 +45,7 @@ class RecipeController extends Controller | ||||||
|                 ['value' => 'tsp', 'label' => 'tsp.'], |                 ['value' => 'tsp', 'label' => 'tsp.'], | ||||||
|                 ['value' => 'tbsp', 'label' => 'tbsp.'], |                 ['value' => 'tbsp', 'label' => 'tbsp.'], | ||||||
|                 ['value' => 'cup', 'label' => 'cup'], |                 ['value' => 'cup', 'label' => 'cup'], | ||||||
|                 ['value' => 'g', 'label' => 'g'], |                 ['value' => 'grams', 'label' => 'g'], | ||||||
|             ])); |             ])); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  | @ -59,12 +61,14 @@ class RecipeController extends Controller | ||||||
|             'name' => 'required|string', |             'name' => 'required|string', | ||||||
|             'description' => 'required|string', |             'description' => 'required|string', | ||||||
|             'servings' => 'required|numeric', |             'servings' => 'required|numeric', | ||||||
|             'ingredients_amount' => 'required|array', |             'ingredients_amount' => ['required', 'array', new ArrayNotEmpty], | ||||||
|             'ingredients_amount.*' => 'required_with:ingredients.*|nullable|numeric|min:0', |             'ingredients_amount.*' => 'required_with:ingredients.*|nullable|numeric|min:0', | ||||||
|             'ingredients_unit' => 'required|array', |             'ingredients_unit' => ['required', 'array', new ArrayNotEmpty], | ||||||
|             'ingredients_unit.*' => 'nullable|string', |             'ingredients_unit.*' => 'nullable|string', | ||||||
|             'ingredients' => 'required|array', |             'ingredients' => ['required', 'array', new ArrayNotEmpty], | ||||||
|             'ingredients.*' => 'required_with:ingredients_amount.*|nullable|exists:App\Models\Ingredient,id', |             'ingredients.*' => 'required_with:ingredients_amount.*|nullable|exists:App\Models\Ingredient,id', | ||||||
|  |             'steps' => ['required', 'array', new ArrayNotEmpty], | ||||||
|  |             'steps.*' => 'nullable|string', | ||||||
|         ]); |         ]); | ||||||
| 
 | 
 | ||||||
|         $recipe = new Recipe([ |         $recipe = new Recipe([ | ||||||
|  | @ -78,17 +82,28 @@ class RecipeController extends Controller | ||||||
|                 if (!$recipe->save()) { |                 if (!$recipe->save()) { | ||||||
|                     return; |                     return; | ||||||
|                 } |                 } | ||||||
|  | 
 | ||||||
|                 $ingredient_amounts = []; |                 $ingredient_amounts = []; | ||||||
|  |                 $weight = 0; | ||||||
|                 foreach (array_filter($input['ingredients_amount']) as $key => $amount) { |                 foreach (array_filter($input['ingredients_amount']) as $key => $amount) { | ||||||
|                     $ingredient_amounts[$key] = new IngredientAmount([ |                     $ingredient_amounts[$key] = new IngredientAmount([ | ||||||
|                         'amount' => (float) $amount, |                         'amount' => (float) $amount, | ||||||
|                         'unit' => $input['ingredients_unit'][$key], |                         'unit' => $input['ingredients_unit'][$key], | ||||||
|                         'weight' => (int) $key, |                         'weight' => $weight++, | ||||||
|                     ]); |                     ]); | ||||||
|                     $ingredient_amounts[$key]->recipe()->associate($recipe); |  | ||||||
|                     $ingredient_amounts[$key]->ingredient()->associate($input['ingredients'][$key]); |                     $ingredient_amounts[$key]->ingredient()->associate($input['ingredients'][$key]); | ||||||
|                 } |                 } | ||||||
|                 $recipe->ingredientAmounts()->saveMany($ingredient_amounts); |                 $recipe->ingredientAmounts()->saveMany($ingredient_amounts); | ||||||
|  | 
 | ||||||
|  |                 $steps = []; | ||||||
|  |                 $number = 1; | ||||||
|  |                 foreach (array_filter($input['steps']) as $step) { | ||||||
|  |                     $steps[] = new RecipeStep([ | ||||||
|  |                         'number' => $number++, | ||||||
|  |                         'step' => $step, | ||||||
|  |                     ]); | ||||||
|  |                 } | ||||||
|  |                 $recipe->ingredientAmounts()->saveMany($steps); | ||||||
|             }); |             }); | ||||||
|         } catch (\Exception $e) { |         } catch (\Exception $e) { | ||||||
|             DB::rollBack(); |             DB::rollBack(); | ||||||
|  |  | ||||||
|  | @ -0,0 +1,30 @@ | ||||||
|  | <?php | ||||||
|  | 
 | ||||||
|  | namespace App\Rules; | ||||||
|  | 
 | ||||||
|  | use Illuminate\Contracts\Validation\Rule; | ||||||
|  | 
 | ||||||
|  | class ArrayNotEmpty implements Rule | ||||||
|  | { | ||||||
|  |     /** | ||||||
|  |      * Determine if the array is empty. | ||||||
|  |      * | ||||||
|  |      * @param string $attribute | ||||||
|  |      * @param mixed $value | ||||||
|  |      * @return bool | ||||||
|  |      */ | ||||||
|  |     public function passes($attribute, $value): bool | ||||||
|  |     { | ||||||
|  |         return !empty(array_filter($value)); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     /** | ||||||
|  |      * Get the validation error message. | ||||||
|  |      * | ||||||
|  |      * @return string | ||||||
|  |      */ | ||||||
|  |     public function message(): string | ||||||
|  |     { | ||||||
|  |         return 'At least one :attribute must be set.'; | ||||||
|  |     } | ||||||
|  | } | ||||||
|  | @ -66,7 +66,7 @@ | ||||||
| 
 | 
 | ||||||
|                         <!-- Ingredients --> |                         <!-- Ingredients --> | ||||||
|                         <h3 class="pt-2 mb-2 font-extrabold">Ingredients</h3> |                         <h3 class="pt-2 mb-2 font-extrabold">Ingredients</h3> | ||||||
|                         @for($i = 0; $i < 5; $i++) |                         @for($i = 0; $i < 10; $i++) | ||||||
|                             <div class="flex flex-row space-x-4 mb-4"> |                             <div class="flex flex-row space-x-4 mb-4"> | ||||||
|                                 <x-inputs.input type="number" |                                 <x-inputs.input type="number" | ||||||
|                                                 name="ingredients_amount[]" |                                                 name="ingredients_amount[]" | ||||||
|  | @ -87,12 +87,12 @@ | ||||||
| 
 | 
 | ||||||
|                         <!-- Steps --> |                         <!-- Steps --> | ||||||
|                         <h3 class="pt-2 mb-2 font-extrabold">Steps</h3> |                         <h3 class="pt-2 mb-2 font-extrabold">Steps</h3> | ||||||
|                         @for($i = 1; $i < 6; $i++) |                         @for($i = 0; $i < 10; $i++) | ||||||
|                             <div class="flex flex-row space-x-4 mb-4"> |                             <div class="flex flex-row space-x-4 mb-4"> | ||||||
|                                 <div class="text-3xl text-gray-400 text-center">{{ $i }}</div> |                                 <div class="text-3xl text-gray-400 text-center">{{ $i + 1 }}</div> | ||||||
|                                 <x-inputs.textarea class="block mt-1 w-full" |                                 <x-inputs.textarea class="block mt-1 w-full" | ||||||
|                                                    name="steps[]" |                                                    name="steps[]" | ||||||
|                                                    :value="old('steps[$i]')" /> |                                                    :value="old('steps.' . $i)" /> | ||||||
|                             </div> |                             </div> | ||||||
|                         @endfor |                         @endfor | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue