This commit is contained in:
Adrian Hopek
2022-01-21 08:45:42 +01:00
parent 652587dbf1
commit a62a428781
12 changed files with 420 additions and 8 deletions

View File

@@ -0,0 +1,40 @@
<?php
declare(strict_types=1);
namespace Toby\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Carbon;
use Inertia\Response;
use Toby\Http\Resources\VacationLimitResource;
use Toby\Http\Resources\YearPeriodResource;
use Toby\Models\YearPeriod;
class VacationDaysController extends Controller
{
public function edit(Request $request): Response
{
$year = $request->query("year", Carbon::now()->year);
/** @var YearPeriod $yearPeriod */
$yearPeriod = YearPeriod::query()->where("year", $year)->firstOrFail();
$previousYearPeriod = YearPeriod::query()->where("year", $year - 1)->first();
$nextYearPeriod = YearPeriod::query()->where("year", $year + 1)->first();
return inertia("VacationDays", [
"vacationLimits" => VacationLimitResource::collection($yearPeriod->vacationLimits()->with("user")->get()),
"yearPeriods" => [
"prev" => $previousYearPeriod ? new YearPeriodResource($previousYearPeriod) : null,
"current" => new YearPeriodResource($yearPeriod),
"next" => $nextYearPeriod ? new YearPeriodResource($nextYearPeriod) : null,
],
]);
}
public function update(Request $request)
{
dump($request->get("items"));
}
}

View File

@@ -8,7 +8,7 @@ use Illuminate\Http\Resources\Json\JsonResource;
class UserResource extends JsonResource
{
public static $wrap = false;
public static $wrap = null;
public function toArray($request): array
{

View File

@@ -0,0 +1,21 @@
<?php
declare(strict_types=1);
namespace Toby\Http\Resources;
use Illuminate\Http\Resources\Json\JsonResource;
class VacationLimitResource extends JsonResource
{
public static $wrap = null;
public function toArray($request): array
{
return [
"user" => new UserResource($this->user),
"hasVacation" => $this->has_vacation,
"days" => $this->days,
];
}
}

View File

@@ -0,0 +1,20 @@
<?php
declare(strict_types=1);
namespace Toby\Http\Resources;
use Illuminate\Http\Resources\Json\JsonResource;
class YearPeriodResource extends JsonResource
{
public static $wrap = false;
public function toArray($request): array
{
return [
"id" => $this->id,
"year" => $this->year,
];
}
}

View File

@@ -0,0 +1,35 @@
<?php
declare(strict_types=1);
namespace Toby\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
/**
* @property int $id
* @property User $user
* @property YearPeriod $yearPeriod
* @property bool $has_vacation
* @property int $days
*/
class VacationLimit extends Model
{
use HasFactory;
protected $casts = [
"has_vacation" => "boolean",
];
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
public function yearPeriod(): BelongsTo
{
return $this->belongsTo(YearPeriod::class);
}
}

View File

@@ -7,10 +7,13 @@ namespace Toby\Models;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Support\Collection;
/**
* @property int $id
* @property int $year
* @property Collection $vacationLimits
*/
class YearPeriod extends Model
{
@@ -27,4 +30,9 @@ class YearPeriod extends Model
return $year;
}
public function vacationLimits(): HasMany
{
return $this->hasMany(VacationLimit::class);
}
}