issues/27: update crud from admin api
This commit is contained in:
committed by
Valeriy Petrov
parent
839ccdffb5
commit
bc5468e5a0
@@ -3,7 +3,10 @@
|
||||
namespace App\Controller;
|
||||
|
||||
use App\Entity\Promo;
|
||||
use App\Service\PromoCrudService;
|
||||
use App\Repository\PromoRepository;
|
||||
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;
|
||||
@@ -14,72 +17,55 @@ use Symfony\Component\Security\Http\Attribute\IsGranted;
|
||||
#[Route('/promo')]
|
||||
final class PromoController extends AbstractController
|
||||
{
|
||||
private const READ_GROUPS = ['promo:read'];
|
||||
private const WRITE_GROUPS = ['promo:write'];
|
||||
|
||||
public function __construct(
|
||||
private PromoCrudService $promoCrud,
|
||||
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: 'search', in: 'query', schema: new OA\Schema(type: 'string'))]
|
||||
#[Route('/list', name: 'promo_list', methods: ['GET'])]
|
||||
public function list(Request $request): JsonResponse
|
||||
public function list(Request $request, PromoRepository $repository): JsonResponse
|
||||
{
|
||||
$regionId = $request->query->getInt('regionId', 0);
|
||||
$activeParam = $request->query->get('active');
|
||||
$active = $activeParam === null ? true : filter_var($activeParam, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
|
||||
if ($activeParam !== null && $active === null) {
|
||||
return $this->json(['error' => 'Параметр active должен быть boolean'], Response::HTTP_BAD_REQUEST);
|
||||
}
|
||||
$qb = $repository->createFilteredQueryBuilder($request->query->all());
|
||||
|
||||
return $this->json(['data' => $this->promoCrud->getList($regionId > 0 ? $regionId : null, $active)], Response::HTTP_OK, [], [
|
||||
'groups' => ['promo:read'],
|
||||
return $this->json($this->paginator->paginate($qb, $request), Response::HTTP_OK, [], [
|
||||
'groups' => self::READ_GROUPS,
|
||||
]);
|
||||
}
|
||||
|
||||
#[Route('/{id}', name: 'promo_show', methods: ['GET'], requirements: ['id' => '\d+'])]
|
||||
public function show(Promo $promo): JsonResponse
|
||||
{
|
||||
return $this->json($promo, Response::HTTP_OK, [], [
|
||||
'groups' => ['promo:read'],
|
||||
]);
|
||||
return $this->crud->read($promo, self::READ_GROUPS);
|
||||
}
|
||||
|
||||
#[IsGranted('ROLE_ADMIN')]
|
||||
#[Route('/create', name: 'promo_create', methods: ['POST'])]
|
||||
public function create(Request $request): JsonResponse
|
||||
{
|
||||
$data = json_decode($request->getContent(), true);
|
||||
if (!is_array($data)) {
|
||||
return $this->json(['error' => 'Ожидается JSON-объект в теле запроса'], Response::HTTP_BAD_REQUEST);
|
||||
}
|
||||
|
||||
$promo = $this->promoCrud->create($data);
|
||||
|
||||
return $this->json($promo, Response::HTTP_CREATED, [], [
|
||||
'groups' => ['promo:read'],
|
||||
]);
|
||||
return $this->crud->create($request, Promo::class, self::WRITE_GROUPS, self::READ_GROUPS);
|
||||
}
|
||||
|
||||
#[IsGranted('ROLE_ADMIN')]
|
||||
#[Route('/{id}', name: 'promo_update', methods: ['PUT'], requirements: ['id' => '\d+'])]
|
||||
public function update(Promo $promo, Request $request): JsonResponse
|
||||
public function update(Request $request, Promo $promo): JsonResponse
|
||||
{
|
||||
$data = json_decode($request->getContent(), true);
|
||||
if (!is_array($data)) {
|
||||
return $this->json(['error' => 'Ожидается JSON-объект в теле запроса'], Response::HTTP_BAD_REQUEST);
|
||||
}
|
||||
|
||||
$promo = $this->promoCrud->update($promo, $data);
|
||||
|
||||
return $this->json($promo, Response::HTTP_OK, [], [
|
||||
'groups' => ['promo:read'],
|
||||
]);
|
||||
return $this->crud->update($request, $promo, self::WRITE_GROUPS, self::READ_GROUPS);
|
||||
}
|
||||
|
||||
#[IsGranted('ROLE_ADMIN')]
|
||||
#[Route('/{id}', name: 'promo_delete', methods: ['DELETE'], requirements: ['id' => '\d+'])]
|
||||
public function delete(Promo $promo): JsonResponse
|
||||
{
|
||||
$this->promoCrud->delete($promo);
|
||||
|
||||
return new JsonResponse(null, Response::HTTP_NO_CONTENT);
|
||||
return $this->crud->delete($promo);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user