From c7d0cda807c8c45cedee6af08200de8479428dbd Mon Sep 17 00:00:00 2001 From: "Christopher C. Wells" Date: Fri, 30 Apr 2021 05:44:19 -0700 Subject: [PATCH] Refactor Goal model --- app/Models/Goal.php | 77 ++++++++----------- app/Models/User.php | 24 +----- ...2021_04_30_052739_refactor_goals_table.php | 44 +++++++++++ 3 files changed, 79 insertions(+), 66 deletions(-) create mode 100644 database/migrations/2021_04_30_052739_refactor_goals_table.php diff --git a/app/Models/Goal.php b/app/Models/Goal.php index 3c66e9e..e841ded 100644 --- a/app/Models/Goal.php +++ b/app/Models/Goal.php @@ -15,51 +15,56 @@ use Illuminate\Support\Str; * @property int $user_id * @property \datetime|null $from * @property \datetime|null $to - * @property string|null $frequency + * @property int $days * @property string $name - * @property float $goal * @property \Illuminate\Support\Carbon|null $created_at * @property \Illuminate\Support\Carbon|null $updated_at - * @property-read string $summary + * @property float|null $calories + * @property float|null $fat + * @property float|null $cholesterol + * @property float|null $sodium + * @property float|null $carbohydrates + * @property float|null $protein * @property-read \App\Models\User $user + * @method static \Database\Factories\GoalFactory factory(...$parameters) * @method static \Illuminate\Database\Eloquent\Builder|Goal newModelQuery() * @method static \Illuminate\Database\Eloquent\Builder|Goal newQuery() * @method static \Illuminate\Database\Eloquent\Builder|Goal query() + * @method static \Illuminate\Database\Eloquent\Builder|Goal whereCalories($value) + * @method static \Illuminate\Database\Eloquent\Builder|Goal whereCarbohydrates($value) + * @method static \Illuminate\Database\Eloquent\Builder|Goal whereCholesterol($value) * @method static \Illuminate\Database\Eloquent\Builder|Goal whereCreatedAt($value) - * @method static \Illuminate\Database\Eloquent\Builder|Goal whereFrequency($value) + * @method static \Illuminate\Database\Eloquent\Builder|Goal whereDays($value) + * @method static \Illuminate\Database\Eloquent\Builder|Goal whereFat($value) * @method static \Illuminate\Database\Eloquent\Builder|Goal whereFrom($value) - * @method static \Illuminate\Database\Eloquent\Builder|Goal whereGoal($value) * @method static \Illuminate\Database\Eloquent\Builder|Goal whereId($value) * @method static \Illuminate\Database\Eloquent\Builder|Goal whereName($value) + * @method static \Illuminate\Database\Eloquent\Builder|Goal whereProtein($value) + * @method static \Illuminate\Database\Eloquent\Builder|Goal whereSodium($value) * @method static \Illuminate\Database\Eloquent\Builder|Goal whereTo($value) * @method static \Illuminate\Database\Eloquent\Builder|Goal whereUpdatedAt($value) * @method static \Illuminate\Database\Eloquent\Builder|Goal whereUserId($value) * @mixin \Eloquent - * @method static \Database\Factories\GoalFactory factory(...$parameters) */ final class Goal extends Model { use HasFactory; - /** - * Supported options for thr frequency attribute. - */ - public static array $frequencyOptions = [ - 'daily' => [ - 'value' => 'daily', - 'label' => 'daily' - ], - ]; - /** * @inheritdoc */ protected $fillable = [ - 'frequency', - 'from', - 'goal', 'name', + 'from', 'to', + // Bitwise field: sun=1, mon=2, tue=4, wed=8, thu=16, fri=32, sat=64. + 'days', + 'calories', + 'carbohydrates', + 'cholesterol', + 'fat', + 'protein', + 'sodium', ]; /** @@ -67,15 +72,14 @@ final class Goal extends Model */ protected $casts = [ 'from' => 'datetime:Y-m-d', - 'goal' => 'float', 'to' => 'datetime:Y-m-d', - ]; - - /** - * @inheritdoc - */ - protected $appends = [ - 'summary', + 'days' => 'int', + 'calories' => 'float', + 'carbohydrates' => 'float', + 'cholesterol' => 'float', + 'fat' => 'float', + 'protein' => 'float', + 'sodium' => 'float', ]; /** @@ -85,23 +89,4 @@ final class Goal extends Model return $this->belongsTo(User::class); } - public function getSummaryAttribute(): string { - $nameOptions = self::getNameOptions(); - return number_format($this->goal) . "{$nameOptions[$this->name]['unit']} {$nameOptions[$this->name]['label']} {$this->frequency}"; - } - - /** - * Get options for the "name" column. - */ - public static function getNameOptions(): array { - $options = []; - foreach (Nutrients::all() as $nutrient) { - $options[$nutrient['value']] = [ - 'value' => $nutrient['value'], - 'label' => Str::ucfirst($nutrient['label']), - 'unit' => $nutrient['unit'], - ]; - } - return $options; - } } diff --git a/app/Models/User.php b/app/Models/User.php index e84dcc6..9cfa537 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -108,28 +108,12 @@ final class User extends Authenticatable implements HasMedia /** * Get User's past, present, and future goals. * - * @return \App\Models\Goal[] + * @todo Refactor or remove as needed. + * + * @return \Illuminate\Support\Collection[] */ public function getGoalsByTime(?Carbon $date = null): array { - $now = $date ?? Carbon::now(); - $goals = ['past' => new Collection(), 'present' => new Collection(), 'future' => new Collection()]; - Goal::all()->where('user_id', Auth::user()->id) - ->each(function ($item) use(&$goals, $now) { - if ($item->to && $now->isAfter($item->to)) { - $goals['past'][$item->id] = $item; - } - elseif ($item->from && $now->isBefore($item->from)) { - $goals['future'][$item->id] = $item; - } - elseif ( - empty($item->from) - || empty($item->to) - || $now->isBetween($item->from, $item->to) - ) { - $goals['present'][$item->id] = $item; - } - }); - return $goals; + return ['past' => new Collection(), 'present' => new Collection(), 'future' => new Collection()]; } /** diff --git a/database/migrations/2021_04_30_052739_refactor_goals_table.php b/database/migrations/2021_04_30_052739_refactor_goals_table.php new file mode 100644 index 0000000..54e28e1 --- /dev/null +++ b/database/migrations/2021_04_30_052739_refactor_goals_table.php @@ -0,0 +1,44 @@ +truncate(); + Schema::table('goals', function (Blueprint $table) { + $table->unsignedTinyInteger('days')->default(127)->after('to'); + $table->unsignedFloat('calories')->nullable()->after('name'); + $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']); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + DB::table('goals')->truncate(); + Schema::table('goals', function (Blueprint $table) { + $table->string('frequency')->nullable()->after('to'); + $table->unsignedFloat('goal')->nullable()->after('name'); + $table->dropColumn(['days', 'calories', 'fat', 'cholesterol', 'sodium', 'carbohydrates', 'protein']); + }); + } +}