mirror of https://github.com/kcal-app/kcal.git
Refactor Goal model
This commit is contained in:
parent
0bfba72f72
commit
c7d0cda807
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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()];
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -0,0 +1,44 @@
|
|||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
class RefactorGoalsTable extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
DB::table('goals')->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']);
|
||||
});
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue