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:
@@ -0,0 +1,226 @@
|
||||
<?php
|
||||
|
||||
namespace App\Location\Controller;
|
||||
|
||||
use App\Location\Entity\City;
|
||||
use App\Location\Entity\Province;
|
||||
use App\Location\Repository\CityRepository;
|
||||
use App\Location\Repository\ProvinceRepository;
|
||||
use App\Shared\Constant\ErrorCodes;
|
||||
use App\Shared\Controller\BaseController;
|
||||
use Symfony\Component\HttpFoundation\JsonResponse;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\Routing\Attribute\Route;
|
||||
use Symfony\Component\Security\Http\Attribute\IsGranted;
|
||||
|
||||
class LocationController extends BaseController
|
||||
{
|
||||
public function __construct(
|
||||
private readonly ProvinceRepository $provinceRepo,
|
||||
private readonly CityRepository $cityRepo,
|
||||
) {}
|
||||
|
||||
// ── Public endpoints ──────────────────────────────────────────────────────
|
||||
|
||||
#[Route('/api/v1/provinces', methods: ['GET'])]
|
||||
public function provinces(): JsonResponse
|
||||
{
|
||||
$items = array_map(fn(Province $p) => $p->toArray(), $this->provinceRepo->findActive());
|
||||
return $this->success(['data' => $items]);
|
||||
}
|
||||
|
||||
#[Route('/api/v1/cities', methods: ['GET'])]
|
||||
public function cities(Request $request): JsonResponse
|
||||
{
|
||||
$provinceId = $request->query->get('province_id');
|
||||
$items = array_map(
|
||||
fn(City $c) => $c->toArray(),
|
||||
$this->cityRepo->findActive($provinceId !== null ? (int) $provinceId : null)
|
||||
);
|
||||
return $this->success(['data' => $items]);
|
||||
}
|
||||
|
||||
// ── Admin CRUD — Province ─────────────────────────────────────────────────
|
||||
|
||||
#[Route('/api/v1/admin/province', methods: ['POST'])]
|
||||
#[IsGranted('ROLE_ADMIN')]
|
||||
public function createProvince(Request $request): JsonResponse
|
||||
{
|
||||
$data = json_decode($request->getContent(), true) ?? [];
|
||||
$name = trim($data['name'] ?? '');
|
||||
if ($name === '') {
|
||||
return $this->error(ErrorCodes::ERR_VALIDATION_001, 'name الزامی است', 422, 'name');
|
||||
}
|
||||
|
||||
$province = new Province($name);
|
||||
if (isset($data['status'])) $province->setStatus((int) $data['status']);
|
||||
if (isset($data['weight'])) $province->setWeight((int) $data['weight']);
|
||||
|
||||
$this->provinceRepo->save($province);
|
||||
return $this->success(['data' => $province->toArray()], 201);
|
||||
}
|
||||
|
||||
#[Route('/api/v1/admin/province/{id}', methods: ['PATCH'])]
|
||||
#[IsGranted('ROLE_ADMIN')]
|
||||
public function updateProvince(int $id, Request $request): JsonResponse
|
||||
{
|
||||
$province = $this->provinceRepo->find($id);
|
||||
if ($province === null) {
|
||||
return $this->error(ErrorCodes::ERR_VALIDATION_002, 'استان یافت نشد', 404);
|
||||
}
|
||||
|
||||
$data = json_decode($request->getContent(), true) ?? [];
|
||||
if (isset($data['name'])) $province->setName($data['name']);
|
||||
if (isset($data['status'])) $province->setStatus((int) $data['status']);
|
||||
if (isset($data['weight'])) $province->setWeight((int) $data['weight']);
|
||||
|
||||
$this->provinceRepo->save($province);
|
||||
return $this->success(['data' => $province->toArray()]);
|
||||
}
|
||||
|
||||
#[Route('/api/v1/admin/province/{id}', methods: ['DELETE'])]
|
||||
#[IsGranted('ROLE_ADMIN')]
|
||||
public function deleteProvince(int $id): JsonResponse
|
||||
{
|
||||
$province = $this->provinceRepo->find($id);
|
||||
if ($province === null) {
|
||||
return $this->error(ErrorCodes::ERR_VALIDATION_002, 'استان یافت نشد', 404);
|
||||
}
|
||||
|
||||
$this->provinceRepo->remove($province);
|
||||
return $this->success(['message' => 'استان با موفقیت حذف شد']);
|
||||
}
|
||||
|
||||
// ── Admin CRUD — City ─────────────────────────────────────────────────────
|
||||
|
||||
#[Route('/api/v1/admin/city', methods: ['POST'])]
|
||||
#[IsGranted('ROLE_ADMIN')]
|
||||
public function createCity(Request $request): JsonResponse
|
||||
{
|
||||
$data = json_decode($request->getContent(), true) ?? [];
|
||||
$name = trim($data['name'] ?? '');
|
||||
if ($name === '') {
|
||||
return $this->error(ErrorCodes::ERR_VALIDATION_001, 'name الزامی است', 422, 'name');
|
||||
}
|
||||
|
||||
$province = null;
|
||||
if (!empty($data['province_id'])) {
|
||||
$province = $this->provinceRepo->find((int) $data['province_id']);
|
||||
}
|
||||
|
||||
$city = new City($name, $province);
|
||||
$this->applyCityData($city, $data);
|
||||
$this->cityRepo->save($city);
|
||||
|
||||
return $this->success(['data' => $city->toArray()], 201);
|
||||
}
|
||||
|
||||
#[Route('/api/v1/admin/city/{id}', methods: ['PATCH'])]
|
||||
#[IsGranted('ROLE_ADMIN')]
|
||||
public function updateCity(int $id, Request $request): JsonResponse
|
||||
{
|
||||
$city = $this->cityRepo->find($id);
|
||||
if ($city === null) {
|
||||
return $this->error(ErrorCodes::ERR_VALIDATION_002, 'شهر یافت نشد', 404);
|
||||
}
|
||||
|
||||
$data = json_decode($request->getContent(), true) ?? [];
|
||||
if (isset($data['name'])) $city->setName($data['name']);
|
||||
if (array_key_exists('province_id', $data)) {
|
||||
$province = $data['province_id'] ? $this->provinceRepo->find((int) $data['province_id']) : null;
|
||||
$city->setProvince($province);
|
||||
}
|
||||
$this->applyCityData($city, $data);
|
||||
$this->cityRepo->save($city);
|
||||
|
||||
return $this->success(['data' => $city->toArray()]);
|
||||
}
|
||||
|
||||
#[Route('/api/v1/admin/city/{id}', methods: ['DELETE'])]
|
||||
#[IsGranted('ROLE_ADMIN')]
|
||||
public function deleteCity(int $id): JsonResponse
|
||||
{
|
||||
$city = $this->cityRepo->find($id);
|
||||
if ($city === null) {
|
||||
return $this->error(ErrorCodes::ERR_VALIDATION_002, 'شهر یافت نشد', 404);
|
||||
}
|
||||
|
||||
$this->cityRepo->remove($city);
|
||||
return $this->success(['message' => 'شهر با موفقیت حذف شد']);
|
||||
}
|
||||
|
||||
// ── Admin paginated lists ─────────────────────────────────────────────────
|
||||
|
||||
#[Route('/api/v1/admin/provinces', methods: ['GET'])]
|
||||
#[IsGranted('ROLE_ADMIN')]
|
||||
public function adminProvinces(Request $request): JsonResponse
|
||||
{
|
||||
$page = max(1, (int) $request->query->get('page', 1));
|
||||
$limit = min(100, max(1, (int) $request->query->get('limit', 15)));
|
||||
$search = trim((string) $request->query->get('search', ''));
|
||||
|
||||
$qb = $this->provinceRepo->createQueryBuilder('p')->orderBy('p.weight', 'ASC')->addOrderBy('p.name', 'ASC');
|
||||
if ($search !== '') {
|
||||
$qb->andWhere('p.name LIKE :s')->setParameter('s', '%' . $search . '%');
|
||||
}
|
||||
|
||||
$total = (clone $qb)->select('COUNT(p.id)')->getQuery()->getSingleScalarResult();
|
||||
$rows = $qb->setFirstResult(($page - 1) * $limit)->setMaxResults($limit)->getQuery()->getArrayResult();
|
||||
|
||||
$items = array_map(fn(array $r) => [
|
||||
'id' => $r['id'], 'uuid' => $r['uuid'], 'name' => $r['name'],
|
||||
'status' => $r['status'], 'weight' => $r['weight'],
|
||||
], $rows);
|
||||
|
||||
return $this->paginated($items, (int) $total, $page, $limit);
|
||||
}
|
||||
|
||||
#[Route('/api/v1/admin/cities', methods: ['GET'])]
|
||||
#[IsGranted('ROLE_ADMIN')]
|
||||
public function adminCities(Request $request): JsonResponse
|
||||
{
|
||||
$page = max(1, (int) $request->query->get('page', 1));
|
||||
$limit = min(100, max(1, (int) $request->query->get('limit', 15)));
|
||||
$search = trim((string) $request->query->get('search', ''));
|
||||
$provinceId = $request->query->get('province_id');
|
||||
|
||||
$qb = $this->cityRepo->createQueryBuilder('c')
|
||||
->leftJoin('c.province', 'p')
|
||||
->addSelect('p')
|
||||
->orderBy('c.weight', 'ASC')
|
||||
->addOrderBy('c.name', 'ASC');
|
||||
|
||||
if ($search !== '') {
|
||||
$qb->andWhere('c.name LIKE :s')->setParameter('s', '%' . $search . '%');
|
||||
}
|
||||
if ($provinceId !== null && $provinceId !== '') {
|
||||
$qb->andWhere('c.province = :province')->setParameter('province', (int) $provinceId);
|
||||
}
|
||||
|
||||
$total = (clone $qb)->select('COUNT(c.id)')->getQuery()->getSingleScalarResult();
|
||||
$cities = $qb->setFirstResult(($page - 1) * $limit)->setMaxResults($limit)->getQuery()->getResult();
|
||||
|
||||
$items = array_map(fn(City $c) => $c->toArray(), $cities);
|
||||
|
||||
return $this->paginated($items, (int) $total, $page, $limit);
|
||||
}
|
||||
|
||||
// ── Helpers ───────────────────────────────────────────────────────────────
|
||||
|
||||
private function applyCityData(City $city, array $data): void
|
||||
{
|
||||
if (isset($data['status'])) $city->setStatus((int) $data['status']);
|
||||
if (isset($data['weight'])) $city->setWeight((int) $data['weight']);
|
||||
if (array_key_exists('representation_id', $data))
|
||||
$city->setRepresentationId($data['representation_id'] !== null ? (int) $data['representation_id'] : null);
|
||||
if (array_key_exists('contact_phone', $data)) $city->setContactPhone($data['contact_phone']);
|
||||
if (array_key_exists('email', $data)) $city->setEmail($data['email']);
|
||||
if (array_key_exists('description', $data)) $city->setDescription($data['description']);
|
||||
if (array_key_exists('slogan', $data)) $city->setSlogan($data['slogan']);
|
||||
if (array_key_exists('domain', $data)) $city->setDomain($data['domain']);
|
||||
if (array_key_exists('keywords', $data)) $city->setKeywords($data['keywords']);
|
||||
if (array_key_exists('footer_description', $data)) $city->setFooterDescription($data['footer_description']);
|
||||
if (array_key_exists('social_media', $data)) $city->setSocialMedia($data['social_media']);
|
||||
if (array_key_exists('logo_url', $data)) $city->setLogoUrl($data['logo_url']);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,128 @@
|
||||
<?php
|
||||
|
||||
namespace App\Location\Entity;
|
||||
|
||||
use App\Location\Repository\CityRepository;
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
use Symfony\Component\Uid\Uuid;
|
||||
|
||||
#[ORM\Entity(repositoryClass: CityRepository::class)]
|
||||
#[ORM\Table(name: 'cities')]
|
||||
#[ORM\Index(columns: ['province_id'], name: 'idx_cities_province')]
|
||||
#[ORM\Index(columns: ['status'], name: 'idx_cities_status')]
|
||||
#[ORM\Index(columns: ['representation_id'], name: 'idx_cities_representation')]
|
||||
class City
|
||||
{
|
||||
#[ORM\Id]
|
||||
#[ORM\GeneratedValue]
|
||||
#[ORM\Column(type: 'integer')]
|
||||
private ?int $id = null;
|
||||
|
||||
#[ORM\Column(type: 'string', length: 36, unique: true)]
|
||||
private string $uuid;
|
||||
|
||||
#[ORM\Column(type: 'string', length: 255)]
|
||||
private string $name;
|
||||
|
||||
#[ORM\Column(type: 'smallint')]
|
||||
private int $status = 1;
|
||||
|
||||
#[ORM\Column(type: 'integer')]
|
||||
private int $weight = 0;
|
||||
|
||||
#[ORM\ManyToOne(targetEntity: Province::class)]
|
||||
#[ORM\JoinColumn(name: 'province_id', referencedColumnName: 'id', nullable: true)]
|
||||
private ?Province $province = null;
|
||||
|
||||
#[ORM\Column(name: 'representation_id', type: 'integer', nullable: true)]
|
||||
private ?int $representationId = null;
|
||||
|
||||
#[ORM\Column(name: 'contact_phone', type: 'string', length: 255, nullable: true)]
|
||||
private ?string $contactPhone = null;
|
||||
|
||||
#[ORM\Column(type: 'string', length: 255, nullable: true)]
|
||||
private ?string $email = null;
|
||||
|
||||
#[ORM\Column(type: 'text', nullable: true)]
|
||||
private ?string $description = null;
|
||||
|
||||
#[ORM\Column(type: 'string', length: 255, nullable: true)]
|
||||
private ?string $slogan = null;
|
||||
|
||||
#[ORM\Column(type: 'string', length: 255, nullable: true)]
|
||||
private ?string $domain = null;
|
||||
|
||||
#[ORM\Column(type: 'string', length: 255, nullable: true)]
|
||||
private ?string $keywords = null;
|
||||
|
||||
#[ORM\Column(name: 'footer_description', type: 'text', nullable: true)]
|
||||
private ?string $footerDescription = null;
|
||||
|
||||
#[ORM\Column(name: 'social_media', type: 'json', nullable: true)]
|
||||
private ?array $socialMedia = null;
|
||||
|
||||
#[ORM\Column(name: 'logo_url', type: 'string', length: 500, nullable: true)]
|
||||
private ?string $logoUrl = null;
|
||||
|
||||
public function __construct(string $name, ?Province $province = null)
|
||||
{
|
||||
$this->uuid = Uuid::v4()->toRfc4122();
|
||||
$this->name = $name;
|
||||
$this->province = $province;
|
||||
}
|
||||
|
||||
public function getId(): ?int { return $this->id; }
|
||||
public function getUuid(): string { return $this->uuid; }
|
||||
public function getName(): string { return $this->name; }
|
||||
public function getStatus(): int { return $this->status; }
|
||||
public function getWeight(): int { return $this->weight; }
|
||||
public function getProvince(): ?Province { return $this->province; }
|
||||
public function getRepresentationId(): ?int { return $this->representationId; }
|
||||
public function getContactPhone(): ?string { return $this->contactPhone; }
|
||||
public function getEmail(): ?string { return $this->email; }
|
||||
public function getDescription(): ?string { return $this->description; }
|
||||
public function getSlogan(): ?string { return $this->slogan; }
|
||||
public function getDomain(): ?string { return $this->domain; }
|
||||
public function getKeywords(): ?string { return $this->keywords; }
|
||||
public function getFooterDescription(): ?string { return $this->footerDescription; }
|
||||
public function getSocialMedia(): ?array { return $this->socialMedia; }
|
||||
public function getLogoUrl(): ?string { return $this->logoUrl; }
|
||||
|
||||
public function setName(string $v): self { $this->name = $v; return $this; }
|
||||
public function setStatus(int $v): self { $this->status = $v; return $this; }
|
||||
public function setWeight(int $v): self { $this->weight = $v; return $this; }
|
||||
public function setProvince(?Province $v): self { $this->province = $v; return $this; }
|
||||
public function setRepresentationId(?int $v): self { $this->representationId = $v; return $this; }
|
||||
public function setContactPhone(?string $v): self { $this->contactPhone = $v; return $this; }
|
||||
public function setEmail(?string $v): self { $this->email = $v; return $this; }
|
||||
public function setDescription(?string $v): self { $this->description = $v; return $this; }
|
||||
public function setSlogan(?string $v): self { $this->slogan = $v; return $this; }
|
||||
public function setDomain(?string $v): self { $this->domain = $v; return $this; }
|
||||
public function setKeywords(?string $v): self { $this->keywords = $v; return $this; }
|
||||
public function setFooterDescription(?string $v): self { $this->footerDescription = $v; return $this; }
|
||||
public function setSocialMedia(?array $v): self { $this->socialMedia = $v; return $this; }
|
||||
public function setLogoUrl(?string $v): self { $this->logoUrl = $v; return $this; }
|
||||
|
||||
public function toArray(): array
|
||||
{
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'uuid' => $this->uuid,
|
||||
'name' => $this->name,
|
||||
'status' => $this->status,
|
||||
'weight' => $this->weight,
|
||||
'province_id' => $this->province?->getId(),
|
||||
'province_name' => $this->province?->getName(),
|
||||
'representation_id' => $this->representationId,
|
||||
'contact_phone' => $this->contactPhone,
|
||||
'email' => $this->email,
|
||||
'description' => $this->description,
|
||||
'slogan' => $this->slogan,
|
||||
'domain' => $this->domain,
|
||||
'keywords' => $this->keywords,
|
||||
'footer_description' => $this->footerDescription,
|
||||
'social_media' => $this->socialMedia,
|
||||
'logo_url' => $this->logoUrl,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
|
||||
namespace App\Location\Entity;
|
||||
|
||||
use App\Location\Repository\ProvinceRepository;
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
use Symfony\Component\Uid\Uuid;
|
||||
|
||||
#[ORM\Entity(repositoryClass: ProvinceRepository::class)]
|
||||
#[ORM\Table(name: 'provinces')]
|
||||
#[ORM\Index(columns: ['status'], name: 'idx_provinces_status')]
|
||||
class Province
|
||||
{
|
||||
#[ORM\Id]
|
||||
#[ORM\GeneratedValue]
|
||||
#[ORM\Column(type: 'integer')]
|
||||
private ?int $id = null;
|
||||
|
||||
#[ORM\Column(type: 'string', length: 36, unique: true)]
|
||||
private string $uuid;
|
||||
|
||||
#[ORM\Column(type: 'string', length: 255)]
|
||||
private string $name;
|
||||
|
||||
#[ORM\Column(type: 'smallint')]
|
||||
private int $status = 1;
|
||||
|
||||
#[ORM\Column(type: 'integer')]
|
||||
private int $weight = 0;
|
||||
|
||||
public function __construct(string $name)
|
||||
{
|
||||
$this->uuid = Uuid::v4()->toRfc4122();
|
||||
$this->name = $name;
|
||||
}
|
||||
|
||||
public function getId(): ?int { return $this->id; }
|
||||
public function getUuid(): string { return $this->uuid; }
|
||||
public function getName(): string { return $this->name; }
|
||||
public function getStatus(): int { return $this->status; }
|
||||
public function getWeight(): int { return $this->weight; }
|
||||
|
||||
public function setName(string $v): self { $this->name = $v; return $this; }
|
||||
public function setStatus(int $v): self { $this->status = $v; return $this; }
|
||||
public function setWeight(int $v): self { $this->weight = $v; return $this; }
|
||||
|
||||
public function toArray(): array
|
||||
{
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'uuid' => $this->uuid,
|
||||
'name' => $this->name,
|
||||
'status' => $this->status,
|
||||
'weight' => $this->weight,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
namespace App\Location\Repository;
|
||||
|
||||
use App\Location\Entity\City;
|
||||
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
|
||||
use Doctrine\Persistence\ManagerRegistry;
|
||||
|
||||
class CityRepository extends ServiceEntityRepository
|
||||
{
|
||||
public function __construct(ManagerRegistry $registry)
|
||||
{
|
||||
parent::__construct($registry, City::class);
|
||||
}
|
||||
|
||||
/** @return City[] */
|
||||
public function findActive(?int $provinceId = null): array
|
||||
{
|
||||
$qb = $this->createQueryBuilder('c')
|
||||
->where('c.status = 1')
|
||||
->orderBy('c.weight', 'ASC')
|
||||
->addOrderBy('c.name', 'ASC');
|
||||
|
||||
if ($provinceId !== null) {
|
||||
$qb->andWhere('c.province = :province')->setParameter('province', $provinceId);
|
||||
}
|
||||
|
||||
return $qb->getQuery()->getResult();
|
||||
}
|
||||
|
||||
public function save(City $city, bool $flush = true): void
|
||||
{
|
||||
$this->getEntityManager()->persist($city);
|
||||
if ($flush) $this->getEntityManager()->flush();
|
||||
}
|
||||
|
||||
public function remove(City $city, bool $flush = true): void
|
||||
{
|
||||
$this->getEntityManager()->remove($city);
|
||||
if ($flush) $this->getEntityManager()->flush();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
namespace App\Location\Repository;
|
||||
|
||||
use App\Location\Entity\Province;
|
||||
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
|
||||
use Doctrine\Persistence\ManagerRegistry;
|
||||
|
||||
class ProvinceRepository extends ServiceEntityRepository
|
||||
{
|
||||
public function __construct(ManagerRegistry $registry)
|
||||
{
|
||||
parent::__construct($registry, Province::class);
|
||||
}
|
||||
|
||||
/** @return Province[] */
|
||||
public function findActive(): array
|
||||
{
|
||||
return $this->createQueryBuilder('p')
|
||||
->where('p.status = 1')
|
||||
->orderBy('p.weight', 'ASC')
|
||||
->addOrderBy('p.name', 'ASC')
|
||||
->getQuery()
|
||||
->getResult();
|
||||
}
|
||||
|
||||
public function save(Province $province, bool $flush = true): void
|
||||
{
|
||||
$this->getEntityManager()->persist($province);
|
||||
if ($flush) $this->getEntityManager()->flush();
|
||||
}
|
||||
|
||||
public function remove(Province $province, bool $flush = true): void
|
||||
{
|
||||
$this->getEntityManager()->remove($province);
|
||||
if ($flush) $this->getEntityManager()->flush();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user