issues/27: update crud from admin api
This commit is contained in:
committed by
Valeriy Petrov
parent
839ccdffb5
commit
bc5468e5a0
@@ -4,52 +4,42 @@ namespace App\Controller;
|
||||
|
||||
use App\Entity\Article;
|
||||
use App\Repository\ArticleRepository;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use App\Service\Crud\CrudResponder;
|
||||
use App\Service\Pagination\Paginator;
|
||||
use OpenApi\Attributes as OA;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\JsonResponse;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\Routing\Annotation\Route;
|
||||
use Symfony\Component\Security\Http\Attribute\IsGranted;
|
||||
use Symfony\Component\Serializer\SerializerInterface;
|
||||
use Symfony\Component\Validator\Validator\ValidatorInterface;
|
||||
use Exception;
|
||||
|
||||
#[Route('/article')]
|
||||
final class ArticleController extends AbstractController
|
||||
{
|
||||
public function __construct(
|
||||
private EntityManagerInterface $em,
|
||||
private ValidatorInterface $validator,
|
||||
private SerializerInterface $serializer
|
||||
) { }
|
||||
private const READ_GROUPS = ['article:read'];
|
||||
private const WRITE_GROUPS = ['article:write'];
|
||||
|
||||
public function __construct(
|
||||
private readonly CrudResponder $crud,
|
||||
private readonly Paginator $paginator,
|
||||
) {
|
||||
}
|
||||
|
||||
#[OA\Tag(name: 'Статьи')]
|
||||
#[OA\Parameter(name: 'page', in: 'query', schema: new OA\Schema(type: 'integer'))]
|
||||
#[OA\Parameter(name: 'perPage', in: 'query', schema: new OA\Schema(type: 'integer'))]
|
||||
#[OA\Parameter(name: 'regionId', in: 'query', schema: new OA\Schema(type: 'integer'))]
|
||||
#[OA\Parameter(name: 'active', in: 'query', schema: new OA\Schema(type: 'boolean'))]
|
||||
#[OA\Parameter(name: 'alias', in: 'query', schema: new OA\Schema(type: 'string'))]
|
||||
#[OA\Parameter(name: 'search', in: 'query', schema: new OA\Schema(type: 'string'))]
|
||||
#[Route('/list', name: 'article_list', methods: ['GET'])]
|
||||
public function list(Request $request, ArticleRepository $repository): JsonResponse
|
||||
{
|
||||
$page = max(1, (int) $request->query->get('page', 1));
|
||||
$limit = min(100, max(1, (int) $request->query->get('limit', 20)));
|
||||
$qb = $repository->createFilteredQueryBuilder($request->query->all());
|
||||
|
||||
$filters = [
|
||||
'alias' => $request->query->get('alias', ''),
|
||||
'active' => $request->query->get('active', ''),
|
||||
'regionId' => $request->query->get('regionId', ''),
|
||||
];
|
||||
|
||||
$articles = $repository->findByFilters($filters, $page, $limit);
|
||||
$total = $repository->countByFilters($filters);
|
||||
$totalPages = (int) ceil($total / $limit);
|
||||
|
||||
return $this->json([
|
||||
'data' => $articles,
|
||||
'meta' => [
|
||||
'total' => $total,
|
||||
'page' => $page,
|
||||
'limit' => $limit,
|
||||
'totalPages' => $totalPages,
|
||||
],
|
||||
], Response::HTTP_OK, [], [
|
||||
'groups' => ['article:read']
|
||||
return $this->json($this->paginator->paginate($qb, $request), Response::HTTP_OK, [], [
|
||||
'groups' => self::READ_GROUPS,
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -60,99 +50,34 @@ final class ArticleController extends AbstractController
|
||||
if (!$article) {
|
||||
throw $this->createNotFoundException('Статья не найдена');
|
||||
}
|
||||
return $this->json($article, Response::HTTP_OK, [], [
|
||||
'groups' => ['article:read']
|
||||
]);
|
||||
|
||||
return $this->crud->read($article, self::READ_GROUPS);
|
||||
}
|
||||
|
||||
#[Route('/{id}', name: 'article_show', methods: ['GET'], requirements: ['id' => '\d+'])]
|
||||
public function show(Article $article): JsonResponse
|
||||
{
|
||||
return $this->json($article, Response::HTTP_OK, [], [
|
||||
'groups' => ['article:read']
|
||||
]);
|
||||
return $this->crud->read($article, self::READ_GROUPS);
|
||||
}
|
||||
|
||||
#[IsGranted('ROLE_ADMIN')]
|
||||
#[Route('/create', name: 'article_create', methods: ['POST'])]
|
||||
public function create(Request $request): JsonResponse
|
||||
{
|
||||
try {
|
||||
$article = $this->serializer->deserialize(
|
||||
$request->getContent(),
|
||||
Article::class,
|
||||
'json',
|
||||
['groups' => ['article:write']]
|
||||
);
|
||||
|
||||
$errors = $this->validator->validate($article);
|
||||
|
||||
if (count($errors) > 0) {
|
||||
return $this->json($errors, Response::HTTP_BAD_REQUEST);
|
||||
}
|
||||
|
||||
$this->em->persist($article);
|
||||
$this->em->flush();
|
||||
|
||||
return $this->json($article, Response::HTTP_CREATED, [], [
|
||||
'groups' => ['article:read']
|
||||
]);
|
||||
} catch (Exception $e) {
|
||||
return new JsonResponse([
|
||||
'error' => 'Ошибка при создании статьи',
|
||||
'message' => $e->getMessage()
|
||||
], Response::HTTP_INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
return $this->crud->create($request, Article::class, self::WRITE_GROUPS, self::READ_GROUPS);
|
||||
}
|
||||
|
||||
#[IsGranted('ROLE_ADMIN')]
|
||||
#[Route('/{id}', name: 'article_update', methods: ['PUT'], requirements: ['id' => '\d+'])]
|
||||
public function update(Request $request, Article $article): JsonResponse
|
||||
{
|
||||
try {
|
||||
$this->serializer->deserialize(
|
||||
$request->getContent(),
|
||||
Article::class,
|
||||
'json',
|
||||
[
|
||||
'groups' => ['article:write'],
|
||||
'object_to_populate' => $article
|
||||
]
|
||||
);
|
||||
|
||||
$errors = $this->validator->validate($article);
|
||||
|
||||
if (count($errors) > 0) {
|
||||
return $this->json($errors, Response::HTTP_BAD_REQUEST);
|
||||
}
|
||||
|
||||
$this->em->flush();
|
||||
|
||||
return $this->json($article, Response::HTTP_OK, [], [
|
||||
'groups' => ['article:read']
|
||||
]);
|
||||
} catch (Exception $e) {
|
||||
return new JsonResponse([
|
||||
'error' => 'Ошибка при обновлении статьи',
|
||||
'message' => $e->getMessage()
|
||||
], Response::HTTP_INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
return $this->crud->update($request, $article, self::WRITE_GROUPS, self::READ_GROUPS);
|
||||
}
|
||||
|
||||
#[IsGranted('ROLE_ADMIN')]
|
||||
#[Route('/{id}', name: 'article_delete', methods: ['DELETE'], requirements: ['id' => '\d+'])]
|
||||
public function delete(Article $article): JsonResponse
|
||||
{
|
||||
try {
|
||||
$this->em->remove($article);
|
||||
$this->em->flush();
|
||||
|
||||
return new JsonResponse(null, Response::HTTP_NO_CONTENT);
|
||||
} catch (Exception $e) {
|
||||
return new JsonResponse([
|
||||
'error' => 'Ошибка при удалении статьи',
|
||||
'message' => $e->getMessage()
|
||||
], Response::HTTP_INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
return $this->crud->delete($article);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user