From a62a428781bff592d0dff1be9203eecb69e0c28c Mon Sep 17 00:00:00 2001 From: Adrian Hopek Date: Fri, 21 Jan 2022 08:45:42 +0100 Subject: [PATCH] #23 - wip --- .../Controllers/VacationDaysController.php | 40 ++++ app/Http/Resources/UserResource.php | 2 +- app/Http/Resources/VacationLimitResource.php | 21 ++ app/Http/Resources/YearPeriodResource.php | 20 ++ app/Models/VacationLimit.php | 35 +++ app/Models/YearPeriod.php | 8 + database/factories/VacationLimitFactory.php | 24 ++ ...19_140630_create_vacation_limits_table.php | 28 +++ database/seeders/DatabaseSeeder.php | 25 +- resources/js/Pages/VacationDays.vue | 218 ++++++++++++++++++ resources/js/Shared/MainMenu.vue | 3 +- routes/web.php | 4 + 12 files changed, 420 insertions(+), 8 deletions(-) create mode 100644 app/Http/Controllers/VacationDaysController.php create mode 100644 app/Http/Resources/VacationLimitResource.php create mode 100644 app/Http/Resources/YearPeriodResource.php create mode 100644 app/Models/VacationLimit.php create mode 100644 database/factories/VacationLimitFactory.php create mode 100644 database/migrations/2022_01_19_140630_create_vacation_limits_table.php create mode 100644 resources/js/Pages/VacationDays.vue diff --git a/app/Http/Controllers/VacationDaysController.php b/app/Http/Controllers/VacationDaysController.php new file mode 100644 index 0000000..d6575fc --- /dev/null +++ b/app/Http/Controllers/VacationDaysController.php @@ -0,0 +1,40 @@ +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")); + } +} diff --git a/app/Http/Resources/UserResource.php b/app/Http/Resources/UserResource.php index c9217a6..132092b 100644 --- a/app/Http/Resources/UserResource.php +++ b/app/Http/Resources/UserResource.php @@ -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 { diff --git a/app/Http/Resources/VacationLimitResource.php b/app/Http/Resources/VacationLimitResource.php new file mode 100644 index 0000000..ec461ed --- /dev/null +++ b/app/Http/Resources/VacationLimitResource.php @@ -0,0 +1,21 @@ + new UserResource($this->user), + "hasVacation" => $this->has_vacation, + "days" => $this->days, + ]; + } +} diff --git a/app/Http/Resources/YearPeriodResource.php b/app/Http/Resources/YearPeriodResource.php new file mode 100644 index 0000000..4b19f83 --- /dev/null +++ b/app/Http/Resources/YearPeriodResource.php @@ -0,0 +1,20 @@ + $this->id, + "year" => $this->year, + ]; + } +} diff --git a/app/Models/VacationLimit.php b/app/Models/VacationLimit.php new file mode 100644 index 0000000..dc8ef42 --- /dev/null +++ b/app/Models/VacationLimit.php @@ -0,0 +1,35 @@ + "boolean", + ]; + + public function user(): BelongsTo + { + return $this->belongsTo(User::class); + } + + public function yearPeriod(): BelongsTo + { + return $this->belongsTo(YearPeriod::class); + } +} diff --git a/app/Models/YearPeriod.php b/app/Models/YearPeriod.php index bcf2096..569ad03 100644 --- a/app/Models/YearPeriod.php +++ b/app/Models/YearPeriod.php @@ -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); + } } diff --git a/database/factories/VacationLimitFactory.php b/database/factories/VacationLimitFactory.php new file mode 100644 index 0000000..d5f3d6d --- /dev/null +++ b/database/factories/VacationLimitFactory.php @@ -0,0 +1,24 @@ +faker->boolean(75); + + return [ + "user_id" => User::factory(), + "year_period_id" => YearPeriod::factory(), + "has_vacation" => $hasVacation, + "days" => $hasVacation ? $this->faker->numberBetween(20, 26) : null, + ]; + } +} diff --git a/database/migrations/2022_01_19_140630_create_vacation_limits_table.php b/database/migrations/2022_01_19_140630_create_vacation_limits_table.php new file mode 100644 index 0000000..eab4fdf --- /dev/null +++ b/database/migrations/2022_01_19_140630_create_vacation_limits_table.php @@ -0,0 +1,28 @@ +id(); + $table->foreignIdFor(User::class)->constrained()->cascadeOnDelete(); + $table->foreignIdFor(YearPeriod::class)->constrained()->cascadeOnDelete(); + $table->boolean("has_vacation")->default(false); + $table->integer("days")->nullable(); + $table->timestamps(); + }); + } + + public function down(): void + { + Schema::dropIfExists('vacation_limits'); + } +}; diff --git a/database/seeders/DatabaseSeeder.php b/database/seeders/DatabaseSeeder.php index af2722b..e2b867c 100644 --- a/database/seeders/DatabaseSeeder.php +++ b/database/seeders/DatabaseSeeder.php @@ -7,6 +7,7 @@ namespace Database\Seeders; use Illuminate\Database\Seeder; use Illuminate\Support\Carbon; use Toby\Models\User; +use Toby\Models\VacationLimit; use Toby\Models\YearPeriod; class DatabaseSeeder extends Seeder @@ -18,11 +19,23 @@ class DatabaseSeeder extends Seeder "email" => env("LOCAL_EMAIL_FOR_LOGIN_VIA_GOOGLE"), ])->create(); - YearPeriod::factory([ - "year" => Carbon::now()->year, - ])->create(); - YearPeriod::factory([ - "year" => Carbon::now()->year + 1, - ])->create(); + $users = User::all(); + + YearPeriod::factory() + ->count(3) + ->sequence( + ["year" => Carbon::now()->year - 1], + ["year" => Carbon::now()->year], + ["year" => Carbon::now()->year + 1], + ) + ->afterCreating(function (YearPeriod $yearPeriod) use ($users) { + foreach ($users as $user) { + VacationLimit::factory() + ->for($yearPeriod) + ->for($user) + ->create(); + } + }) + ->create(); } } diff --git a/resources/js/Pages/VacationDays.vue b/resources/js/Pages/VacationDays.vue new file mode 100644 index 0000000..9c78434 --- /dev/null +++ b/resources/js/Pages/VacationDays.vue @@ -0,0 +1,218 @@ + + + diff --git a/resources/js/Shared/MainMenu.vue b/resources/js/Shared/MainMenu.vue index 7373e89..b40ae7c 100644 --- a/resources/js/Shared/MainMenu.vue +++ b/resources/js/Shared/MainMenu.vue @@ -266,10 +266,11 @@ export default { }, setup() { const user = computed(() => usePage().props.value.auth.user); + const navigation = [ {name: 'Strona główna', href: '/', current: true}, {name: 'Użytkownicy', href: '/users', current: false}, - {name: 'Resources', href: '#', current: false}, + {name: 'Dostępne urlopy', href: '/vacation-days', current: false}, {name: 'Company Directory', href: '#', current: false}, {name: 'Openings', href: '#', current: false}, ]; diff --git a/routes/web.php b/routes/web.php index 5479b3e..1b2f387 100644 --- a/routes/web.php +++ b/routes/web.php @@ -6,6 +6,7 @@ use Illuminate\Support\Facades\Route; use Toby\Http\Controllers\GoogleController; use Toby\Http\Controllers\LogoutController; use Toby\Http\Controllers\UserController; +use Toby\Http\Controllers\VacationDaysController; Route::middleware("auth")->group(function (): void { Route::get("/", fn() => inertia("Dashboard"))->name("dashboard"); @@ -13,6 +14,9 @@ Route::middleware("auth")->group(function (): void { Route::resource("users", UserController::class); Route::post("users/{user}/restore", [UserController::class, "restore"])->withTrashed(); + + Route::get("/vacation-days", [VacationDaysController::class, "edit"])->name("vacation.days"); + Route::put("/vacation-days", [VacationDaysController::class, "update"]); }); Route::middleware("guest")->group(function (): void {