diff --git a/app/Http/Controllers/GoalController.php b/app/Http/Controllers/GoalController.php index 061fdde..f6a88a2 100644 --- a/app/Http/Controllers/GoalController.php +++ b/app/Http/Controllers/GoalController.php @@ -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!"); diff --git a/app/Http/Requests/UpdateGoalRequest.php b/app/Http/Requests/UpdateGoalRequest.php index 8e8146d..bed92ba 100644 --- a/app/Http/Requests/UpdateGoalRequest.php +++ b/app/Http/Requests/UpdateGoalRequest.php @@ -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'], ]; } diff --git a/app/JsonApi/Adapters/GoalAdapter.php b/app/JsonApi/Adapters/GoalAdapter.php index bf3d2bb..7822107 100644 --- a/app/JsonApi/Adapters/GoalAdapter.php +++ b/app/JsonApi/Adapters/GoalAdapter.php @@ -23,7 +23,7 @@ class GoalAdapter extends AdapterBase /** * {@inheritdoc} */ - protected $defaultSort = ['-from', '-to']; + protected $defaultSort = ['-name']; /** * @inheritdoc diff --git a/app/JsonApi/Schemas/GoalSchema.php b/app/JsonApi/Schemas/GoalSchema.php index ba1bc8f..8a7c482 100644 --- a/app/JsonApi/Schemas/GoalSchema.php +++ b/app/JsonApi/Schemas/GoalSchema.php @@ -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, ]; } diff --git a/app/Models/Goal.php b/app/Models/Goal.php index 40731e7..0181374 100644 --- a/app/Models/Goal.php +++ b/app/Models/Goal.php @@ -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', diff --git a/database/migrations/2021_04_30_052739_refactor_goals_table.php b/database/migrations/2021_04_30_052739_refactor_goals_table.php index 54e28e1..9dbcada 100644 --- a/database/migrations/2021_04_30_052739_refactor_goals_table.php +++ b/database/migrations/2021_04_30_052739_refactor_goals_table.php @@ -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']); }); } diff --git a/resources/views/goals/edit.blade.php b/resources/views/goals/edit.blade.php index f9530a2..b9a1f16 100644 --- a/resources/views/goals/edit.blade.php +++ b/resources/views/goals/edit.blade.php @@ -21,26 +21,6 @@