feat: add insurance and location management

- Introduced InsuranceType enum for insurance categorization.
- Created InsuranceRepository for managing insurance entities.
- Developed LocationController for handling provinces and cities, including CRUD operations.
- Implemented City and Province entities with necessary fields and relationships.
- Added CityRepository and ProvinceRepository for database interactions.
- Established Specialty management with SpecialtyController, including CRUD operations.
- Created Specialty and Tag entities with appropriate fields and relationships.
- Implemented TagController for managing tags, including CRUD operations.
- Added TagRepository for database interactions with tags.
This commit is contained in:
hamed
2026-06-10 14:22:26 +03:30
parent 4b8504df91
commit 5066fcbd91
36 changed files with 2937 additions and 1241 deletions
+27 -149
View File
@@ -2,164 +2,42 @@
namespace App\Category\Controller;
use App\Category\Entity\Category;
use App\Category\Repository\CategoryRepository;
use App\Category\Service\CategoryService;
use App\Shared\Constant\ErrorCodes;
use App\Shared\Controller\BaseController;
use App\Shared\Service\FileValidatorService;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Security\Http\Attribute\IsGranted;
use Symfony\Component\Uid\Uuid;
/**
* Legacy stub — all endpoints migrated to domain-specific controllers:
* Provinces/Cities → App\Location\Controller\LocationController
* Specialties → App\Specialty\Controller\SpecialtyController
* DoctorServices → App\DoctorService\Controller\DoctorServiceController
* Insurances → App\Insurance\Controller\InsuranceController
* Tags → App\Tag\Controller\TagController
*/
class CategoryController extends BaseController
{
public function __construct(
private readonly CategoryRepository $repository,
private readonly CategoryService $service,
private readonly FileValidatorService $fileValidator,
private readonly string $projectDir,
) {}
#[Route('/api/v1/categorys/tag', methods: ['GET'])]
public function listTags(): JsonResponse
#[Route('/api/v1/categorys/{bundle}', methods: ['GET'])]
public function legacyList(string $bundle): JsonResponse
{
return $this->success(['data' => $this->service->listByBundle('tag')]);
}
$map = [
'state' => '/api/v1/provinces',
'city' => '/api/v1/cities',
'specially_doctor' => '/api/v1/specialties',
'doctor_services' => '/api/v1/doctor-services',
'insurance_type' => '/api/v1/insurances?type=basic',
'supplementary_insurance' => '/api/v1/insurances?type=supplementary',
'tag' => '/api/v1/tags',
];
#[Route('/api/v1/categorys/supplementary_insurance', methods: ['GET'])]
public function listSupplementaryInsurance(): JsonResponse
{
return $this->success(['data' => $this->service->listByBundle('supplementary_insurance')]);
}
#[Route('/api/v1/categorys/insurance_type', methods: ['GET'])]
public function listInsuranceType(): JsonResponse
{
return $this->success(['data' => $this->service->listByBundle('insurance_type')]);
}
#[Route('/api/v1/categorys/state', methods: ['GET'])]
public function listStates(): JsonResponse
{
return $this->success(['data' => $this->service->listByBundle('state')]);
}
#[Route('/api/v1/categorys/city', methods: ['GET'])]
public function listCities(Request $request): JsonResponse
{
$stateId = $request->query->get('state_id');
$parentId = $stateId !== null ? (int) $stateId : null;
return $this->success(['data' => $this->service->listByBundle('city', $parentId)]);
}
#[Route('/api/v1/categorys/specially_doctor', methods: ['GET'])]
public function listSpecialties(): JsonResponse
{
return $this->success(['data' => $this->service->listByBundle('specially_doctor')]);
}
#[Route('/api/v1/categorys/doctor_services', methods: ['GET'])]
public function listDoctorServices(): JsonResponse
{
return $this->success(['data' => $this->service->listByBundle('doctor_services')]);
}
#[Route('/api/v1/category', methods: ['POST'])]
#[IsGranted('ROLE_ADMIN')]
public function create(Request $request): JsonResponse
{
$data = json_decode($request->getContent(), true) ?? [];
$bundle = trim($data['bundle'] ?? '');
$label = trim($data['label'] ?? '');
if (!in_array($bundle, Category::BUNDLES, true)) {
return $this->error(ErrorCodes::ERR_VALIDATION_001, 'bundle نامعتبر است', 422, 'bundle');
$newUrl = $map[$bundle] ?? null;
if ($newUrl === null) {
return $this->error('ERR_GONE', 'این endpoint حذف شده است', 410);
}
if ($label === '') {
return $this->error(ErrorCodes::ERR_VALIDATION_001, 'label الزامی است', 422, 'label');
}
$category = $this->service->create($bundle, $label, $data);
return $this->success(['data' => $category->toArray()], 201);
}
#[Route('/api/v1/category/{id}', methods: ['PATCH'])]
#[IsGranted('ROLE_ADMIN')]
public function update(int $id, Request $request): JsonResponse
{
$category = $this->repository->find($id);
if ($category === null) {
return $this->error(ErrorCodes::ERR_VALIDATION_002, 'دسته‌بندی یافت نشد', 404);
}
$data = json_decode($request->getContent(), true) ?? [];
if (isset($data['bundle']) && !in_array($data['bundle'], Category::BUNDLES, true)) {
return $this->error(ErrorCodes::ERR_VALIDATION_001, 'bundle نامعتبر است', 422, 'bundle');
}
$category = $this->service->update($category, $data);
return $this->success(['data' => $category->toArray()]);
}
#[Route('/api/v1/category/{id}', methods: ['DELETE'])]
#[IsGranted('ROLE_ADMIN')]
public function delete(int $id): JsonResponse
{
$category = $this->repository->find($id);
if ($category === null) {
return $this->error(ErrorCodes::ERR_VALIDATION_002, 'دسته‌بندی یافت نشد', 404);
}
$this->service->delete($category);
return $this->success(['message' => 'دسته‌بندی با موفقیت حذف شد']);
}
#[Route('/api/v1/admin/category/upload-logo', methods: ['POST'])]
#[IsGranted('ROLE_ADMIN')]
public function uploadLogo(Request $request): JsonResponse
{
$content = $request->getContent();
$disposition = $request->headers->get('Content-Disposition', '');
preg_match('/filename=["\']?([^"\';\s]+)["\']?/i', $disposition, $m);
$filename = $m[1] ?? 'logo.jpg';
$tmpPath = sys_get_temp_dir() . '/' . uniqid('upload_', true);
file_put_contents($tmpPath, $content);
try {
$safeFilename = $this->fileValidator->sanitizeFilename($filename);
$mime = $this->fileValidator->detectMimeType($tmpPath);
$year = date('Y'); $month = date('m');
$dir = $this->projectDir . '/public/uploads/categories/logo/' . $year . '-' . $month;
if (!is_dir($dir)) mkdir($dir, 0755, true);
$storedName = uniqid('', true) . '_' . $safeFilename;
rename($tmpPath, $dir . '/' . $storedName);
$url = '/uploads/categories/logo/' . $year . '-' . $month . '/' . $storedName;
return $this->success([
'url' => $url,
'uuid' => Uuid::v4()->toRfc4122(),
'filename' => $safeFilename,
'filemime' => $mime,
'filesize' => strlen($content),
]);
} catch (\Throwable $e) {
if (file_exists($tmpPath)) unlink($tmpPath);
return $this->error(ErrorCodes::ERR_VALIDATION_001, $e->getMessage(), 422);
}
return $this->error(
'ERR_MOVED',
sprintf('این endpoint منتقل شده. لطفاً از %s استفاده کنید.', $newUrl),
301
);
}
}