This commit is contained in:
Adrian Hopek
2022-01-24 10:19:26 +01:00
parent b4c6cfe612
commit 75889a16e6
12 changed files with 223 additions and 24 deletions

View File

@@ -5,7 +5,6 @@ declare(strict_types=1);
namespace Toby\Http\Controllers;
use Illuminate\Http\RedirectResponse;
use Illuminate\Support\Arr;
use Inertia\Response;
use Toby\Http\Requests\VacationLimitRequest;
use Toby\Http\Resources\VacationLimitResource;
@@ -22,14 +21,14 @@ class VacationLimitController extends Controller
public function update(VacationLimitRequest $request): RedirectResponse
{
foreach ($request->data() as $data) {
$limit = VacationLimit::query()->find($data["id"]);
$data = $request->data();
$limit->update(Arr::only($data, ["has_vacation", "days"]));
}
foreach ($request->vacationLimits() as $limit) {
$limit->update($data[$limit->id]);
}
return redirect()
->back()
->with("success", __("Vacation limits have been updated"));
return redirect()
->back()
->with("success", __("Vacation limits have been updated"));
}
}

View File

@@ -6,6 +6,7 @@ namespace Toby\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Collection;
use Toby\Models\VacationLimit;
class VacationLimitRequest extends FormRequest
{
@@ -19,12 +20,19 @@ class VacationLimitRequest extends FormRequest
];
}
public function vacationLimits(): Collection
{
return VacationLimit::query()->find($this->collect("items")->pluck("id"));
}
public function data(): Collection
{
return $this->collect("items")->map(fn(array $item): array => [
"id" => $item["id"],
"has_vacation" => $item["hasVacation"],
"days" => $item["days"],
return $this->collect("items")->mapWithKeys(fn(array $item): array => [
$item["id"] => [
"has_vacation" => $item["hasVacation"],
"days" => $item["days"],
]
]);
}
}

View File

@@ -6,10 +6,12 @@ namespace Toby\Models;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Carbon;
use Illuminate\Support\Collection;
use Toby\Enums\EmploymentForm;
/**
@@ -19,6 +21,7 @@ use Toby\Enums\EmploymentForm;
* @property string $avatar
* @property EmploymentForm $employment_form
* @property Carbon $employment_date
* @property Collection $vacationLimits
*/
class User extends Authenticatable
{
@@ -43,6 +46,11 @@ class User extends Authenticatable
"remember_token",
];
public function vacationLimits(): HasMany
{
return $this->hasMany(VacationLimit::class);
}
public function scopeSearch(Builder $query, ?string $text): Builder
{
if ($text === null) {
@@ -53,4 +61,11 @@ class User extends Authenticatable
->where("name", "LIKE", "%{$text}%")
->orWhere("email", "LIKE", "%{$text}%");
}
public function saveAvatar(string $path): void
{
$this->avatar = $path;
$this->save();
}
}

View File

@@ -6,20 +6,22 @@ namespace Toby\Observers;
use Illuminate\Support\Facades\Storage;
use Toby\Helpers\UserAvatarGenerator;
use Toby\Helpers\YearPeriodRetriever;
use Toby\Models\User;
class UserObserver
{
public function __construct(
protected UserAvatarGenerator $generator,
protected YearPeriodRetriever $yearPeriodRetriever,
) {
}
public function created(User $user): void
{
$user->avatar = $this->generator->generateFor($user);
$user->saveAvatar($this->generator->generateFor($user));
$user->save();
$user->vacationLimits()->create(["year_period_id" => $this->yearPeriodRetriever->current()->id]);
}
public function updating(User $user): void

View File

@@ -0,0 +1,26 @@
<?php
declare(strict_types=1);
namespace Toby\Observers;
use Toby\Helpers\UserAvatarGenerator;
use Toby\Models\User;
use Toby\Models\YearPeriod;
class YearPeriodObserver
{
public function __construct(
protected UserAvatarGenerator $generator,
) {
}
public function created(YearPeriod $yearPeriod): void
{
$users = User::all();
foreach ($users as $user) {
$yearPeriod->vacationLimits()->updateOrCreate(["user_id" => $user->id]);
}
}
}

View File

@@ -8,7 +8,9 @@ use Illuminate\Support\Carbon;
use Illuminate\Support\ServiceProvider;
use Toby\Models\User;
use Toby\Models\VacationLimit;
use Toby\Models\YearPeriod;
use Toby\Observers\UserObserver;
use Toby\Observers\YearPeriodObserver;
use Toby\Scopes\SelectedYearPeriodScope;
class AppServiceProvider extends ServiceProvider
@@ -16,6 +18,7 @@ class AppServiceProvider extends ServiceProvider
public function boot(): void
{
User::observe(UserObserver::class);
YearPeriod::observe(YearPeriodObserver::class);
Carbon::macro("toDisplayString", fn() => $this->translatedFormat("j F Y"));