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,108 @@
|
||||
<?php
|
||||
|
||||
namespace App\Tag\Controller;
|
||||
|
||||
use App\Shared\Constant\ErrorCodes;
|
||||
use App\Shared\Controller\BaseController;
|
||||
use App\Tag\Entity\Tag;
|
||||
use App\Tag\Repository\TagRepository;
|
||||
use Symfony\Component\HttpFoundation\JsonResponse;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\Routing\Attribute\Route;
|
||||
use Symfony\Component\Security\Http\Attribute\IsGranted;
|
||||
|
||||
class TagController extends BaseController
|
||||
{
|
||||
public function __construct(
|
||||
private readonly TagRepository $repo,
|
||||
) {}
|
||||
|
||||
#[Route('/api/v1/tags', methods: ['GET'])]
|
||||
public function list(): JsonResponse
|
||||
{
|
||||
$items = array_map(fn(Tag $t) => $t->toArray(), $this->repo->findActive());
|
||||
return $this->success(['data' => $items]);
|
||||
}
|
||||
|
||||
// ── Admin CRUD ────────────────────────────────────────────────────────────
|
||||
|
||||
#[Route('/api/v1/admin/tag', 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);
|
||||
|
||||
$tag = new Tag($name, $slug);
|
||||
if (isset($data['status'])) $tag->setStatus((int) $data['status']);
|
||||
|
||||
$this->repo->save($tag);
|
||||
return $this->success(['data' => $tag->toArray()], 201);
|
||||
}
|
||||
|
||||
#[Route('/api/v1/admin/tag/{id}', methods: ['PATCH'])]
|
||||
#[IsGranted('ROLE_ADMIN')]
|
||||
public function update(int $id, Request $request): JsonResponse
|
||||
{
|
||||
$tag = $this->repo->find($id);
|
||||
if ($tag === null) {
|
||||
return $this->error(ErrorCodes::ERR_VALIDATION_002, 'تگ یافت نشد', 404);
|
||||
}
|
||||
|
||||
$data = json_decode($request->getContent(), true) ?? [];
|
||||
if (isset($data['name'])) $tag->setName($data['name']);
|
||||
if (isset($data['slug'])) $tag->setSlug($data['slug']);
|
||||
if (isset($data['status'])) $tag->setStatus((int) $data['status']);
|
||||
|
||||
$this->repo->save($tag);
|
||||
return $this->success(['data' => $tag->toArray()]);
|
||||
}
|
||||
|
||||
#[Route('/api/v1/admin/tag/{id}', methods: ['DELETE'])]
|
||||
#[IsGranted('ROLE_ADMIN')]
|
||||
public function delete(int $id): JsonResponse
|
||||
{
|
||||
$tag = $this->repo->find($id);
|
||||
if ($tag === null) {
|
||||
return $this->error(ErrorCodes::ERR_VALIDATION_002, 'تگ یافت نشد', 404);
|
||||
}
|
||||
|
||||
$this->repo->remove($tag);
|
||||
return $this->success(['message' => 'تگ با موفقیت حذف شد']);
|
||||
}
|
||||
|
||||
#[Route('/api/v1/admin/tags', 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('t')->orderBy('t.name', 'ASC');
|
||||
if ($search !== '') {
|
||||
$qb->andWhere('t.name LIKE :s')->setParameter('s', '%' . $search . '%');
|
||||
}
|
||||
|
||||
$total = (clone $qb)->select('COUNT(t.id)')->getQuery()->getSingleScalarResult();
|
||||
$rows = $qb->setFirstResult(($page - 1) * $limit)->setMaxResults($limit)->getQuery()->getResult();
|
||||
|
||||
return $this->paginated(
|
||||
array_map(fn(Tag $t) => $t->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 ?: 'tag-' . time();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
namespace App\Tag\Entity;
|
||||
|
||||
use App\Tag\Repository\TagRepository;
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
use Symfony\Component\Uid\Uuid;
|
||||
|
||||
#[ORM\Entity(repositoryClass: TagRepository::class)]
|
||||
#[ORM\Table(name: 'tags')]
|
||||
#[ORM\UniqueConstraint(name: 'uq_tags_slug', columns: ['slug'])]
|
||||
#[ORM\Index(columns: ['status'], name: 'idx_tags_status')]
|
||||
class Tag
|
||||
{
|
||||
#[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;
|
||||
|
||||
public function __construct(string $name, string $slug)
|
||||
{
|
||||
$this->uuid = Uuid::v4()->toRfc4122();
|
||||
$this->name = $name;
|
||||
$this->slug = $slug;
|
||||
}
|
||||
|
||||
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 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 toArray(): array
|
||||
{
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'uuid' => $this->uuid,
|
||||
'name' => $this->name,
|
||||
'slug' => $this->slug,
|
||||
'status' => $this->status,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace App\Tag\Repository;
|
||||
|
||||
use App\Tag\Entity\Tag;
|
||||
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
|
||||
use Doctrine\Persistence\ManagerRegistry;
|
||||
|
||||
class TagRepository extends ServiceEntityRepository
|
||||
{
|
||||
public function __construct(ManagerRegistry $registry)
|
||||
{
|
||||
parent::__construct($registry, Tag::class);
|
||||
}
|
||||
|
||||
/** @return Tag[] */
|
||||
public function findActive(): array
|
||||
{
|
||||
return $this->createQueryBuilder('t')
|
||||
->where('t.status = 1')
|
||||
->orderBy('t.name', 'ASC')
|
||||
->getQuery()
|
||||
->getResult();
|
||||
}
|
||||
|
||||
public function save(Tag $tag, bool $flush = true): void
|
||||
{
|
||||
$this->getEntityManager()->persist($tag);
|
||||
if ($flush) $this->getEntityManager()->flush();
|
||||
}
|
||||
|
||||
public function remove(Tag $tag, bool $flush = true): void
|
||||
{
|
||||
$this->getEntityManager()->remove($tag);
|
||||
if ($flush) $this->getEntityManager()->flush();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user