Add recipe time fields (WIP)

Not present in th frontend yet.
This commit is contained in:
Christopher C. Wells 2021-03-06 09:30:15 -08:00
parent f643baf8ff
commit be681f0dd7
6 changed files with 83 additions and 7 deletions

View File

@ -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 {

View File

@ -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,

View File

@ -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).
*/

View File

@ -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();

View File

@ -0,0 +1,33 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class AddTimesToRecipes extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('recipes', function (Blueprint $table) {
$table->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']);
});
}
}

View File

@ -18,7 +18,8 @@
:value="old('name', $recipe->name)"
required />
</div>
</div>
<div class="flex flex-col space-y-4 mt-4 md:flex-row md:space-x-4 md:space-y-0">
<!-- Servings -->
<div class="flex-auto">
<x-inputs.label for="servings" value="Servings" />
@ -40,6 +41,30 @@
class="block mt-1 w-full"
:value="old('weight', $recipe->weight)" />
</div>
<!-- Prep Time -->
<div class="flex-auto">
<x-inputs.label for="time_prep" value="Prep time (minutes)" />
<x-inputs.input name="time_prep"
type="number"
step="1"
min="0"
class="block mt-1 w-full"
:value="old('name', $recipe->time_prep)"/>
</div>
<!-- Active Time -->
<div class="flex-auto">
<x-inputs.label for="time_active" value="Active time (minutes)" />
<x-inputs.input name="time_active"
type="number"
step="1"
min="0"
class="block mt-1 w-full"
:value="old('servings', $recipe->time_active)"/>
</div>
</div>
<div class="flex flex-col space-y-4 mt-4">
<!-- Source -->