feat(doctor): add filtering logic for clinic doctor list with pagination and sorting
This commit is contained in:
@@ -3,8 +3,10 @@
|
||||
namespace App\Doctor\Repository;
|
||||
|
||||
use App\Auth\Entity\User;
|
||||
use App\Clinic\Entity\Clinic;
|
||||
use App\Doctor\Entity\Doctor;
|
||||
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
|
||||
use Doctrine\ORM\Query\Expr\Join;
|
||||
use Doctrine\ORM\Tools\Pagination\Paginator;
|
||||
use Doctrine\Persistence\ManagerRegistry;
|
||||
|
||||
@@ -88,6 +90,55 @@ class DoctorRepository extends ServiceEntityRepository
|
||||
];
|
||||
}
|
||||
|
||||
public function findByClinicWithFilters(int $clinicId, array $filters): array
|
||||
{
|
||||
$page = max(1, (int) ($filters['page'] ?? 1));
|
||||
$limit = min(50, max(1, (int) ($filters['limit'] ?? 10)));
|
||||
$sort = strtoupper($filters['sort'] ?? 'DESC') === 'ASC' ? 'ASC' : 'DESC';
|
||||
|
||||
// Doctor has no inverse 'clinics' relation; the ManyToMany is owned by
|
||||
// Clinic.doctors. Join Clinic and match its doctors collection to d.
|
||||
$qb = $this->createQueryBuilder('d')
|
||||
->innerJoin(Clinic::class, 'c', Join::WITH, 'd MEMBER OF c.doctors')
|
||||
->leftJoin('d.specialties', 's')
|
||||
->where('c.id = :clinicId')
|
||||
->setParameter('clinicId', $clinicId)
|
||||
->distinct();
|
||||
|
||||
if (!empty($filters['specialty'])) {
|
||||
$qb->andWhere('s.id = :specialty')->setParameter('specialty', (int) $filters['specialty']);
|
||||
}
|
||||
if (!empty($filters['gender'])) {
|
||||
$qb->andWhere('d.gender = :gender')->setParameter('gender', $filters['gender']);
|
||||
}
|
||||
if (!empty($filters['degree'])) {
|
||||
$qb->andWhere('d.degree = :degree')->setParameter('degree', $filters['degree']);
|
||||
}
|
||||
if (!empty($filters['name'])) {
|
||||
$qb->andWhere('d.name LIKE :name')->setParameter('name', '%' . $filters['name'] . '%');
|
||||
}
|
||||
if (isset($filters['active'])) {
|
||||
$qb->andWhere('d.activeDoctorAppointment = :active')
|
||||
->setParameter('active', (bool) $filters['active']);
|
||||
}
|
||||
|
||||
$qb->orderBy('d.doctorRate', $sort);
|
||||
|
||||
$total = (new Paginator($qb))->count();
|
||||
$results = $qb->setFirstResult(($page - 1) * $limit)
|
||||
->setMaxResults($limit)
|
||||
->getQuery()
|
||||
->getResult();
|
||||
|
||||
return [
|
||||
'items' => $results,
|
||||
'total' => $total,
|
||||
'page' => $page,
|
||||
'limit' => $limit,
|
||||
'totalPages' => (int) ceil($total / $limit),
|
||||
];
|
||||
}
|
||||
|
||||
public function save(Doctor $doctor, bool $flush = true): void
|
||||
{
|
||||
$this->getEntityManager()->persist($doctor);
|
||||
|
||||
Reference in New Issue
Block a user