Commit 85ecf8b0 authored by Адлан Шамавов's avatar Адлан Шамавов
Browse files

STA-1047 | Добавлены POST, PUT, PATCH, DELETE для контроллеров

parent ddafca3f
Loading
Loading
Loading
Loading
+46 −0
Original line number Diff line number Diff line
<?php

namespace App\ArgumentResolver;

use App\Exception\RequestBodyConvertException;
use App\Exception\ValidationException;
use Symfony\Component\HttpKernel\Controller\ValueResolverInterface;
use Symfony\Component\Validator\Validator\ValidatorInterface;
use App\Attribute\RequestBody;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\SerializerInterface;

class RequestBodyArgumentResolver implements ValueResolverInterface
{

    public function __construct(
        private SerializerInterface $serializer,
        private ValidatorInterface $validator,
    ) {}

    public function resolve(Request $request, ArgumentMetadata $argument): iterable
    {
        if (!$argument->getAttributesOfType(RequestBody::class, ArgumentMetadata::IS_INSTANCEOF)) {
            return [];
        }

        try {
            $model = $this->serializer->deserialize(
                $request->getContent(),
                $argument->getType(),
                JsonEncoder::FORMAT
            );
        } catch (\Throwable $throwable) {
            throw new RequestBodyConvertException($throwable->getMessage());
        }

        $errors = $this->validator->validate($model);
        if (count($errors) > 0) {
            throw new ValidationException($errors);
        }

        return [$model];
    }
}
 No newline at end of file
+11 −0
Original line number Diff line number Diff line
<?php

namespace App\Attribute;

use Attribute;

#[Attribute(Attribute::TARGET_PARAMETER)]
class RequestBody
{

}
 No newline at end of file
+33 −0
Original line number Diff line number Diff line
@@ -4,7 +4,10 @@ declare(strict_types=1);

namespace App\Controller;

use App\Attribute\RequestBody;
use App\Exception\NewsNotFoundException;
use App\Requests\CreateNewsRequest;
use App\Requests\EditNewsRequest;
use App\Requests\NewsListRequest;
use App\Service\NewsService;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
@@ -55,4 +58,34 @@ class NewsController extends AbstractController
            ], $e->getCode());
        }
    }

    #[Route('/news', name: 'addNews', methods: ['POST'])]
    public function addNews(
        #[RequestBody] CreateNewsRequest $request
    ): Response {
        return $this->json($this->newsService->addNewsByRequest($request));
    }

    #[Route('/news/{id}', name: 'putNews', methods: ['PUT'])]
    public function putNews(
        string $id,
        #[RequestBody] EditNewsRequest $request
    ): Response {
        return $this->json($this->newsService->putNewsByRequest($id, $request));
    }

    #[Route('/news/{id}', name: 'patchNews', methods: ['PATCH'])]
    public function patchNews(
        string $id,
        #[RequestBody] EditNewsRequest $request
    ): Response {
        return $this->json($this->newsService->patchNewsByRequest($id, $request));
    }

    #[Route('/news/{id}', name: 'deleteNews', methods: ['DELETE'])]
    public function deleteNews(string $id): Response
    {
        $this->newsService->deleteNews($id);
        return $this->json(null);
    }
}
+33 −0
Original line number Diff line number Diff line
@@ -4,7 +4,10 @@ declare(strict_types=1);

namespace App\Controller;

use App\Attribute\RequestBody;
use App\Exception\RestaurantNotFoundException;
use App\Requests\CreateRestaurantRequest;
use App\Requests\EditRestaurantRequest;
use App\Requests\RestaurantListRequest;
use App\Service\RestaurantService;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
@@ -39,4 +42,34 @@ class RestaurantController extends AbstractController
            ], $e->getCode());
        }
    }

    #[Route('/restaurant', name: 'addRestaurant', methods: ['POST'])]
    public function addRestaurant(
        #[RequestBody] CreateRestaurantRequest $request
    ): Response {
        return $this->json($this->restaurantService->addRestaurantByRequest($request));
    }

    #[Route('/restaurant/{id}', name: 'putRestaurant', methods: ['PUT'])]
    public function putRestaurant(
        string $id,
        #[RequestBody] EditRestaurantRequest $request
    ): Response {
        return $this->json($this->restaurantService->putRestaurantByRequest($id, $request));
    }

    #[Route('/restaurant/{id}', name: 'patchRestaurant', methods: ['PATCH'])]
    public function patchRestaurant(
        string $id,
        #[RequestBody] EditRestaurantRequest $request
    ): Response {
        return $this->json($this->restaurantService->patchRestaurantByRequest($id, $request));
    }

    #[Route('/restaurant/{id}', name: 'deleteRestaurant', methods: ['DELETE'])]
    public function deleteRestaurant(string $id): Response
    {
        $this->restaurantService->deleteRestaurant($id);
        return $this->json(null);
    }
}
+1 −1
Original line number Diff line number Diff line
@@ -63,7 +63,7 @@ class News
    #[ORM\JoinColumn(nullable: false)]
    private ?Seo $seo = null;

    #[ORM\OneToOne(cascade: ['persist', 'remove'])]
    #[ORM\OneToOne]
    #[ORM\JoinColumn(nullable: false)]
    private ?File $file = null;

Loading