feat(clinic): enforce maximum gallery image limit of 5 and update validation logic
This commit is contained in:
@@ -30,6 +30,8 @@ use Symfony\Component\Uid\Uuid;
|
||||
#[OA\Tag(name: 'Clinics')]
|
||||
class ClinicController extends BaseController
|
||||
{
|
||||
private const MAX_GALLERY_IMAGES = 5;
|
||||
|
||||
public function __construct(
|
||||
private readonly ClinicRepository $clinicRepo,
|
||||
private readonly DoctorRepository $doctorRepo,
|
||||
@@ -91,6 +93,9 @@ class ClinicController extends BaseController
|
||||
public function create(Request $request, #[CurrentUser] User $user): JsonResponse
|
||||
{
|
||||
$data = json_decode($request->getContent(), true) ?? [];
|
||||
if (($err = $this->validateGallerySize($data)) !== null) {
|
||||
return $err;
|
||||
}
|
||||
$clinic = new Clinic($user);
|
||||
$this->hydrateClinic($clinic, $data);
|
||||
$this->clinicRepo->save($clinic);
|
||||
@@ -209,6 +214,9 @@ class ClinicController extends BaseController
|
||||
}
|
||||
|
||||
$data = json_decode($request->getContent(), true) ?? [];
|
||||
if (($err = $this->validateGallerySize($data)) !== null) {
|
||||
return $err;
|
||||
}
|
||||
$this->hydrateClinic($clinic, $data);
|
||||
$this->clinicRepo->save($clinic);
|
||||
|
||||
@@ -457,6 +465,22 @@ class ClinicController extends BaseController
|
||||
|
||||
// ── Helpers ───────────────────────────────────────────────────────────────
|
||||
|
||||
private function validateGallerySize(array $data): ?JsonResponse
|
||||
{
|
||||
if (array_key_exists('image_clinic', $data)
|
||||
&& is_array($data['image_clinic'])
|
||||
&& count($data['image_clinic']) > self::MAX_GALLERY_IMAGES
|
||||
) {
|
||||
return $this->error(
|
||||
ErrorCodes::ERR_VALIDATION_001,
|
||||
'گالری تصاویر حداکثر ' . self::MAX_GALLERY_IMAGES . ' عکس میتواند داشته باشد',
|
||||
422,
|
||||
'image_clinic',
|
||||
);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private function hydrateClinic(Clinic $clinic, array $data): void
|
||||
{
|
||||
if (array_key_exists('name', $data)) $clinic->setName($data['name']);
|
||||
@@ -476,9 +500,9 @@ class ClinicController extends BaseController
|
||||
$clinic->setCityId((int) $data['city'][0]);
|
||||
}
|
||||
|
||||
// Images stored as JSON (from upload response)
|
||||
// Images stored as JSON (from upload response); gallery is capped at 5.
|
||||
if (array_key_exists('image_clinic', $data) && is_array($data['image_clinic'])) {
|
||||
$clinic->setImagesClinic($data['image_clinic']);
|
||||
$clinic->setImagesClinic(array_slice(array_values($data['image_clinic']), 0, self::MAX_GALLERY_IMAGES));
|
||||
}
|
||||
if (array_key_exists('clinic_logo', $data)) {
|
||||
$logo = $data['clinic_logo'];
|
||||
|
||||
Reference in New Issue
Block a user