Remove Goal `from` and `to` attributes

This commit is contained in:
Christopher C. Wells 2021-05-08 16:06:16 -07:00 committed by Christopher Charbonneau Wells
parent 3ae6c6ef13
commit 153611e66d
9 changed files with 25 additions and 54 deletions

View File

@ -59,6 +59,7 @@ class GoalController extends Controller
public function update(UpdateGoalRequest $request, Goal $goal): RedirectResponse
{
$attributes = $request->validated();
$attributes['days'] = array_sum($attributes['days']);
$goal->fill($attributes)->user()->associate(Auth::user());
$goal->save();
session()->flash('message', "Goal updated!");

View File

@ -14,11 +14,15 @@ class UpdateGoalRequest extends FormRequest
public function rules(): array
{
return [
'from' => ['nullable', 'date'],
'to' => ['nullable', 'date'],
'frequency' => ['required', 'string'],
'name' => ['required', 'string'],
'goal' => ['required', 'numeric', 'min:0'],
'days' => ['nullable', 'array'],
'days.*' => ['nullable', 'numeric', 'min:0', 'max:64'],
'calories' => ['nullable', 'numeric', 'min:0'],
'fat' => ['nullable', 'numeric', 'min:0'],
'cholesterol' => ['nullable', 'numeric', 'min:0'],
'sodium' => ['nullable', 'numeric', 'min:0'],
'carbohydrates' => ['nullable', 'numeric', 'min:0'],
'protein' => ['nullable', 'numeric', 'min:0'],
];
}

View File

@ -23,7 +23,7 @@ class GoalAdapter extends AdapterBase
/**
* {@inheritdoc}
*/
protected $defaultSort = ['-from', '-to'];
protected $defaultSort = ['-name'];
/**
* @inheritdoc

View File

@ -26,11 +26,14 @@ class GoalSchema extends SchemaProvider
public function getAttributes($resource): array
{
return [
'frequency' => $resource->frequency,
'from' => $resource->from,
'goal' => $resource->goal,
'name' => $resource->name,
'to' => $resource->to,
'days' => $resource->days,
'calories' => $resource->calories,
'carbohydrates' => $resource->carbohydrates,
'cholesterol' => $resource->cholesterol,
'fat' => $resource->fat,
'protein' => $resource->protein,
'sodium' => $resource->sodium,
];
}

View File

@ -12,8 +12,6 @@ use Illuminate\Support\Collection;
*
* @property int $id
* @property int $user_id
* @property \datetime|null $from
* @property \datetime|null $to
* @property int $days
* @property string $name
* @property \Illuminate\Support\Carbon|null $created_at
@ -54,8 +52,6 @@ final class Goal extends Model
*/
protected $fillable = [
'name',
'from',
'to',
// Bitwise field: mon=1, tue=2, wed=4, thu=8, fri=16, sat=32, sun=64.
'days',
'calories',
@ -70,8 +66,6 @@ final class Goal extends Model
* @inheritdoc
*/
protected $casts = [
'from' => 'datetime:Y-m-d',
'to' => 'datetime:Y-m-d',
'days' => 'int',
'calories' => 'float',
'carbohydrates' => 'float',

View File

@ -16,14 +16,14 @@ class RefactorGoalsTable extends Migration
{
DB::table('goals')->truncate();
Schema::table('goals', function (Blueprint $table) {
$table->unsignedTinyInteger('days')->default(127)->after('to');
$table->unsignedFloat('calories')->nullable()->after('name');
$table->unsignedTinyInteger('days')->default(127)->after('name');
$table->unsignedFloat('calories')->nullable()->after('days');
$table->unsignedFloat('fat')->nullable()->after('calories');
$table->unsignedFloat('cholesterol')->nullable()->after('fat');
$table->unsignedFloat('sodium')->nullable()->after('cholesterol');
$table->unsignedFloat('carbohydrates')->nullable()->after('sodium');
$table->unsignedFloat('protein')->nullable()->after('carbohydrates');
$table->dropColumn(['frequency', 'goal']);
$table->dropColumn(['frequency', 'from', 'goal', 'to']);
});
}
@ -36,8 +36,10 @@ class RefactorGoalsTable extends Migration
{
DB::table('goals')->truncate();
Schema::table('goals', function (Blueprint $table) {
$table->string('frequency')->nullable()->after('to');
$table->unsignedFloat('goal')->nullable()->after('name');
$table->date('from')->nullable();
$table->date('to')->nullable();
$table->string('frequency')->nullable();
$table->unsignedFloat('goal')->nullable();
$table->dropColumn(['days', 'calories', 'fat', 'cholesterol', 'sodium', 'carbohydrates', 'protein']);
});
}

View File

@ -21,26 +21,6 @@
</div>
</div>
<div class="flex flex-col space-y-4 md:flex-row md:space-x-4 md:space-y-0">
<!-- From -->
<div class="flex-auto">
<x-inputs.label for="from" value="From"/>
<x-inputs.input name="from"
type="date"
class="block w-full"
:value="old('from', $goal->from?->toDateString())"
:hasError="$errors->has('from')" />
</div>
<!-- To -->
<div class="flex-auto">
<x-inputs.label for="to" value="To"/>
<x-inputs.input name="to"
type="date"
class="block w-full"
:value="old('to', $goal->to?->toDateString())"
:hasError="$errors->has('to')" />
</div>
<!-- Days of the week -->
<fieldset class="flex-auto">
<legend class="block font-medium text-sm text-gray-700">Days of the week</legend>

View File

@ -12,8 +12,6 @@
<thead>
<tr class="bg-gray-200 text-gray-600 uppercase text-sm leading-normal">
<th class="py-3 px-6 text-left">Name</th>
<th class="py-3 px-6 text-left">From</th>
<th class="py-3 px-6 text-left">To</th>
<th class="py-3 px-6 text-left">Days of Week</th>
<th class="py-3 px-6 text-left">Goals</th>
<th class="py-3 px-6 text-left">&nbsp;</th>
@ -23,8 +21,6 @@
@foreach($goals as $goal)
<tr class="border-b border-gray-200">
<td class="py-3 px-6">{{ $goal->name }}</td>
<td class="py-3 px-6">{{ $goal->from }}</td>
<td class="py-3 px-6">{{ $goal->to }}</td>
<td class="py-3 px-6">{{ $goal->days }}</td>
<td class="py-3 px-6">
Calories: {{ $goal->calories }}<br />

View File

@ -19,15 +19,6 @@
</h1>
</x-slot>
<div class="grid grid-cols-2 gap-y-1 gap-x-3 max-w-md inline-grid">
<div class="font-bold">From</div>
<div>{{ $goal->from?->toDateString() ?? 'Any' }}</div>
<div class="font-bold">To</div>
<div>{{ $goal->to?->toDateString() ?? 'Any' }}</div>
<div class="font-bold">Frequency</div>
<div>{{ \Illuminate\Support\Str::ucfirst($frequencyOptions[$goal->frequency]['label']) }}</div>
<div class="font-bold">Trackable</div>
<div>{{ \Illuminate\Support\Str::ucfirst($nameOptions[$goal->name]['label']) }}</div>
<div class="font-bold">Goal</div>
<div>{{ $goal->goal }}{{ $nameOptions[$goal->name]['unit'] }}</div>
@todo Add data!
</div>
</x-app-layout>