mirror of https://github.com/kcal-app/kcal.git
Add formatted days attribute to Goal
This commit is contained in:
parent
b21f483af2
commit
77b06c62b1
|
@ -59,7 +59,9 @@ class GoalController extends Controller
|
||||||
public function update(UpdateGoalRequest $request, Goal $goal): RedirectResponse
|
public function update(UpdateGoalRequest $request, Goal $goal): RedirectResponse
|
||||||
{
|
{
|
||||||
$attributes = $request->validated();
|
$attributes = $request->validated();
|
||||||
$attributes['days'] = array_sum($attributes['days']);
|
if (isset($attributes['days'])) {
|
||||||
|
$attributes['days'] = array_sum($attributes['days']);
|
||||||
|
}
|
||||||
$goal->fill($attributes)->user()->associate(Auth::user());
|
$goal->fill($attributes)->user()->associate(Auth::user());
|
||||||
$goal->save();
|
$goal->save();
|
||||||
session()->flash('message', "Goal updated!");
|
session()->flash('message', "Goal updated!");
|
||||||
|
|
|
@ -25,6 +25,7 @@ class FoodSchema extends SchemaProvider
|
||||||
*/
|
*/
|
||||||
public function getAttributes($resource): array
|
public function getAttributes($resource): array
|
||||||
{
|
{
|
||||||
|
/** @var \App\Models\Food $resource */
|
||||||
return [
|
return [
|
||||||
'slug' => $resource->slug,
|
'slug' => $resource->slug,
|
||||||
'name' => $resource->name,
|
'name' => $resource->name,
|
||||||
|
|
|
@ -29,6 +29,7 @@ class GoalSchema extends SchemaProvider
|
||||||
return [
|
return [
|
||||||
'name' => $resource->name,
|
'name' => $resource->name,
|
||||||
'days' => $resource->days,
|
'days' => $resource->days,
|
||||||
|
'daysFormatted' => $resource->days_formatted,
|
||||||
'calories' => $resource->calories,
|
'calories' => $resource->calories,
|
||||||
'carbohydrates' => $resource->carbohydrates,
|
'carbohydrates' => $resource->carbohydrates,
|
||||||
'cholesterol' => $resource->cholesterol,
|
'cholesterol' => $resource->cholesterol,
|
||||||
|
|
|
@ -25,6 +25,7 @@ class IngredientAmountSchema extends SchemaProvider
|
||||||
*/
|
*/
|
||||||
public function getAttributes($resource): array
|
public function getAttributes($resource): array
|
||||||
{
|
{
|
||||||
|
/** @var \App\Models\IngredientAmount $resource */
|
||||||
return [
|
return [
|
||||||
'amount' => $resource->amount,
|
'amount' => $resource->amount,
|
||||||
'amountFormatted' => $resource->amount_formatted,
|
'amountFormatted' => $resource->amount_formatted,
|
||||||
|
|
|
@ -25,6 +25,7 @@ class JournalEntrySchema extends SchemaProvider
|
||||||
*/
|
*/
|
||||||
public function getAttributes($resource): array
|
public function getAttributes($resource): array
|
||||||
{
|
{
|
||||||
|
/** @var \App\Models\JournalEntry $resource */
|
||||||
return [
|
return [
|
||||||
'calories' => $resource->calories,
|
'calories' => $resource->calories,
|
||||||
'carbohydrates' => $resource->carbohydrates,
|
'carbohydrates' => $resource->carbohydrates,
|
||||||
|
|
|
@ -25,6 +25,7 @@ class RecipeSchema extends SchemaProvider
|
||||||
*/
|
*/
|
||||||
public function getAttributes($resource): array
|
public function getAttributes($resource): array
|
||||||
{
|
{
|
||||||
|
/** @var \App\Models\Recipe $resource */
|
||||||
return [
|
return [
|
||||||
'slug' => $resource->slug,
|
'slug' => $resource->slug,
|
||||||
'name' => $resource->name,
|
'name' => $resource->name,
|
||||||
|
|
|
@ -25,6 +25,7 @@ class RecipeSeparatorSchema extends SchemaProvider
|
||||||
*/
|
*/
|
||||||
public function getAttributes($resource): array
|
public function getAttributes($resource): array
|
||||||
{
|
{
|
||||||
|
/** @var \App\Models\RecipeSeparator $resource */
|
||||||
return [
|
return [
|
||||||
'container' => $resource->container,
|
'container' => $resource->container,
|
||||||
'weight' => $resource->weight,
|
'weight' => $resource->weight,
|
||||||
|
|
|
@ -25,6 +25,7 @@ class RecipeStepSchema extends SchemaProvider
|
||||||
*/
|
*/
|
||||||
public function getAttributes($resource): array
|
public function getAttributes($resource): array
|
||||||
{
|
{
|
||||||
|
/** @var \App\Models\RecipeStep $resource */
|
||||||
return [
|
return [
|
||||||
'number' => $resource->number,
|
'number' => $resource->number,
|
||||||
'step' => $resource->step,
|
'step' => $resource->step,
|
||||||
|
|
|
@ -25,6 +25,7 @@ class TagSchema extends SchemaProvider
|
||||||
*/
|
*/
|
||||||
public function getAttributes($resource): array
|
public function getAttributes($resource): array
|
||||||
{
|
{
|
||||||
|
/** @var \App\Models\Tag $resource */
|
||||||
return [
|
return [
|
||||||
'name' => $resource->name,
|
'name' => $resource->name,
|
||||||
'slug' => $resource->slug,
|
'slug' => $resource->slug,
|
||||||
|
|
|
@ -25,6 +25,7 @@ class UserSchema extends SchemaProvider
|
||||||
*/
|
*/
|
||||||
public function getAttributes($resource): array
|
public function getAttributes($resource): array
|
||||||
{
|
{
|
||||||
|
/** @var \App\Models\User $resource */
|
||||||
return [
|
return [
|
||||||
'username' => $resource->username,
|
'username' => $resource->username,
|
||||||
'name' => $resource->name,
|
'name' => $resource->name,
|
||||||
|
|
|
@ -42,6 +42,7 @@ use Illuminate\Support\Collection;
|
||||||
* @method static \Illuminate\Database\Eloquent\Builder|Goal whereUpdatedAt($value)
|
* @method static \Illuminate\Database\Eloquent\Builder|Goal whereUpdatedAt($value)
|
||||||
* @method static \Illuminate\Database\Eloquent\Builder|Goal whereUserId($value)
|
* @method static \Illuminate\Database\Eloquent\Builder|Goal whereUserId($value)
|
||||||
* @mixin \Eloquent
|
* @mixin \Eloquent
|
||||||
|
* @property-read array $days_formatted
|
||||||
*/
|
*/
|
||||||
final class Goal extends Model
|
final class Goal extends Model
|
||||||
{
|
{
|
||||||
|
@ -75,6 +76,31 @@ final class Goal extends Model
|
||||||
'sodium' => 'float',
|
'sodium' => 'float',
|
||||||
];
|
];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @inheritdoc
|
||||||
|
*/
|
||||||
|
protected $appends = [
|
||||||
|
'days_formatted',
|
||||||
|
];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the days for the goals as strings in array keyed by dow.
|
||||||
|
*/
|
||||||
|
public function getDaysFormattedAttribute(): array {
|
||||||
|
$days = [];
|
||||||
|
if (empty($this->days)) {
|
||||||
|
return $days;
|
||||||
|
}
|
||||||
|
|
||||||
|
self::days()->each(function ($day) use (&$days) {
|
||||||
|
if (($this->days & $day['value']) != 0) {
|
||||||
|
$days[$day['dow']] = $day['label'];
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return $days;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get all supported days and metadata.
|
* Get all supported days and metadata.
|
||||||
*
|
*
|
||||||
|
|
|
@ -150,8 +150,6 @@ final class IngredientAmount extends Model
|
||||||
* @param array $parameters
|
* @param array $parameters
|
||||||
*
|
*
|
||||||
* @return mixed
|
* @return mixed
|
||||||
*
|
|
||||||
* @noinspection PhpMissingParamTypeInspection
|
|
||||||
*/
|
*/
|
||||||
public function __call($method, $parameters): mixed {
|
public function __call($method, $parameters): mixed {
|
||||||
if (in_array($method, $this->nutrientMethods)) {
|
if (in_array($method, $this->nutrientMethods)) {
|
||||||
|
|
|
@ -27,6 +27,7 @@ use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||||
* @method static \Illuminate\Database\Eloquent\Builder|JournalDate whereUpdatedAt($value)
|
* @method static \Illuminate\Database\Eloquent\Builder|JournalDate whereUpdatedAt($value)
|
||||||
* @method static \Illuminate\Database\Eloquent\Builder|JournalDate whereUserId($value)
|
* @method static \Illuminate\Database\Eloquent\Builder|JournalDate whereUserId($value)
|
||||||
* @mixin \Eloquent
|
* @mixin \Eloquent
|
||||||
|
* @method static \Database\Factories\JournalDateFactory factory(...$parameters)
|
||||||
*/
|
*/
|
||||||
final class JournalDate extends Model
|
final class JournalDate extends Model
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue