<?php

namespace App\Patient\Repository;

use App\Patient\Entity\PatientAttachment;
use App\Patient\Entity\PatientRecord;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;

class PatientAttachmentRepository extends ServiceEntityRepository
{
    public function __construct(ManagerRegistry $registry)
    {
        parent::__construct($registry, PatientAttachment::class);
    }

    public function findByUuid(string $uuid): ?PatientAttachment
    {
        return $this->findOneBy(['uuid' => $uuid]);
    }

    /** @return PatientAttachment[] */
    public function findByRecord(PatientRecord $record): array
    {
        return $this->createQueryBuilder('a')
            ->where('a.record = :record')
            ->setParameter('record', $record)
            ->orderBy('a.id', 'DESC')
            ->getQuery()
            ->getResult();
    }

    public function save(PatientAttachment $a): void
    {
        $this->getEntityManager()->persist($a);
        $this->getEntityManager()->flush();
    }

    public function remove(PatientAttachment $a): void
    {
        $this->getEntityManager()->remove($a);
        $this->getEntityManager()->flush();
    }
}
