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,131 @@
|
||||
<?php
|
||||
|
||||
namespace App\Specialty\Controller;
|
||||
|
||||
use App\Shared\Constant\ErrorCodes;
|
||||
use App\Shared\Controller\BaseController;
|
||||
use App\Specialty\Entity\Specialty;
|
||||
use App\Specialty\Repository\SpecialtyRepository;
|
||||
use Symfony\Component\HttpFoundation\JsonResponse;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\Routing\Attribute\Route;
|
||||
use Symfony\Component\Security\Http\Attribute\IsGranted;
|
||||
|
||||
class SpecialtyController extends BaseController
|
||||
{
|
||||
public function __construct(
|
||||
private readonly SpecialtyRepository $repo,
|
||||
) {}
|
||||
|
||||
#[Route('/api/v1/specialties', methods: ['GET'])]
|
||||
public function list(Request $request): JsonResponse
|
||||
{
|
||||
$parentId = $request->query->get('parent_id');
|
||||
$items = array_map(
|
||||
fn(Specialty $s) => $s->toArray(),
|
||||
$this->repo->findActive($parentId !== null ? (int) $parentId : null)
|
||||
);
|
||||
return $this->success(['data' => $items]);
|
||||
}
|
||||
|
||||
// ── Admin CRUD ────────────────────────────────────────────────────────────
|
||||
|
||||
#[Route('/api/v1/admin/specialty', methods: ['POST'])]
|
||||
#[IsGranted('ROLE_ADMIN')]
|
||||
public function create(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');
|
||||
}
|
||||
|
||||
$slug = $data['slug'] ?? $this->slugify($name);
|
||||
if ($this->repo->findBySlug($slug) !== null) {
|
||||
$slug = $slug . '-' . time();
|
||||
}
|
||||
|
||||
$parent = null;
|
||||
if (!empty($data['parent_id'])) {
|
||||
$parent = $this->repo->find((int) $data['parent_id']);
|
||||
}
|
||||
|
||||
$specialty = new Specialty($name, $slug, $parent);
|
||||
if (isset($data['status'])) $specialty->setStatus((int) $data['status']);
|
||||
if (isset($data['weight'])) $specialty->setWeight((int) $data['weight']);
|
||||
|
||||
$this->repo->save($specialty);
|
||||
return $this->success(['data' => $specialty->toArray()], 201);
|
||||
}
|
||||
|
||||
#[Route('/api/v1/admin/specialty/{id}', methods: ['PATCH'])]
|
||||
#[IsGranted('ROLE_ADMIN')]
|
||||
public function update(int $id, Request $request): JsonResponse
|
||||
{
|
||||
$specialty = $this->repo->find($id);
|
||||
if ($specialty === null) {
|
||||
return $this->error(ErrorCodes::ERR_VALIDATION_002, 'تخصص یافت نشد', 404);
|
||||
}
|
||||
|
||||
$data = json_decode($request->getContent(), true) ?? [];
|
||||
if (isset($data['name'])) $specialty->setName($data['name']);
|
||||
if (isset($data['slug'])) $specialty->setSlug($data['slug']);
|
||||
if (isset($data['status'])) $specialty->setStatus((int) $data['status']);
|
||||
if (isset($data['weight'])) $specialty->setWeight((int) $data['weight']);
|
||||
if (array_key_exists('parent_id', $data)) {
|
||||
$parent = $data['parent_id'] ? $this->repo->find((int) $data['parent_id']) : null;
|
||||
$specialty->setParent($parent);
|
||||
}
|
||||
|
||||
$this->repo->save($specialty);
|
||||
return $this->success(['data' => $specialty->toArray()]);
|
||||
}
|
||||
|
||||
#[Route('/api/v1/admin/specialty/{id}', methods: ['DELETE'])]
|
||||
#[IsGranted('ROLE_ADMIN')]
|
||||
public function delete(int $id): JsonResponse
|
||||
{
|
||||
$specialty = $this->repo->find($id);
|
||||
if ($specialty === null) {
|
||||
return $this->error(ErrorCodes::ERR_VALIDATION_002, 'تخصص یافت نشد', 404);
|
||||
}
|
||||
|
||||
$this->repo->remove($specialty);
|
||||
return $this->success(['message' => 'تخصص با موفقیت حذف شد']);
|
||||
}
|
||||
|
||||
#[Route('/api/v1/admin/specialties', methods: ['GET'])]
|
||||
#[IsGranted('ROLE_ADMIN')]
|
||||
public function adminList(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->repo->createQueryBuilder('s')
|
||||
->leftJoin('s.parent', 'p')
|
||||
->addSelect('p')
|
||||
->orderBy('s.weight', 'ASC')
|
||||
->addOrderBy('s.name', 'ASC');
|
||||
|
||||
if ($search !== '') {
|
||||
$qb->andWhere('s.name LIKE :s')->setParameter('s', '%' . $search . '%');
|
||||
}
|
||||
|
||||
$total = (clone $qb)->select('COUNT(s.id)')->getQuery()->getSingleScalarResult();
|
||||
$rows = $qb->setFirstResult(($page - 1) * $limit)->setMaxResults($limit)->getQuery()->getResult();
|
||||
|
||||
return $this->paginated(
|
||||
array_map(fn(Specialty $s) => $s->toArray(), $rows),
|
||||
(int) $total, $page, $limit
|
||||
);
|
||||
}
|
||||
|
||||
private function slugify(string $text): string
|
||||
{
|
||||
$text = mb_strtolower(trim($text));
|
||||
$text = preg_replace('/\s+/', '-', $text);
|
||||
$text = preg_replace('/[^\p{L}\p{N}\-]/u', '', $text);
|
||||
return $text ?: 'specialty-' . time();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
<?php
|
||||
|
||||
namespace App\Specialty\Entity;
|
||||
|
||||
use App\Specialty\Repository\SpecialtyRepository;
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
|
||||
use Symfony\Component\Uid\Uuid;
|
||||
|
||||
#[ORM\Entity(repositoryClass: SpecialtyRepository::class)]
|
||||
#[ORM\Table(name: 'specialties')]
|
||||
#[ORM\UniqueConstraint(name: 'uq_specialties_slug', columns: ['slug'])]
|
||||
#[ORM\Index(columns: ['status'], name: 'idx_specialties_status')]
|
||||
#[ORM\Index(columns: ['parent_id'], name: 'idx_specialties_parent')]
|
||||
class Specialty
|
||||
{
|
||||
#[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: 'string', length: 255, unique: true)]
|
||||
private string $slug;
|
||||
|
||||
#[ORM\Column(type: 'smallint')]
|
||||
private int $status = 1;
|
||||
|
||||
#[ORM\Column(type: 'integer')]
|
||||
private int $weight = 0;
|
||||
|
||||
#[ORM\ManyToOne(targetEntity: self::class)]
|
||||
#[ORM\JoinColumn(name: 'parent_id', referencedColumnName: 'id', nullable: true, onDelete: 'SET NULL')]
|
||||
private ?self $parent = null;
|
||||
|
||||
public function __construct(string $name, string $slug, ?self $parent = null)
|
||||
{
|
||||
$this->uuid = Uuid::v4()->toRfc4122();
|
||||
$this->name = $name;
|
||||
$this->slug = $slug;
|
||||
$this->parent = $parent;
|
||||
}
|
||||
|
||||
public function getId(): ?int { return $this->id; }
|
||||
public function getUuid(): string { return $this->uuid; }
|
||||
public function getName(): string { return $this->name; }
|
||||
public function getSlug(): string { return $this->slug; }
|
||||
public function getStatus(): int { return $this->status; }
|
||||
public function getWeight(): int { return $this->weight; }
|
||||
public function getParent(): ?self { return $this->parent; }
|
||||
|
||||
public function setName(string $v): self { $this->name = $v; return $this; }
|
||||
public function setSlug(string $v): self { $this->slug = $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 setParent(?self $v): self { $this->parent = $v; return $this; }
|
||||
|
||||
public function toArray(): array
|
||||
{
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'uuid' => $this->uuid,
|
||||
'name' => $this->name,
|
||||
'slug' => $this->slug,
|
||||
'status' => $this->status,
|
||||
'weight' => $this->weight,
|
||||
'parent_id' => $this->parent?->getId(),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
namespace App\Specialty\Repository;
|
||||
|
||||
use App\Specialty\Entity\Specialty;
|
||||
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
|
||||
use Doctrine\Persistence\ManagerRegistry;
|
||||
|
||||
class SpecialtyRepository extends ServiceEntityRepository
|
||||
{
|
||||
public function __construct(ManagerRegistry $registry)
|
||||
{
|
||||
parent::__construct($registry, Specialty::class);
|
||||
}
|
||||
|
||||
/** @return Specialty[] */
|
||||
public function findActive(?int $parentId = null): array
|
||||
{
|
||||
$qb = $this->createQueryBuilder('s')
|
||||
->where('s.status = 1')
|
||||
->orderBy('s.weight', 'ASC')
|
||||
->addOrderBy('s.name', 'ASC');
|
||||
|
||||
if ($parentId !== null) {
|
||||
$qb->andWhere('s.parent = :parent')->setParameter('parent', $parentId);
|
||||
}
|
||||
|
||||
return $qb->getQuery()->getResult();
|
||||
}
|
||||
|
||||
public function findBySlug(string $slug): ?Specialty
|
||||
{
|
||||
return $this->findOneBy(['slug' => $slug]);
|
||||
}
|
||||
|
||||
public function save(Specialty $specialty, bool $flush = true): void
|
||||
{
|
||||
$this->getEntityManager()->persist($specialty);
|
||||
if ($flush) $this->getEntityManager()->flush();
|
||||
}
|
||||
|
||||
public function remove(Specialty $specialty, bool $flush = true): void
|
||||
{
|
||||
$this->getEntityManager()->remove($specialty);
|
||||
if ($flush) $this->getEntityManager()->flush();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user