From be681f0dd73684f94aa6572586906668d27bb5a4 Mon Sep 17 00:00:00 2001 From: "Christopher C. Wells" Date: Sat, 6 Mar 2021 09:30:15 -0800 Subject: [PATCH] Add recipe time fields (WIP) Not present in th frontend yet. --- app/Http/Controllers/RecipeController.php | 16 +++++---- app/JsonApi/Schemas/RecipeSchema.php | 3 ++ app/Models/Recipe.php | 9 +++++ ...2020_12_21_215932_create_recipes_table.php | 2 ++ ...2021_03_06_090902_add_times_to_recipes.php | 33 +++++++++++++++++++ resources/views/recipes/edit.blade.php | 27 ++++++++++++++- 6 files changed, 83 insertions(+), 7 deletions(-) create mode 100644 database/migrations/2021_03_06_090902_add_times_to_recipes.php diff --git a/app/Http/Controllers/RecipeController.php b/app/Http/Controllers/RecipeController.php index 4c7a75e..eef0b21 100644 --- a/app/Http/Controllers/RecipeController.php +++ b/app/Http/Controllers/RecipeController.php @@ -152,11 +152,13 @@ class RecipeController extends Controller public function update(Request $request, Recipe $recipe): RedirectResponse { $input = $request->validate([ - 'name' => 'required|string', - 'description' => 'nullable|string', - 'source' => 'nullable|string', - 'servings' => 'required|numeric', - 'weight' => 'nullable|numeric', + 'name' => ['required', 'string'], + 'description' => ['nullable', 'string'], + 'servings' => ['required', 'numeric'], + 'time_prep' => ['nullable', 'numeric'], + 'time_active' => ['nullable', 'numeric'], + 'weight' => ['nullable', 'numeric'], + 'source' => ['nullable', 'string'], 'ingredients.amount' => ['required', 'array', new ArrayNotEmpty], 'ingredients.amount.*' => ['required_with:ingredients.id.*', 'nullable', new StringIsDecimalOrFraction], 'ingredients.unit' => ['required', 'array'], @@ -184,9 +186,11 @@ class RecipeController extends Controller $recipe->fill([ 'name' => Str::lower($input['name']), 'description' => $input['description'], - 'source' => $input['source'], 'servings' => (int) $input['servings'], 'weight' => $input['weight'], + 'time_prep' => (int) $input['time_prep'], + 'time_active' => (int) $input['time_active'], + 'source' => $input['source'], ]); try { diff --git a/app/JsonApi/Schemas/RecipeSchema.php b/app/JsonApi/Schemas/RecipeSchema.php index 250dc3c..6ba24a9 100644 --- a/app/JsonApi/Schemas/RecipeSchema.php +++ b/app/JsonApi/Schemas/RecipeSchema.php @@ -29,6 +29,9 @@ class RecipeSchema extends SchemaProvider 'slug' => $resource->slug, 'name' => $resource->name, 'description' => $resource->description, + 'time_prep' => $resource->time_prep, + 'time_active' => $resource->time_active, + 'time_total' => $resource->time_total, 'source' => $resource->source, 'servings' => $resource->servings, 'weight' => $resource->weight, diff --git a/app/Models/Recipe.php b/app/Models/Recipe.php index 5d1862e..5f264aa 100644 --- a/app/Models/Recipe.php +++ b/app/Models/Recipe.php @@ -73,6 +73,8 @@ final class Recipe extends Model protected $fillable = [ 'name', 'description', + 'time_prep', + 'time_active', 'source', 'servings', 'weight', @@ -83,6 +85,8 @@ final class Recipe extends Model */ protected $casts = [ 'servings' => 'int', + 'time_prep' => 'int', + 'time_active' => 'int', 'weight' => 'float', ]; @@ -103,6 +107,7 @@ final class Recipe extends Model */ protected $appends = [ 'serving_weight', + 'time_total', ]; /** @@ -120,6 +125,10 @@ final class Recipe extends Model ]; } + public function getTimeTotalAttribute(): int { + return $this->time_prep + $this->time_active; + } + /** * Get the serving weight (rounded). */ diff --git a/database/migrations/2020_12_21_215932_create_recipes_table.php b/database/migrations/2020_12_21_215932_create_recipes_table.php index b6cd285..3c29b07 100644 --- a/database/migrations/2020_12_21_215932_create_recipes_table.php +++ b/database/migrations/2020_12_21_215932_create_recipes_table.php @@ -18,6 +18,8 @@ class CreateRecipesTable extends Migration $table->string('name'); $table->string('slug')->unique(); $table->longText('description')->nullable(); + $table->integer('time_prep')->nullable(); + $table->integer('time_active')->nullable(); $table->string('source')->nullable(); $table->unsignedInteger('servings'); $table->unsignedFloat('weight')->nullable(); diff --git a/database/migrations/2021_03_06_090902_add_times_to_recipes.php b/database/migrations/2021_03_06_090902_add_times_to_recipes.php new file mode 100644 index 0000000..9da6ac9 --- /dev/null +++ b/database/migrations/2021_03_06_090902_add_times_to_recipes.php @@ -0,0 +1,33 @@ +integer('time_prep')->nullable()->after('description'); + $table->integer('time_active')->nullable()->after('time_prep'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('recipes', function (Blueprint $table) { + $table->dropColumn(['time_prep', 'time_active']); + }); + } +} diff --git a/resources/views/recipes/edit.blade.php b/resources/views/recipes/edit.blade.php index f330ee8..45f00b1 100644 --- a/resources/views/recipes/edit.blade.php +++ b/resources/views/recipes/edit.blade.php @@ -18,7 +18,8 @@ :value="old('name', $recipe->name)" required /> - + +
@@ -40,6 +41,30 @@ class="block mt-1 w-full" :value="old('weight', $recipe->weight)" />
+ + +
+ + + +
+ + +
+ + + +