mirror of https://github.com/kcal-app/kcal.git
Add recipe weight support
This commit is contained in:
parent
0b862dc7e9
commit
836c40abf2
|
|
@ -156,6 +156,7 @@ class RecipeController extends Controller
|
|||
'description' => 'nullable|string',
|
||||
'source' => 'nullable|string',
|
||||
'servings' => 'required|numeric',
|
||||
'weight' => 'nullable|numeric',
|
||||
'ingredients.amount' => ['required', 'array', new ArrayNotEmpty],
|
||||
'ingredients.amount.*' => ['required_with:ingredients.id.*', 'nullable', new StringIsDecimalOrFraction],
|
||||
'ingredients.unit' => ['required', 'array'],
|
||||
|
|
@ -185,6 +186,7 @@ class RecipeController extends Controller
|
|||
'description' => $input['description'],
|
||||
'source' => $input['source'],
|
||||
'servings' => (int) $input['servings'],
|
||||
'weight' => $input['weight'],
|
||||
]);
|
||||
|
||||
try {
|
||||
|
|
|
|||
|
|
@ -31,6 +31,8 @@ class RecipeSchema extends SchemaProvider
|
|||
'description' => $resource->description,
|
||||
'source' => $resource->source,
|
||||
'servings' => $resource->servings,
|
||||
'weight' => $resource->weight,
|
||||
'serving_weight' => $resource->serving_weight,
|
||||
'caloriesPerServing' => $resource->caloriesPerServing(),
|
||||
'carbohydratesPerServing' => $resource->carbohydratesPerServing(),
|
||||
'cholesterolPerServing' => $resource->cholesterolPerServing(),
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ use App\Models\Traits\HasIngredients;
|
|||
use App\Models\Traits\Ingredient;
|
||||
use App\Models\Traits\Journalable;
|
||||
use App\Models\Traits\Sluggable;
|
||||
use App\Support\Number;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
|
|
@ -65,6 +66,7 @@ final class Recipe extends Model
|
|||
'description',
|
||||
'source',
|
||||
'servings',
|
||||
'weight',
|
||||
];
|
||||
|
||||
/**
|
||||
|
|
@ -72,6 +74,7 @@ final class Recipe extends Model
|
|||
*/
|
||||
protected $casts = [
|
||||
'servings' => 'int',
|
||||
'weight' => 'float',
|
||||
];
|
||||
|
||||
/**
|
||||
|
|
@ -86,6 +89,23 @@ final class Recipe extends Model
|
|||
'sodiumPerServing',
|
||||
];
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected $appends = [
|
||||
'serving_weight',
|
||||
];
|
||||
|
||||
/**
|
||||
* Get the serving weight (rounded).
|
||||
*/
|
||||
public function getServingWeightAttribute(): ?float {
|
||||
if (empty($this->weight)) {
|
||||
return null;
|
||||
}
|
||||
return round($this->weight / $this->servings, 2);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the steps for this Recipe.
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -20,6 +20,7 @@ class CreateRecipesTable extends Migration
|
|||
$table->longText('description')->nullable();
|
||||
$table->string('source')->nullable();
|
||||
$table->unsignedInteger('servings');
|
||||
$table->unsignedFloat('weight')->nullable();
|
||||
$table->timestamps();
|
||||
});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,32 @@
|
|||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
class AddWeightColumnToRecipes extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
Schema::table('recipes', function (Blueprint $table) {
|
||||
$table->unsignedFloat('weight')->nullable()->after('servings');
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
Schema::table('recipes', function (Blueprint $table) {
|
||||
$table->dropColumn('weight');
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
@ -17,10 +17,9 @@
|
|||
<div class="flex-auto">
|
||||
<x-inputs.label for="name" value="Name" />
|
||||
|
||||
<x-inputs.input id="name"
|
||||
class="block mt-1 w-full"
|
||||
<x-inputs.input name="name"
|
||||
type="text"
|
||||
name="name"
|
||||
class="block mt-1 w-full"
|
||||
:value="old('name', $recipe->name)"
|
||||
required />
|
||||
</div>
|
||||
|
|
@ -29,33 +28,41 @@
|
|||
<div class="flex-auto">
|
||||
<x-inputs.label for="servings" value="Servings" />
|
||||
|
||||
<x-inputs.input id="servings"
|
||||
class="block mt-1 w-full"
|
||||
<x-inputs.input name="servings"
|
||||
type="number"
|
||||
name="servings"
|
||||
class="block mt-1 w-full"
|
||||
:value="old('servings', $recipe->servings)"
|
||||
required />
|
||||
</div>
|
||||
|
||||
<!-- Weight -->
|
||||
<div class="flex-auto">
|
||||
<x-inputs.label for="weight" value="Total weight (g)" />
|
||||
|
||||
<x-inputs.input name="weight"
|
||||
type="number"
|
||||
step="any"
|
||||
class="block mt-1 w-full"
|
||||
:value="old('weight', $recipe->weight)" />
|
||||
</div>
|
||||
</div>
|
||||
<div class="flex flex-col space-y-4 mt-4">
|
||||
<!-- Source -->
|
||||
<div class="flex-auto">
|
||||
<x-inputs.label for="source" value="Source" />
|
||||
|
||||
<x-inputs.input id="source"
|
||||
class="block mt-1 w-full"
|
||||
<x-inputs.input name="source"
|
||||
type="text"
|
||||
name="source"
|
||||
class="block mt-1 w-full"
|
||||
:value="old('source', $recipe->source)" />
|
||||
</div>
|
||||
</div>
|
||||
<div class="flex flex-col space-y-4 mt-4">
|
||||
|
||||
<!-- Description -->
|
||||
<div>
|
||||
<x-inputs.label for="description" value="Description" />
|
||||
|
||||
<x-inputs.textarea id="description"
|
||||
<x-inputs.textarea name="description"
|
||||
class="block mt-1 w-full"
|
||||
name="description"
|
||||
:value="old('description', $recipe->description)" />
|
||||
</div>
|
||||
|
||||
|
|
|
|||
|
|
@ -23,10 +23,14 @@
|
|||
<a x-bind:href="recipe.showUrl"
|
||||
class="hover:text-gray-600" x-text="recipe.name"></a>
|
||||
</div>
|
||||
<div class="flex justify-between font-bold border-b-8 border-black">
|
||||
<div class="flex justify-between">
|
||||
<div class="leading-snug" x-text="`${recipe.servings} servings`"></div>
|
||||
</div>
|
||||
<div class="font-bold text-right">Amount per serving</div>
|
||||
<div class="flex justify-between items-end font-extrabold" x-show="recipe.serving_weight">
|
||||
<div>Serving weight</div>
|
||||
<div x-text="`${recipe.serving_weight}g`"></div>
|
||||
</div>
|
||||
<div class="font-bold text-right border-t-8 border-black">Amount per serving</div>
|
||||
<div class="flex justify-between items-end font-extrabold">
|
||||
<div class="text-xl">Calories</div>
|
||||
<div class="text-xl" x-text="`${recipe.caloriesPerServing}`"></div>
|
||||
|
|
|
|||
|
|
@ -50,6 +50,12 @@
|
|||
<div class="p-1 border-2 border-black font-sans md:w-72">
|
||||
<div class="text-3xl font-extrabold leading-none">Nutrition Facts</div>
|
||||
<div class="leading-snug">{{ $recipe->servings }} {{ \Illuminate\Support\Str::plural('serving', $recipe->servings ) }}</div>
|
||||
@if($recipe->serving_weight)
|
||||
<div class="flex justify-between items-end font-extrabold">
|
||||
<div>Serving weight</div>
|
||||
<div>{{ $recipe->serving_weight }}g</div>
|
||||
</div>
|
||||
@endif
|
||||
<div class="font-bold text-right border-t-8 border-black">Amount per serving</div>
|
||||
<div class="flex justify-between items-end font-extrabold">
|
||||
<div class="text-3xl">Calories</div>
|
||||
|
|
|
|||
Loading…
Reference in New Issue