From b544b8280d0b18b339362efd443c1e4d0fed58b7 Mon Sep 17 00:00:00 2001 From: AlexP Date: Fri, 17 May 2024 13:32:24 +0500 Subject: [PATCH] STA-960 | fixes --- app/src/News/Controller/NewsController.php | 50 ++++++-------- app/src/News/Dto/NewsDetailElementDto.php | 3 +- app/src/News/Dto/NewsListDto.php | 3 +- app/src/News/Dto/NewsListingElementDto.php | 3 +- app/src/News/Request/NewsDetailRequest.php | 43 ++++++++++++ app/src/News/Request/NewsListingRequest.php | 51 ++++++++++++++ .../Service/NewsPrepareResponseService.php | 66 +++++++------------ .../News/UseCase/NewsGetDetailMainUseCase.php | 27 ++++++++ app/src/News/UseCase/NewsGetDetailUseCase.php | 34 ++++++++++ .../News/UseCase/NewsGetListingUseUseCase.php | 27 ++++++++ app/src/News/UseCase/NewsGetMainUseCase.php | 26 ++++++++ .../Controller/RestaurantsController.php | 39 ++++------- .../Dto/RestaurantDetailElementDto.php | 3 +- app/src/Restaurants/Dto/RestaurantListDto.php | 3 +- .../Dto/RestaurantListingElementDto.php | 3 +- .../Request/RestaurantDetailRequest.php | 43 ++++++++++++ .../Request/RestaurantListingRequest.php | 56 ++++++++++++++++ .../RestaurantPrepareRequestService.php | 60 +++++------------ .../UseCase/RestaurantGetDetailUseCase.php | 34 ++++++++++ .../UseCase/RestaurantGetListingUseCase.php | 27 ++++++++ .../Shared/Abstraction/AbstractController.php | 53 --------------- .../Abstraction/AbstractDetailElement.php | 9 --- .../Shared/Abstraction/AbstractListDto.php | 14 ---- .../Abstraction/AbstractListingElementDto.php | 10 --- .../Shared/Abstraction/ServiceInterface.php | 21 ------ .../DtoFactory/PaginationDtoFactory.php | 5 +- app/src/Shared/Service/ValidateService.php | 25 ------- app/src/Shared/Service/ValidationService.php | 29 ++++++++ 28 files changed, 478 insertions(+), 289 deletions(-) create mode 100644 app/src/News/Request/NewsDetailRequest.php create mode 100644 app/src/News/Request/NewsListingRequest.php create mode 100644 app/src/News/UseCase/NewsGetDetailMainUseCase.php create mode 100644 app/src/News/UseCase/NewsGetDetailUseCase.php create mode 100644 app/src/News/UseCase/NewsGetListingUseUseCase.php create mode 100644 app/src/News/UseCase/NewsGetMainUseCase.php create mode 100644 app/src/Restaurants/Request/RestaurantDetailRequest.php create mode 100644 app/src/Restaurants/Request/RestaurantListingRequest.php create mode 100644 app/src/Restaurants/UseCase/RestaurantGetDetailUseCase.php create mode 100644 app/src/Restaurants/UseCase/RestaurantGetListingUseCase.php delete mode 100644 app/src/Shared/Abstraction/AbstractController.php delete mode 100644 app/src/Shared/Abstraction/AbstractDetailElement.php delete mode 100644 app/src/Shared/Abstraction/AbstractListDto.php delete mode 100644 app/src/Shared/Abstraction/AbstractListingElementDto.php delete mode 100644 app/src/Shared/Abstraction/ServiceInterface.php delete mode 100644 app/src/Shared/Service/ValidateService.php create mode 100644 app/src/Shared/Service/ValidationService.php diff --git a/app/src/News/Controller/NewsController.php b/app/src/News/Controller/NewsController.php index 98694b5..34e1dbb 100644 --- a/app/src/News/Controller/NewsController.php +++ b/app/src/News/Controller/NewsController.php @@ -2,58 +2,48 @@ namespace App\News\Controller; -use App\News\Service\NewsPrepareResponseService; -use App\Shared\Abstraction\AbstractController; -use App\Shared\DtoFactory\ErrorDtoFactory; +use App\News\Request\NewsDetailRequest; +use App\News\Request\NewsListingRequest; +use App\News\UseCase\NewsGetDetailMainUseCase; +use App\News\UseCase\NewsGetDetailUseCase; +use App\News\UseCase\NewsGetListingUseUseCase; +use App\News\UseCase\NewsGetMainUseCase; +use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\JsonResponse; -use Symfony\Component\HttpKernel\Attribute\MapQueryParameter; use Symfony\Component\Routing\Attribute\Route; #[Route('/api/v1/news')] class NewsController extends AbstractController { public function __construct( - ErrorDtoFactory $errorFactory, - private readonly NewsPrepareResponseService $responsePrepareService, + private readonly NewsGetListingUseUseCase $getListingUseCase, + private readonly NewsGetDetailUseCase $getDetailNews, + private readonly NewsGetMainUseCase $getMainNews, + private readonly NewsGetDetailMainUseCase $getDetailMainNews, ) { - parent::__construct($errorFactory); } #[Route('/', name: 'news', methods: ['GET'])] - public function news( - #[MapQueryParameter] int $page = 1, - #[MapQueryParameter] int $limit = 12, - #[MapQueryParameter] string $news_category = null - ): JsonResponse { - return $this->handleListing( - $this->responsePrepareService, - $page, - $limit, - $news_category, - ); + public function news(NewsListingRequest $request): JsonResponse + { + return $this->getListingUseCase->execute($request); } #[Route('/mainNews', name: 'mainNews', methods: ['GET'])] public function mainNews(): JsonResponse { - return new JsonResponse($this->responsePrepareService->bornMainNews()); + return $this->getMainNews->execute(); } #[Route('/search', name: 'searchNews', methods: ['GET'])] - public function search(): JsonResponse + public function detailMainNews(): JsonResponse { - return new JsonResponse( - $this->responsePrepareService->bornDetailMainNews() - ); + return $this->getDetailMainNews->execute(); } #[Route('/{detailId}', name: 'oneNews', methods: ['GET'])] - public function oneNews( - #[MapQueryParameter] string $detailId = null - ): JsonResponse { - return $this->handleDetailElement( - $this->responsePrepareService, - $detailId, - ); + public function oneNews(NewsDetailRequest $request): JsonResponse + { + return $this->getDetailNews->execute($request); } } diff --git a/app/src/News/Dto/NewsDetailElementDto.php b/app/src/News/Dto/NewsDetailElementDto.php index 530f5e0..e715dda 100644 --- a/app/src/News/Dto/NewsDetailElementDto.php +++ b/app/src/News/Dto/NewsDetailElementDto.php @@ -2,10 +2,9 @@ namespace App\News\Dto; -use App\Shared\Abstraction\AbstractDetailElement; use App\Shared\Dto\FileDto; -class NewsDetailElementDto extends AbstractDetailElement +class NewsDetailElementDto { /** * @param string $id diff --git a/app/src/News/Dto/NewsListDto.php b/app/src/News/Dto/NewsListDto.php index 63da8fa..833a93c 100644 --- a/app/src/News/Dto/NewsListDto.php +++ b/app/src/News/Dto/NewsListDto.php @@ -2,11 +2,10 @@ namespace App\News\Dto; -use App\Shared\Abstraction\AbstractListDto; use App\Shared\Collection\ListingCollection; use App\Shared\Dto\PaginationDto; -class NewsListDto extends AbstractListDto +class NewsListDto { /** * @param PaginationDto $pagination diff --git a/app/src/News/Dto/NewsListingElementDto.php b/app/src/News/Dto/NewsListingElementDto.php index a291ea1..0ff49bb 100644 --- a/app/src/News/Dto/NewsListingElementDto.php +++ b/app/src/News/Dto/NewsListingElementDto.php @@ -2,10 +2,9 @@ namespace App\News\Dto; -use App\Shared\Abstraction\AbstractListingElementDto; use App\Shared\Dto\FileDto; -class NewsListingElementDto extends AbstractListingElementDto +class NewsListingElementDto { /** * @param string $id diff --git a/app/src/News/Request/NewsDetailRequest.php b/app/src/News/Request/NewsDetailRequest.php new file mode 100644 index 0000000..8fdcf65 --- /dev/null +++ b/app/src/News/Request/NewsDetailRequest.php @@ -0,0 +1,43 @@ +populate(); + $this->checkAndCorrectParams(); + } + + private function checkAndCorrectParams(): void + { + if (!$this->validation->isUuidValid($this->detailId)) { + throw new NotFoundError('News not found'); + } + } + + private function populate(): void + { + $requestUrl = $this->getRequest()->getUri(); + $index = strrpos($requestUrl, "/") + 1; + + $restaurantId = substr($requestUrl, $index); + + if (property_exists($this, "detailId")) { + $this->{"detailId"} = $restaurantId; + } + } + + public function getRequest(): Request + { + return Request::createFromGlobals(); + } +} \ No newline at end of file diff --git a/app/src/News/Request/NewsListingRequest.php b/app/src/News/Request/NewsListingRequest.php new file mode 100644 index 0000000..fc80383 --- /dev/null +++ b/app/src/News/Request/NewsListingRequest.php @@ -0,0 +1,51 @@ +populate(); + $this->checkAndCorrectParams(); + } + + private function checkAndCorrectParams(): void + { + if (!$this->validation->isPageValid($this->page)) { + $this->page = 1; + } + + if (!$this->validation->isLimitValid($this->limit)) { + $this->limit = 12; + } + + if (!$this->validation->isUuidValid($this->news_category)) { + $this->news_category = null; + } + } + + private function populate(): void + { + foreach ( + $this->getRequest()->query->getIterator() as $property => $value + ) { + if (property_exists($this, $property)) { + $this->{$property} = $value; + } + } + } + + public function getRequest(): Request + { + return Request::createFromGlobals(); + } +} \ No newline at end of file diff --git a/app/src/News/Service/NewsPrepareResponseService.php b/app/src/News/Service/NewsPrepareResponseService.php index dbdd17c..b652b1d 100644 --- a/app/src/News/Service/NewsPrepareResponseService.php +++ b/app/src/News/Service/NewsPrepareResponseService.php @@ -3,61 +3,43 @@ namespace App\News\Service; use App\News\Dto\NewsDetailElementDto; +use App\News\Dto\NewsListDto; use App\News\Dto\NewsListingElementDto; use App\News\DtoFactory\NewsCategoryDtoFactory; use App\News\DtoFactory\NewsDetailElementDtoFactory; use App\News\DtoFactory\NewsFilterVariantsDtoFactory; use App\News\DtoFactory\NewsListDtoFactory; use App\News\DtoFactory\NewsListingElementDtoFactory; -use App\Shared\Abstraction\AbstractListDto; -use App\Shared\Abstraction\ServiceInterface; +use App\News\Request\NewsDetailRequest; +use App\News\Request\NewsListingRequest; use App\Shared\DtoFactory\PaginationDtoFactory; -use App\Shared\Error\NonValidUuidError; use App\Shared\Error\NotFoundError; use App\Shared\Repository\NewsCategoriesRepository; use App\Shared\Repository\NewsRepository; -use App\Shared\Service\ValidateService; -readonly class NewsPrepareResponseService implements ServiceInterface +class NewsPrepareResponseService { public function __construct( - private NewsRepository $news, - private NewsCategoriesRepository $newsCategories, - private PaginationDtoFactory $paginationFactory, - private NewsListingElementDtoFactory $listFactory, - private NewsFilterVariantsDtoFactory $filterFactory, - private NewsListDtoFactory $listingFactory, - private NewsCategoryDtoFactory $categoryFactory, - private ValidateService $validate, - private NewsDetailElementDtoFactory $detailElementFactory + private readonly NewsRepository $news, + private readonly NewsCategoriesRepository $newsCategories, + private readonly PaginationDtoFactory $paginationFactory, + private readonly NewsListingElementDtoFactory $listFactory, + private readonly NewsFilterVariantsDtoFactory $filterFactory, + private readonly NewsListDtoFactory $listingFactory, + private readonly NewsCategoryDtoFactory $categoryFactory, + private readonly NewsDetailElementDtoFactory $detailElementFactory ) { } - /** - * @param int $page - * @param int $limit - * @param array|null ...$filters - * @return AbstractListDto - */ - public function bornListDto( - int $page, - int $limit, - ?array $filters - ): AbstractListDto { - $filters['news_category'] = $filters[0]; - if (!$this->validate->isValidUuid($filters['news_category'])) { - $filters['news_category'] = null; - } - - $this->validate->correctPagination($page, $limit); - + public function bornListDto(NewsListingRequest $request): NewsListDto + { $countOfNews = $this->news->getCountWithFilters( - $filters['news_category'] + $request->news_category ); $pagination = $this->paginationFactory->create( - $page, - $limit, + $request->page, + $request->limit, $countOfNews ); @@ -68,8 +50,8 @@ readonly class NewsPrepareResponseService implements ServiceInterface $list = $this->listFactory->createCollection( $this->news->getWithFilters( - $filters['news_category'], - $limit, + $request->news_category, + $request->limit, $offset ) ); @@ -97,13 +79,9 @@ readonly class NewsPrepareResponseService implements ServiceInterface return $this->detailElementFactory->create($this->news->getMainNews()); } - public function bornDetailElement(string $detailId): NewsDetailElementDto - { - if (!$this->validate->isValidUuid($detailId)) { - throw new NonValidUuidError(); - } - - $news = $this->news->find($detailId); + public function bornDetailElement(NewsDetailRequest $request + ): NewsDetailElementDto { + $news = $this->news->find($request->detailId); if ($news === null) { throw new NotFoundError('News not found'); diff --git a/app/src/News/UseCase/NewsGetDetailMainUseCase.php b/app/src/News/UseCase/NewsGetDetailMainUseCase.php new file mode 100644 index 0000000..92725a1 --- /dev/null +++ b/app/src/News/UseCase/NewsGetDetailMainUseCase.php @@ -0,0 +1,27 @@ +prepareRequestService->bornDetailMainNews() + ); + } catch (Throwable) { + return new JsonResponse([], Response::HTTP_INTERNAL_SERVER_ERROR); + } + } +} \ No newline at end of file diff --git a/app/src/News/UseCase/NewsGetDetailUseCase.php b/app/src/News/UseCase/NewsGetDetailUseCase.php new file mode 100644 index 0000000..2605bd9 --- /dev/null +++ b/app/src/News/UseCase/NewsGetDetailUseCase.php @@ -0,0 +1,34 @@ +responsePrepareService->bornDetailElement($request) + ); + } catch (NotFoundError $error) { + $errorDto = $this->errorFactory->create($error); + return new JsonResponse($errorDto, $errorDto->status); + } catch (Throwable) { + return new JsonResponse([], Response::HTTP_INTERNAL_SERVER_ERROR); + } + } +} \ No newline at end of file diff --git a/app/src/News/UseCase/NewsGetListingUseUseCase.php b/app/src/News/UseCase/NewsGetListingUseUseCase.php new file mode 100644 index 0000000..736c1f9 --- /dev/null +++ b/app/src/News/UseCase/NewsGetListingUseUseCase.php @@ -0,0 +1,27 @@ +prepareRequestService->bornListDto($request) + ); + } catch (Throwable) { + return new JsonResponse([], Response::HTTP_INTERNAL_SERVER_ERROR); + } + } +} diff --git a/app/src/News/UseCase/NewsGetMainUseCase.php b/app/src/News/UseCase/NewsGetMainUseCase.php new file mode 100644 index 0000000..9516d18 --- /dev/null +++ b/app/src/News/UseCase/NewsGetMainUseCase.php @@ -0,0 +1,26 @@ +responsePrepareService->bornMainNews() + ); + } catch (Throwable) { + return new JsonResponse([], Response::HTTP_INTERNAL_SERVER_ERROR); + } + } +} \ No newline at end of file diff --git a/app/src/Restaurants/Controller/RestaurantsController.php b/app/src/Restaurants/Controller/RestaurantsController.php index eea4f2d..c589e3a 100644 --- a/app/src/Restaurants/Controller/RestaurantsController.php +++ b/app/src/Restaurants/Controller/RestaurantsController.php @@ -2,45 +2,32 @@ namespace App\Restaurants\Controller; -use App\Restaurants\Service\RestaurantPrepareRequestService; -use App\Shared\Abstraction\AbstractController; -use App\Shared\DtoFactory\ErrorDtoFactory; +use App\Restaurants\UseCase\RestaurantGetListingUseCase; +use App\Restaurants\Request\RestaurantDetailRequest; +use App\Restaurants\Request\RestaurantListingRequest; +use App\Restaurants\UseCase\RestaurantGetDetailUseCase; +use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\JsonResponse; -use Symfony\Component\HttpKernel\Attribute\MapQueryParameter; use Symfony\Component\Routing\Attribute\Route; #[Route('/api/v1/restaurants')] class RestaurantsController extends AbstractController { public function __construct( - ErrorDtoFactory $errorFactory, - private readonly RestaurantPrepareRequestService $responsePrepareService, + private readonly RestaurantGetListingUseCase $getListingUseCase, + private readonly RestaurantGetDetailUseCase $getDetailUseCase, ) { - parent::__construct($errorFactory); } #[Route('/', name: 'restaurants', methods: ['GET'])] - public function restaurants( - #[MapQueryParameter] int $page = 1, - #[MapQueryParameter] int $limit = 12, - #[MapQueryParameter] string $restaurant_type_id = null, - #[MapQueryParameter] string $kitchen_id = null, - ): JsonResponse { - return $this->handleListing( - $this->responsePrepareService, - $page, - $limit, - $restaurant_type_id, - $kitchen_id, - ); + public function restaurants(RestaurantListingRequest $request): JsonResponse + { + return $this->getListingUseCase->execute($request); } #[Route('/{detailId}', name: 'restaurant', methods: ['GET'])] - public function restaurant(#[MapQueryParameter] string $detailId = null - ): JsonResponse { - return $this->handleDetailElement( - $this->responsePrepareService, - $detailId, - ); + public function restaurant(RestaurantDetailRequest $request): JsonResponse + { + return $this->getDetailUseCase->execute($request); } } diff --git a/app/src/Restaurants/Dto/RestaurantDetailElementDto.php b/app/src/Restaurants/Dto/RestaurantDetailElementDto.php index bf4d83f..75eaa59 100644 --- a/app/src/Restaurants/Dto/RestaurantDetailElementDto.php +++ b/app/src/Restaurants/Dto/RestaurantDetailElementDto.php @@ -4,12 +4,11 @@ namespace App\Restaurants\Dto; use App\Restaurants\Collection\KitchenCollection; use App\Restaurants\Collection\TagCollection; -use App\Shared\Abstraction\AbstractDetailElement; use App\Shared\Collection\FileCollection; use App\Shared\Collection\StringCollection; use App\Shared\Dto\FileDto; -class RestaurantDetailElementDto extends AbstractDetailElement +class RestaurantDetailElementDto { /** * @param string $id diff --git a/app/src/Restaurants/Dto/RestaurantListDto.php b/app/src/Restaurants/Dto/RestaurantListDto.php index 79354d8..1e6ac5d 100644 --- a/app/src/Restaurants/Dto/RestaurantListDto.php +++ b/app/src/Restaurants/Dto/RestaurantListDto.php @@ -2,11 +2,10 @@ namespace App\Restaurants\Dto; -use App\Shared\Abstraction\AbstractListDto; use App\Shared\Collection\ListingCollection; use App\Shared\Dto\PaginationDto; -class RestaurantListDto extends AbstractListDto +class RestaurantListDto { /** * @param PaginationDto $pagination diff --git a/app/src/Restaurants/Dto/RestaurantListingElementDto.php b/app/src/Restaurants/Dto/RestaurantListingElementDto.php index 4d7f3dc..2ffed45 100644 --- a/app/src/Restaurants/Dto/RestaurantListingElementDto.php +++ b/app/src/Restaurants/Dto/RestaurantListingElementDto.php @@ -2,10 +2,9 @@ namespace App\Restaurants\Dto; -use App\Shared\Abstraction\AbstractListingElementDto; use App\Shared\Dto\FileDto; -class RestaurantListingElementDto extends AbstractListingElementDto +class RestaurantListingElementDto { public function __construct( public string $id, diff --git a/app/src/Restaurants/Request/RestaurantDetailRequest.php b/app/src/Restaurants/Request/RestaurantDetailRequest.php new file mode 100644 index 0000000..fbc0acb --- /dev/null +++ b/app/src/Restaurants/Request/RestaurantDetailRequest.php @@ -0,0 +1,43 @@ +populate(); + $this->checkAndCorrectParams(); + } + + private function checkAndCorrectParams(): void + { + if (!$this->validation->isUuidValid($this->detailId)) { + throw new NotFoundError('Restaurant not found'); + } + } + + private function populate(): void + { + $requestUrl = $this->getRequest()->getUri(); + $index = strrpos($requestUrl, "/") + 1; + + $restaurantId = substr($requestUrl, $index); + + if (property_exists($this, "detailId")) { + $this->{"detailId"} = $restaurantId; + } + } + + public function getRequest(): Request + { + return Request::createFromGlobals(); + } +} diff --git a/app/src/Restaurants/Request/RestaurantListingRequest.php b/app/src/Restaurants/Request/RestaurantListingRequest.php new file mode 100644 index 0000000..a198fa0 --- /dev/null +++ b/app/src/Restaurants/Request/RestaurantListingRequest.php @@ -0,0 +1,56 @@ +populate(); + $this->checkAndCorrectParams(); + } + + private function checkAndCorrectParams(): void + { + if (!$this->validation->isPageValid($this->page)) { + $this->page = 1; + } + + if (!$this->validation->isLimitValid($this->limit)) { + $this->limit = 12; + } + + if (!$this->validation->isUuidValid($this->restaurant_type_id)) { + $this->restaurant_type_id = null; + } + + if (!$this->validation->isUuidValid($this->kitchen_id)) { + $this->kitchen_id = null; + } + } + + private function populate(): void + { + foreach ( + $this->getRequest()->query->getIterator() as $property => $value + ) { + if (property_exists($this, $property)) { + $this->{$property} = $value; + } + } + } + + public function getRequest(): Request + { + return Request::createFromGlobals(); + } +} \ No newline at end of file diff --git a/app/src/Restaurants/Service/RestaurantPrepareRequestService.php b/app/src/Restaurants/Service/RestaurantPrepareRequestService.php index 742d4c4..9d24a6a 100644 --- a/app/src/Restaurants/Service/RestaurantPrepareRequestService.php +++ b/app/src/Restaurants/Service/RestaurantPrepareRequestService.php @@ -2,25 +2,24 @@ namespace App\Restaurants\Service; +use App\Restaurants\Dto\RestaurantDetailElementDto; +use App\Restaurants\Dto\RestaurantListDto; use App\Restaurants\DtoFactory\KitchenTypeDtoFactory; use App\Restaurants\DtoFactory\RestaurantDetailElementDtoFactory; use App\Restaurants\DtoFactory\RestaurantFilterVariantsDtoFactory; use App\Restaurants\DtoFactory\RestaurantListDtoFactory; use App\Restaurants\DtoFactory\RestaurantListingElementDtoFactory; use App\Restaurants\DtoFactory\RestaurantTypeDtoFactory; -use App\Shared\Abstraction\AbstractDetailElement; -use App\Shared\Abstraction\AbstractListDto; -use App\Shared\Abstraction\ServiceInterface; +use App\Restaurants\Request\RestaurantDetailRequest; +use App\Restaurants\Request\RestaurantListingRequest; use App\Shared\DtoFactory\PaginationDtoFactory; -use App\Shared\Error\NonValidUuidError; use App\Shared\Error\NotFoundError; use App\Shared\Repository\KitchensRepository; use App\Shared\Repository\RestaurantsRepository; use App\Shared\Repository\RestaurantTypesRepository; -use App\Shared\Service\ValidateService; use JsonException; -class RestaurantPrepareRequestService implements ServiceInterface +class RestaurantPrepareRequestService { public function __construct( private readonly RestaurantsRepository $restaurants, @@ -33,41 +32,24 @@ class RestaurantPrepareRequestService implements ServiceInterface private readonly KitchenTypeDtoFactory $kitchenFactory, private readonly RestaurantListDtoFactory $listingFactory, private readonly RestaurantDetailElementDtoFactory $detailFactory, - private readonly ValidateService $validate, ) { } /** - * @param int $page - * @param int $limit - * @param array|null $filters - * @return AbstractListDto + * @param RestaurantListingRequest $request + * @return RestaurantListDto */ - public function bornListDto( - int $page, - int $limit, - ?array $filters - ): AbstractListDto { - $filters['restaurant_type_id'] = $filters[0]; - $filters['kitchen_id'] = $filters[1]; - if (!$this->validate->isValidUuid($filters['restaurant_type_id'])) { - $filters['news_category'] = null; - } - if (!$this->validate->isValidUuid($filters['kitchen_id'])) { - $filters['news_category'] = null; - } - $this->validate->correctPagination($page, $limit); - + public function bornListDto(RestaurantListingRequest $request): RestaurantListDto { $countOfRestaurants = $this ->restaurants ->getCountWithFilters( - $filters['restaurant_type_id'], - $filters['kitchen_id'] + $request->restaurant_type_id, + $request->kitchen_id, ); $pagination = $this->paginationFactory->create( - $page, - $limit, + $request->page, + $request->limit, $countOfRestaurants, ); @@ -78,8 +60,8 @@ class RestaurantPrepareRequestService implements ServiceInterface $list = $this->listFactory->createCollection( $this->restaurants->getWithFilters( - $filters['kitchen_id'], - $filters['restaurant_type_id'], + $request->kitchen_id, + $request->restaurant_type_id, $pagination->pageSize, $offset, ), @@ -99,19 +81,13 @@ class RestaurantPrepareRequestService implements ServiceInterface } /** - * @param string $detailId - * @return AbstractDetailElement + * @param RestaurantDetailRequest $request + * @return RestaurantDetailElementDto * @throws JsonException */ public - function bornDetailElement( - string $detailId - ): AbstractDetailElement { - if (!$this->validate->isValidUuid($detailId)) { - throw new NonValidUuidError(); - } - - $restaurant = $this->restaurants->find($detailId); + function bornDetailElement(RestaurantDetailRequest $request): RestaurantDetailElementDto { + $restaurant = $this->restaurants->find($request->detailId); if ($restaurant === null) { throw new NotFoundError('Restaurant not found'); diff --git a/app/src/Restaurants/UseCase/RestaurantGetDetailUseCase.php b/app/src/Restaurants/UseCase/RestaurantGetDetailUseCase.php new file mode 100644 index 0000000..e369415 --- /dev/null +++ b/app/src/Restaurants/UseCase/RestaurantGetDetailUseCase.php @@ -0,0 +1,34 @@ +responsePrepareService->bornDetailElement($request) + ); + } catch (NotFoundError $error) { + $errorDto = $this->errorFactory->create($error); + return new JsonResponse($errorDto, $errorDto->status); + } catch (Throwable) { + return new JsonResponse([], Response::HTTP_INTERNAL_SERVER_ERROR); + } + } +} \ No newline at end of file diff --git a/app/src/Restaurants/UseCase/RestaurantGetListingUseCase.php b/app/src/Restaurants/UseCase/RestaurantGetListingUseCase.php new file mode 100644 index 0000000..2b34a03 --- /dev/null +++ b/app/src/Restaurants/UseCase/RestaurantGetListingUseCase.php @@ -0,0 +1,27 @@ +prepareRequestService->bornListDto($request) + ); + } catch (Throwable) { + return new JsonResponse([], Response::HTTP_INTERNAL_SERVER_ERROR); + } + } +} diff --git a/app/src/Shared/Abstraction/AbstractController.php b/app/src/Shared/Abstraction/AbstractController.php deleted file mode 100644 index cfd97b4..0000000 --- a/app/src/Shared/Abstraction/AbstractController.php +++ /dev/null @@ -1,53 +0,0 @@ -|null $filters - * @return JsonResponse - */ - protected function handleListing( - ServiceInterface $service, - int $page = 1, - int $limit = 12, - ?string ...$filters, - ): JsonResponse { - try { - return new JsonResponse($service->bornListDto($page, $limit, $filters)); - } catch (Throwable) { - return new JsonResponse([], Response::HTTP_INTERNAL_SERVER_ERROR); - } - } - - protected function handleDetailElement( - ServiceInterface $service, - string $detailId, - ): JsonResponse { - try { - return new JsonResponse($service->bornDetailElement($detailId)); - } catch (BaseError $error) { - $errorDto = $this->errorFactory->create($error); - - return new JsonResponse($errorDto, $errorDto->status); - } catch (Throwable) { - return new JsonResponse([], Response::HTTP_INTERNAL_SERVER_ERROR); - } - } -} diff --git a/app/src/Shared/Abstraction/AbstractDetailElement.php b/app/src/Shared/Abstraction/AbstractDetailElement.php deleted file mode 100644 index 6386d81..0000000 --- a/app/src/Shared/Abstraction/AbstractDetailElement.php +++ /dev/null @@ -1,9 +0,0 @@ - */ - public ListingCollection $list; -} diff --git a/app/src/Shared/Abstraction/AbstractListingElementDto.php b/app/src/Shared/Abstraction/AbstractListingElementDto.php deleted file mode 100644 index 9c676fd..0000000 --- a/app/src/Shared/Abstraction/AbstractListingElementDto.php +++ /dev/null @@ -1,10 +0,0 @@ -|null $filters - * @return AbstractListDto - */ - public function bornListDto( - int $page, - int $limit, - ?array $filters - ): AbstractListDto; - - public function bornDetailElement(string $detailId): AbstractDetailElement; -} - diff --git a/app/src/Shared/DtoFactory/PaginationDtoFactory.php b/app/src/Shared/DtoFactory/PaginationDtoFactory.php index 7973036..f3787bd 100644 --- a/app/src/Shared/DtoFactory/PaginationDtoFactory.php +++ b/app/src/Shared/DtoFactory/PaginationDtoFactory.php @@ -8,10 +8,9 @@ class PaginationDtoFactory { public function create(int $page, int $limit, int $total): PaginationDto { - $pageSize = $limit > 0 ? $limit : 12; - $pages = ceil($total / $pageSize); + $pages = ceil($total / $limit); $page = ($page > 1) && ($page <= $pages) ? $page : 1; - return new PaginationDto($page, $pages, $pageSize); + return new PaginationDto($page, $pages, $limit); } } diff --git a/app/src/Shared/Service/ValidateService.php b/app/src/Shared/Service/ValidateService.php deleted file mode 100644 index d424d6f..0000000 --- a/app/src/Shared/Service/ValidateService.php +++ /dev/null @@ -1,25 +0,0 @@ -= 1; + } + + public function isLimitValid(int $limit): bool + { + return $limit >= 1; + } +} -- GitLab