Skip to content

Add filters for relations #43

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/Http/Controllers/BookingController.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function index(
'groups',
]),
'bookingOption' => $bookingOption,
'bookings' => $bookingsQuery->paginate(),
'bookings' => $bookingsQuery->paginate(24),
]);
}

Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/EventController.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function index(EventFilterRequest $request): View
'groups',
'subEvents',
])
->paginate(),
->paginate(12),
]));
}

Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/EventSeriesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function index(EventSeriesFilterRequest $request): View
'events',
'subEventSeries',
])
->paginate(),
->paginate(18),
]);
}

Expand Down
6 changes: 5 additions & 1 deletion app/Http/Controllers/LocationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Http\Requests\Filters\LocationFilterRequest;
use App\Http\Requests\LocationRequest;
use App\Models\Location;
use App\Models\Organization;
use App\Models\User;
use Illuminate\Http\RedirectResponse;
use Illuminate\Support\Facades\Session;
Expand All @@ -24,7 +25,10 @@ public function index(LocationFilterRequest $request): View
'events',
'organizations',
])
->paginate(),
->paginate(18),
'organizations' => Organization::query()
->orderBy('name')
->get(),
]);
}

Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function index(UserFilterRequest $request): View
'responsibleForEventSeries',
'responsibleForOrganizations',
])
->paginate(),
->paginate(12),
]));
}

Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/UserRoleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function index(UserRoleFilterRequest $request): View
->withCount([
'users',
])
->paginate(),
->paginate(18),
]);
}

Expand Down
17 changes: 6 additions & 11 deletions app/Http/Requests/Filters/BookingFilterRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
use App\Http\Requests\Traits\AuthorizationViaController;
use App\Http\Requests\Traits\FiltersList;
use App\Models\Booking;
use App\Models\Group;
use App\Options\FilterValue;
use App\Options\PaymentStatus;
use App\Policies\BookingPolicy;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

/**
* Filter for {@see Booking}s
Expand All @@ -25,21 +26,15 @@ class BookingFilterRequest extends FormRequest
*/
public function rules(): array
{
$groupExistsRule = Rule::exists('groups', 'id');
$groupQuery = Group::query();
if (isset($this->event)) {
$groupExistsRule->where('event_id', $this->event->id);
$groupQuery->where('event_id', $this->event->id);
}

return [
'filter.search' => $this->ruleForText(),
'filter.payment_status' => [
'nullable',
PaymentStatus::rule(),
],
'filter.group_id' => [
'nullable',
$groupExistsRule,
],
'filter.payment_status' => $this->ruleForAllowedOrExistsInEnum(PaymentStatus::class, [FilterValue::All->value]),
'filter.group_id' => $this->ruleForAllowedOrExistsInDatabase($groupQuery, [FilterValue::All->value]),
'sort' => [
'nullable',
Booking::sortOptions()->getRule(),
Expand Down
11 changes: 3 additions & 8 deletions app/Http/Requests/Filters/DocumentFilterRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use App\Models\Document;
use App\Options\ApprovalStatus;
use App\Options\FileType;
use App\Options\FilterValue;
use App\Policies\DocumentPolicy;
use Illuminate\Foundation\Http\FormRequest;

Expand All @@ -24,14 +25,8 @@ public function rules(): array
{
return [
'filter.search' => $this->ruleForText(),
'filter.file_type' => [
'nullable',
FileType::rule(),
],
'filter.approval_status' => [
'nullable',
ApprovalStatus::rule(),
],
'filter.file_type' => $this->ruleForAllowedOrExistsInEnum(FileType::class, [FilterValue::All->value]),
'filter.approval_status' => $this->ruleForAllowedOrExistsInEnum(ApprovalStatus::class, [FilterValue::All->value]),
'sort' => [
'nullable',
Document::sortOptions()->getRule(),
Expand Down
21 changes: 11 additions & 10 deletions app/Http/Requests/Filters/EventFilterRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@
use App\Http\Controllers\EventController;
use App\Http\Requests\Traits\AuthorizationViaController;
use App\Http\Requests\Traits\FiltersList;
use App\Models\Document;
use App\Models\Event;
use App\Models\EventSeries;
use App\Models\Location;
use App\Models\Organization;
use App\Options\EventType;
use App\Options\FilterValue;
use App\Options\Visibility;
use App\Policies\EventPolicy;
use Illuminate\Foundation\Http\FormRequest;
Expand All @@ -27,18 +32,14 @@ public function rules(): array
{
return [
'filter.search' => $this->ruleForText(),
'filter.visibility' => [
'nullable',
Visibility::rule(),
],
'filter.visibility' => $this->ruleForAllowedOrExistsInEnum(Visibility::class, [FilterValue::All->value]),
'filter.date_from' => $this->ruleForDate(),
'filter.date_until' => $this->ruleForDate('filter.date_from'),
'filter.location_id' => $this->ruleForForeignId('locations'),
'filter.organization_id' => $this->ruleForForeignId('organizations'),
'filter.event_type' => [
'nullable',
EventType::rule(),
],
'filter.event_series_id' => $this->ruleForAllowedOrExistsInDatabase(EventSeries::query(), FilterValue::values()),
'filter.organization_id' => $this->ruleForAllowedOrExistsInDatabase(Organization::query(), FilterValue::values()),
'filter.location_id' => $this->ruleForAllowedOrExistsInDatabase(Location::query(), [FilterValue::All->value]),
'filter.document_id' => $this->ruleForAllowedOrExistsInDatabase(Document::query(), FilterValue::values()),
'filter.event_type' => $this->ruleForAllowedOrExistsInEnum(EventType::class, [FilterValue::All->value]),
'sort' => [
'nullable',
Event::sortOptions()->getRule(),
Expand Down
12 changes: 8 additions & 4 deletions app/Http/Requests/Filters/EventSeriesFilterRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@
use App\Http\Controllers\EventSeriesController;
use App\Http\Requests\Traits\AuthorizationViaController;
use App\Http\Requests\Traits\FiltersList;
use App\Models\Document;
use App\Models\Event;
use App\Models\EventSeries;
use App\Options\EventSeriesType;
use App\Options\FilterValue;
use App\Options\Visibility;
use App\Policies\EventSeriesPolicy;
use Illuminate\Foundation\Http\FormRequest;

Expand All @@ -26,10 +30,10 @@ public function rules(): array
{
return [
'filter.name' => $this->ruleForText(),
'filter.event_series_type' => [
'nullable',
EventSeriesType::rule(),
],
'filter.visibility' => $this->ruleForAllowedOrExistsInEnum(Visibility::class, [FilterValue::All->value]),
'filter.event_id' => $this->ruleForAllowedOrExistsInDatabase(Event::query(), FilterValue::values()),
'filter.document_id' => $this->ruleForAllowedOrExistsInDatabase(Document::query(), FilterValue::values()),
'filter.event_series_type' => $this->ruleForAllowedOrExistsInEnum(EventSeriesType::class, [FilterValue::All->value]),
'sort' => [
'nullable',
EventSeries::sortOptions()->getRule(),
Expand Down
9 changes: 9 additions & 0 deletions app/Http/Requests/Filters/LocationFilterRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@

use App\Http\Requests\Traits\AuthorizationViaController;
use App\Http\Requests\Traits\FiltersList;
use App\Models\Event;
use App\Models\Location;
use App\Models\Organization;
use App\Options\FilterValue;
use App\Policies\LocationPolicy;
use Illuminate\Foundation\Http\FormRequest;

Expand All @@ -25,6 +28,12 @@ public function rules(): array
return [
'filter.name' => $this->ruleForText(),
'filter.address' => $this->ruleForText(),
'filter.event_id' => $this->ruleForAllowedOrExistsInDatabase(Event::query(), FilterValue::values()),
'filter.organization_id' => $this->ruleForAllowedOrExistsInDatabase(Organization::query(), FilterValue::values()),
'sort' => [
'nullable',
Location::sortOptions()->getRule(),
],
];
}
}
12 changes: 11 additions & 1 deletion app/Http/Requests/Filters/OrganizationFilterRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
use App\Http\Controllers\OrganizationController;
use App\Http\Requests\Traits\AuthorizationViaController;
use App\Http\Requests\Traits\FiltersList;
use App\Models\Document;
use App\Models\Event;
use App\Models\Location;
use App\Models\Organization;
use App\Options\FilterValue;
use App\Policies\OrganizationPolicy;
use Illuminate\Foundation\Http\FormRequest;

Expand All @@ -25,7 +29,13 @@ public function rules(): array
{
return [
'filter.name' => $this->ruleForText(),
'filter.location_id' => $this->ruleForForeignId('locations'),
'filter.event_id' => $this->ruleForAllowedOrExistsInDatabase(Event::query(), FilterValue::values()),
'filter.location_id' => $this->ruleForAllowedOrExistsInDatabase(Location::query(), [FilterValue::All->value]),
'filter.document_id' => $this->ruleForAllowedOrExistsInDatabase(Document::query(), FilterValue::values()),
'sort' => [
'nullable',
Organization::sortOptions()->getRule(),
],
];
}
}
13 changes: 4 additions & 9 deletions app/Http/Requests/Filters/UserFilterRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
use App\Http\Requests\Traits\AuthorizationViaController;
use App\Http\Requests\Traits\FiltersList;
use App\Models\User;
use App\Models\UserRole;
use App\Options\ActiveStatus;
use App\Options\FilterValue;
use App\Policies\UserPolicy;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

/**
* Filter for {@see User}s
Expand All @@ -28,14 +29,8 @@ public function rules(): array
return [
'filter.name' => $this->ruleForText(),
'filter.email' => $this->ruleForText(),
'filter.user_role_id' => [
'nullable',
Rule::exists('user_roles', 'id'),
],
'filter.status' => [
'nullable',
ActiveStatus::rule(),
],
'filter.user_role_id' => $this->ruleForAllowedOrExistsInDatabase(UserRole::query(), FilterValue::values()),
'filter.status' => $this->ruleForAllowedOrExistsInEnum(ActiveStatus::class, [FilterValue::All->value]),
'sort' => [
'nullable',
User::sortOptions()->getRule(),
Expand Down
3 changes: 3 additions & 0 deletions app/Http/Requests/Filters/UserRoleFilterRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
use App\Http\Controllers\UserRoleController;
use App\Http\Requests\Traits\AuthorizationViaController;
use App\Http\Requests\Traits\FiltersList;
use App\Models\User;
use App\Models\UserRole;
use App\Options\FilterValue;
use App\Policies\UserRolePolicy;
use Illuminate\Foundation\Http\FormRequest;

Expand All @@ -25,6 +27,7 @@ public function rules(): array
{
return [
'filter.name' => $this->ruleForText(),
'filter.user_id' => $this->ruleForAllowedOrExistsInDatabase(User::query(), FilterValue::values()),
'sort' => [
'nullable',
UserRole::sortOptions()->getRule(),
Expand Down
37 changes: 37 additions & 0 deletions app/Http/Requests/Traits/FiltersList.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Http\Requests\Traits;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Validation\Rule;

trait FiltersList
Expand All @@ -27,6 +28,11 @@ public function mapFilterAttribute(string $attribute): string
return __('validation.attributes.date_until');
}

return $this->getTranslatedAttribute($attribute);
}

private function getTranslatedAttribute(string $attribute): string
{
$validationKey = str_replace(['filter.', '_from', '_until'], '', $attribute);

return __('validation.attributes.' . $validationKey);
Expand All @@ -46,6 +52,37 @@ public function ruleForDate(?string $afterOrEqual = null): array
return $rules;
}

public function ruleForAllowedOrExistsInDatabase(Builder $query, array $allowedValues): array
{
return [
'nullable',
function ($attribute, $value, $fail) use ($allowedValues, $query) {
if (!in_array($value, $allowedValues, true) && !$query->where('id', (int) $value)->exists()) {
$fail(trans('validation.exists', [
'attribute' => $this->getTranslatedAttribute($attribute),
]));
}
},
];
}

/**
* @param class-string $enumClass
*/
public function ruleForAllowedOrExistsInEnum(string $enumClass, array $allowedValues): array
{
return [
'nullable',
function ($attribute, $value, $fail) use ($allowedValues, $enumClass) {
if (!in_array($value, $allowedValues, true) && !$enumClass::exists($value)) {
$fail(trans('validation.exists', [
'attribute' => $this->getTranslatedAttribute($attribute),
]));
}
},
];
}

public function ruleForForeignId(string $table): array
{
return [
Expand Down
9 changes: 5 additions & 4 deletions app/Models/Booking.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use App\Models\QueryBuilder\SortOptions;
use App\Models\Traits\HasAddress;
use App\Options\DeletedFilter;
use App\Options\FilterValue;
use App\Options\FormElementType;
use App\Options\PaymentStatus;
use Carbon\Carbon;
Expand Down Expand Up @@ -86,8 +87,6 @@ class Booking extends Model
'deleted_at' => 'datetime',
];

protected $perPage = 12;

public function age(): Attribute
{
return Attribute::get(fn () => Carbon::now()->floatDiffInYears($this->date_of_birth));
Expand Down Expand Up @@ -255,9 +254,11 @@ public static function allowedFilters(): array
/** @see self::scopeSearchAll() */
AllowedFilter::scope('search', 'searchAll'),
/** @see self::scopeGroup() */
AllowedFilter::scope('group_id', 'group'),
AllowedFilter::scope('group_id', 'group')
->ignore(FilterValue::All->value),
/** @see self::scopePaymentStatus() */
AllowedFilter::scope('payment_status', 'paymentStatus'),
AllowedFilter::scope('payment_status', 'paymentStatus')
->ignore(FilterValue::All->value),
AllowedFilter::trashed()
->default(DeletedFilter::HideDeleted->value),
];
Expand Down
Loading