issues/27: prod baseline without task branch

This commit is contained in:
Valery Petrov
2026-05-28 19:54:31 +03:00
parent f8f03fe849
commit 8579fe3472
31 changed files with 1468 additions and 986 deletions
+101 -30
View File
@@ -2,46 +2,54 @@
namespace App\Controller;
use App\Dto\Content\ContentFilterDto;
use App\Entity\Article;
use App\Repository\ArticleRepository;
use App\Service\Crud\CrudResponder;
use App\Service\Pagination\Paginator;
use Nelmio\ApiDocBundle\Attribute\Model;
use OpenApi\Attributes as OA;
use Doctrine\ORM\EntityManagerInterface;
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
{
private const READ_GROUPS = ['article:read'];
private const WRITE_GROUPS = ['article:write'];
public function __construct(
private readonly CrudResponder $crud,
private readonly Paginator $paginator,
) {
}
private EntityManagerInterface $em,
private ValidatorInterface $validator,
private SerializerInterface $serializer
) { }
#[OA\Tag(name: 'Статьи')]
#[OA\Parameter(name: 'page', in: 'query', schema: new OA\Schema(type: 'integer'))]
#[OA\Parameter(name: 'limit', 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
{
$qb = $repository->createFilteredQueryBuilder(ContentFilterDto::fromRequest($request));
$page = max(1, (int) $request->query->get('page', 1));
$limit = min(100, max(1, (int) $request->query->get('limit', 20)));
return $this->json($this->paginator->paginateWithLegacyMeta($qb, $request), Response::HTTP_OK, [], [
'groups' => self::READ_GROUPS,
$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']
]);
}
@@ -52,36 +60,99 @@ final class ArticleController extends AbstractController
if (!$article) {
throw $this->createNotFoundException('Статья не найдена');
}
return $this->crud->read($article, self::READ_GROUPS);
return $this->json($article, Response::HTTP_OK, [], [
'groups' => ['article:read']
]);
}
#[Route('/{id}', name: 'article_show', methods: ['GET'], requirements: ['id' => '\d+'])]
public function show(Article $article): JsonResponse
{
return $this->crud->read($article, self::READ_GROUPS);
return $this->json($article, Response::HTTP_OK, [], [
'groups' => ['article:read']
]);
}
#[IsGranted('ROLE_ADMIN')]
#[OA\RequestBody(content: new OA\JsonContent(ref: new Model(type: Article::class, groups: self::WRITE_GROUPS)))]
#[Route('/create', name: 'article_create', methods: ['POST'])]
public function create(Request $request): JsonResponse
{
return $this->crud->create($request, Article::class, self::WRITE_GROUPS, self::READ_GROUPS);
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);
}
}
#[IsGranted('ROLE_ADMIN')]
#[OA\RequestBody(content: new OA\JsonContent(ref: new Model(type: Article::class, groups: self::WRITE_GROUPS)))]
#[Route('/{id}', name: 'article_update', methods: ['PUT'], requirements: ['id' => '\d+'])]
public function update(Request $request, Article $article): JsonResponse
{
return $this->crud->update($request, $article, self::WRITE_GROUPS, self::READ_GROUPS);
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);
}
}
#[IsGranted('ROLE_ADMIN')]
#[Route('/{id}', name: 'article_delete', methods: ['DELETE'], requirements: ['id' => '\d+'])]
public function delete(Article $article): JsonResponse
{
return $this->crud->delete($article);
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);
}
}
}