first commit
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
abstract class Controller
|
||||
{
|
||||
//
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Http\Requests\GetGigsRequest;
|
||||
use App\Http\Resources\GigResource;
|
||||
use App\Models\ORM\Gig;
|
||||
use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
|
||||
|
||||
class GigsController extends Controller
|
||||
{
|
||||
public function index(GetGigsRequest $request): AnonymousResourceCollection
|
||||
{
|
||||
// Используем eager loading (with), чтобы избежать проблемы N+1 запросов к категориям
|
||||
$query = Gig::with('categories');
|
||||
|
||||
// Фильтрация по нескольким категориям (Many-to-Many)
|
||||
if ($request->filled('categories')) {
|
||||
$query->whereHas('categories', function ($q) use ($request) {
|
||||
$q->whereIn('categories.id', $request->input('categories'));
|
||||
});
|
||||
}
|
||||
|
||||
// Фильтрация по промежутку дат
|
||||
if ($request->filled('date_from')) {
|
||||
$query->whereDate('event_date', '>=', $request->input('date_from'));
|
||||
}
|
||||
|
||||
if ($request->filled('date_to')) {
|
||||
$query->whereDate('event_date', '<=', $request->input('date_to'));
|
||||
}
|
||||
|
||||
// Сортируем события: сначала ближайшие
|
||||
$query->orderBy('event_date', 'asc');
|
||||
|
||||
// Пагинация по 10 элементов на страницу
|
||||
$gigs = $query->paginate(10);
|
||||
|
||||
return GigResource::collection($gigs);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Http\Requests;
|
||||
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class GetGigsRequest extends FormRequest
|
||||
{
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true; // Разрешаем доступ всем
|
||||
}
|
||||
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'categories' => ['nullable', 'array'],
|
||||
'categories.*' => ['integer', 'exists:categories,id'], // Проверяем, что ID категорий существуют
|
||||
'date_from' => ['nullable', 'date', 'date_format:Y-m-d'],
|
||||
'date_to' => ['nullable', 'date', 'date_format:Y-m-d', 'after_or_equal:date_from'],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Http\Resources;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
class GigResource extends JsonResource
|
||||
{
|
||||
public function toArray(Request $request): array
|
||||
{
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'title' => $this->title,
|
||||
'description' => $this->description,
|
||||
'event_date' => $this->event_date ? Carbon::parse($this->event_date)->toIso8601String() : null,
|
||||
'archived' => $this->archived,
|
||||
'categories' => $this->categories->map(fn($category) => [
|
||||
'id' => $category->id,
|
||||
'name' => $category->name,
|
||||
'slug' => $category->slug,
|
||||
]),
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user