Files
bogazici-api/app/Repositories/Eloquent/CourseRepository.php
2026-03-27 10:41:54 +03:00

60 lines
1.7 KiB
PHP

<?php
namespace App\Repositories\Eloquent;
use App\Models\Course;
use App\Repositories\Contracts\CourseRepositoryInterface;
use Illuminate\Pagination\LengthAwarePaginator;
/**
* @extends BaseRepository<Course>
*/
class CourseRepository extends BaseRepository implements CourseRepositoryInterface
{
public function __construct(Course $model)
{
parent::__construct($model);
}
public function findBySlug(string $slug): ?Course
{
return $this->model->newQuery()
->with(['category', 'schedules', 'blocks'])
->where('slug', $slug)
->first();
}
/**
* @param array<string, mixed> $filters
* @return LengthAwarePaginator<int, Course>
*/
public function paginate(array $filters = [], int $perPage = 15): LengthAwarePaginator
{
$query = $this->model->newQuery()->with('category');
if (! empty($filters['category'])) {
$query->whereHas('category', fn ($q) => $q->where('slug', $filters['category']));
}
if (! empty($filters['search'])) {
$query->where(function ($q) use ($filters) {
$q->where('title', 'like', '%'.$filters['search'].'%')
->orWhere('desc', 'like', '%'.$filters['search'].'%');
});
}
$sortField = $filters['sort'] ?? '-created_at';
$direction = str_starts_with($sortField, '-') ? 'desc' : 'asc';
$sortField = ltrim($sortField, '-');
$allowedSorts = ['title', 'created_at', 'students', 'rating'];
if (in_array($sortField, $allowedSorts)) {
$query->orderBy($sortField, $direction);
} else {
$query->latest();
}
return $query->paginate($perPage);
}
}