From bd0a9c0a8ec128bc79cb4852a71dbcff99a6ee65 Mon Sep 17 00:00:00 2001 From: AlexP Date: Fri, 17 May 2024 04:36:10 +0500 Subject: [PATCH] STA-960 | small fixes --- app/config/services.yaml | 14 +- .../Collection/NewsCategoryCollection.php | 16 +++ app/src/News/Controller/NewsController.php | 49 +++---- app/src/News/Dto/NewsCategoryDto.php | 9 +- app/src/News/Dto/NewsDetailElementDto.php | 21 +-- app/src/News/Dto/NewsFilterVariantsDto.php | 12 +- app/src/News/Dto/NewsListDto.php | 17 ++- app/src/News/Dto/NewsListingElementDto.php | 16 +-- .../DtoFactory/NewsCategoryDtoFactory.php | 9 +- .../NewsDetailElementDtoFactory.php | 5 +- .../NewsFilterVariantsDtoFactory.php | 6 +- .../News/DtoFactory/NewsListDtoFactory.php | 7 +- .../NewsListingElementDtoFactory.php | 20 +-- app/src/News/Request/NewsListingRequest.php | 31 ----- app/src/News/Service/NewsDetailService.php | 31 ----- app/src/News/Service/NewsListingService.php | 57 -------- app/src/News/Service/NewsMainNewsService.php | 22 ---- .../Service/NewsPrepareResponseService.php | 114 ++++++++++++++++ app/src/News/Service/NewsSearchService.php | 22 ---- .../Collection/KitchenCollection.php | 16 +++ .../Collection/RestaurantTypeCollection.php | 16 +++ .../Restaurants/Collection/TagCollection.php | 16 +++ .../Controller/RestaurantsController.php | 35 +++-- app/src/Restaurants/Dto/KitchenTypeDto.php | 6 +- .../Dto/RestaurantDetailElementDto.php | 47 +++---- .../Dto/RestaurantFilterVariantsDto.php | 15 +-- app/src/Restaurants/Dto/RestaurantListDto.php | 12 +- .../Dto/RestaurantListingElementDto.php | 10 +- app/src/Restaurants/Dto/RestaurantTypeDto.php | 6 +- app/src/Restaurants/Dto/TagDto.php | 10 +- .../DtoFactory/KitchenTypeDtoFactory.php | 18 ++- .../RestaurantDetailElementDtoFactory.php | 27 ++-- .../RestaurantFilterVariantsDtoFactory.php | 7 +- .../DtoFactory/RestaurantListDtoFactory.php | 6 +- .../RestaurantListingElementDtoFactory.php | 11 +- .../DtoFactory/RestaurantTypeDtoFactory.php | 9 +- .../Restaurants/DtoFactory/TagDtoFactory.php | 11 +- .../Request/RestaurantListingRequest.php | 36 ------ .../Service/RestaurantDetailService.php | 34 ----- .../Service/RestaurantListingService.php | 71 ---------- .../RestaurantPrepareRequestService.php | 122 ++++++++++++++++++ .../Shared/Abstraction/AbstractController.php | 52 +++++--- .../Abstraction/AbstractDetailElement.php | 9 ++ .../Shared/Abstraction/AbstractListDto.php | 14 ++ .../Abstraction/AbstractListingElementDto.php | 10 ++ .../Shared/Abstraction/AbstractRequest.php | 75 ----------- app/src/Shared/Abstraction/DtoInterface.php | 5 - .../Shared/Abstraction/EntityInterface.php | 5 - .../Shared/Abstraction/ServiceInterface.php | 15 ++- .../{Dto => Collection}/DtoCollection.php | 4 +- app/src/Shared/Collection/FileCollection.php | 15 +++ .../Shared/Collection/ListingCollection.php | 15 +++ .../Shared/Collection/StringCollection.php | 13 ++ app/src/Shared/Dto/ErrorDto.php | 9 +- app/src/Shared/Dto/FileDto.php | 12 +- app/src/Shared/Dto/PaginationDto.php | 7 +- app/src/Shared/Dto/ValidateErrorDto.php | 9 +- app/src/Shared/DtoFactory/ErrorDtoFactory.php | 10 +- app/src/Shared/DtoFactory/FileDtoFactory.php | 11 +- .../DtoFactory/PaginationDtoFactory.php | 2 +- .../DtoFactory/ValidateErrorDtoFactory.php | 5 +- app/src/Shared/Entity/File.php | 24 ++-- app/src/Shared/Entity/Kitchens.php | 3 +- app/src/Shared/Entity/News.php | 4 - app/src/Shared/Entity/NewsCategories.php | 5 +- app/src/Shared/Entity/RestaurantTypes.php | 3 +- app/src/Shared/Entity/Restaurants.php | 24 ++-- app/src/Shared/Error/BaseError.php | 3 +- app/src/Shared/Error/ErrorCode.php | 2 + app/src/Shared/Error/NonValidUuidError.php | 16 +++ app/src/Shared/Error/NotFoundError.php | 7 +- app/src/Shared/Request/DetailRequest.php | 26 ---- app/src/Shared/Request/EmptyRequest.php | 12 -- app/src/Shared/Service/ValidateService.php | 25 ++++ docker-compose.yml => compose.yaml | 0 75 files changed, 723 insertions(+), 747 deletions(-) create mode 100644 app/src/News/Collection/NewsCategoryCollection.php delete mode 100644 app/src/News/Request/NewsListingRequest.php delete mode 100644 app/src/News/Service/NewsDetailService.php delete mode 100644 app/src/News/Service/NewsListingService.php delete mode 100644 app/src/News/Service/NewsMainNewsService.php create mode 100644 app/src/News/Service/NewsPrepareResponseService.php delete mode 100644 app/src/News/Service/NewsSearchService.php create mode 100644 app/src/Restaurants/Collection/KitchenCollection.php create mode 100644 app/src/Restaurants/Collection/RestaurantTypeCollection.php create mode 100644 app/src/Restaurants/Collection/TagCollection.php delete mode 100644 app/src/Restaurants/Request/RestaurantListingRequest.php delete mode 100644 app/src/Restaurants/Service/RestaurantDetailService.php delete mode 100644 app/src/Restaurants/Service/RestaurantListingService.php create mode 100644 app/src/Restaurants/Service/RestaurantPrepareRequestService.php create mode 100644 app/src/Shared/Abstraction/AbstractDetailElement.php create mode 100644 app/src/Shared/Abstraction/AbstractListDto.php create mode 100644 app/src/Shared/Abstraction/AbstractListingElementDto.php delete mode 100644 app/src/Shared/Abstraction/AbstractRequest.php delete mode 100644 app/src/Shared/Abstraction/DtoInterface.php delete mode 100644 app/src/Shared/Abstraction/EntityInterface.php rename app/src/Shared/{Dto => Collection}/DtoCollection.php (87%) create mode 100644 app/src/Shared/Collection/FileCollection.php create mode 100644 app/src/Shared/Collection/ListingCollection.php create mode 100644 app/src/Shared/Collection/StringCollection.php create mode 100644 app/src/Shared/Error/NonValidUuidError.php delete mode 100644 app/src/Shared/Request/DetailRequest.php delete mode 100644 app/src/Shared/Request/EmptyRequest.php create mode 100644 app/src/Shared/Service/ValidateService.php rename docker-compose.yml => compose.yaml (100%) diff --git a/app/config/services.yaml b/app/config/services.yaml index 55758d4..851a662 100644 --- a/app/config/services.yaml +++ b/app/config/services.yaml @@ -14,11 +14,9 @@ services: # add more service definitions when explicit configuration is needed # please note that last definitions always *replace* previous ones - App\Restaurants\: - resource: '../src/Restaurants/' - - App\News\: - resource: '../src/News/' - - App\Shared\: - resource: '../src/Shared/' \ No newline at end of file + App\: + resource: '../src/' + exclude: + - '../src/DependencyInjection/' + - '../src/Entity/' + - '../src/Kernel.php' diff --git a/app/src/News/Collection/NewsCategoryCollection.php b/app/src/News/Collection/NewsCategoryCollection.php new file mode 100644 index 0000000..cd3b77d --- /dev/null +++ b/app/src/News/Collection/NewsCategoryCollection.php @@ -0,0 +1,16 @@ +collectionType, $data); + } +} diff --git a/app/src/News/Controller/NewsController.php b/app/src/News/Controller/NewsController.php index 286ff07..98694b5 100644 --- a/app/src/News/Controller/NewsController.php +++ b/app/src/News/Controller/NewsController.php @@ -2,16 +2,11 @@ namespace App\News\Controller; -use App\News\Request\NewsListingRequest; -use App\News\Service\NewsDetailService; -use App\News\Service\NewsListingService; -use App\News\Service\NewsMainNewsService; -use App\News\Service\NewsSearchService; +use App\News\Service\NewsPrepareResponseService; use App\Shared\Abstraction\AbstractController; use App\Shared\DtoFactory\ErrorDtoFactory; -use App\Shared\Request\DetailRequest; -use App\Shared\Request\EmptyRequest; use Symfony\Component\HttpFoundation\JsonResponse; +use Symfony\Component\HttpKernel\Attribute\MapQueryParameter; use Symfony\Component\Routing\Attribute\Route; #[Route('/api/v1/news')] @@ -19,36 +14,46 @@ class NewsController extends AbstractController { public function __construct( ErrorDtoFactory $errorFactory, - - private readonly NewsDetailService $detailService, - private readonly NewsListingService $listingService, - private readonly NewsMainNewsService $mainNewsService, - private readonly NewsSearchService $searchService, + private readonly NewsPrepareResponseService $responsePrepareService, ) { parent::__construct($errorFactory); } #[Route('/', name: 'news', methods: ['GET'])] - public function news(NewsListingRequest $request): JsonResponse - { - return $this->handle($this->listingService, $request); + 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, + ); } #[Route('/mainNews', name: 'mainNews', methods: ['GET'])] - public function mainNews(EmptyRequest $request): JsonResponse + public function mainNews(): JsonResponse { - return $this->handle($this->mainNewsService, $request); + return new JsonResponse($this->responsePrepareService->bornMainNews()); } #[Route('/search', name: 'searchNews', methods: ['GET'])] - public function search(EmptyRequest $request): JsonResponse + public function search(): JsonResponse { - return $this->handle($this->searchService, $request); + return new JsonResponse( + $this->responsePrepareService->bornDetailMainNews() + ); } #[Route('/{detailId}', name: 'oneNews', methods: ['GET'])] - public function oneNews(DetailRequest $request): JsonResponse - { - return $this->handle($this->detailService, $request); + public function oneNews( + #[MapQueryParameter] string $detailId = null + ): JsonResponse { + return $this->handleDetailElement( + $this->responsePrepareService, + $detailId, + ); } } diff --git a/app/src/News/Dto/NewsCategoryDto.php b/app/src/News/Dto/NewsCategoryDto.php index 21917d9..010a7e7 100644 --- a/app/src/News/Dto/NewsCategoryDto.php +++ b/app/src/News/Dto/NewsCategoryDto.php @@ -2,15 +2,12 @@ namespace App\News\Dto; -use App\Shared\Abstraction\DtoInterface; - -class NewsCategoryDto implements DtoInterface +class NewsCategoryDto { public function __construct( public string $id, - public string $name, - public string $code, - ) {} + ) { + } } diff --git a/app/src/News/Dto/NewsDetailElementDto.php b/app/src/News/Dto/NewsDetailElementDto.php index 634effa..530f5e0 100644 --- a/app/src/News/Dto/NewsDetailElementDto.php +++ b/app/src/News/Dto/NewsDetailElementDto.php @@ -2,23 +2,26 @@ namespace App\News\Dto; -use App\Shared\Abstraction\DtoInterface; +use App\Shared\Abstraction\AbstractDetailElement; use App\Shared\Dto\FileDto; -class NewsDetailElementDto implements DtoInterface +class NewsDetailElementDto extends AbstractDetailElement { + /** + * @param string $id + * @param string $name + * @param string $description + * @param string $createAt + * @param string|null $text + * @param FileDto|null $image Преаью + */ public function __construct( public string $id, - public string $name, - public string $description, - public string $createAt, - public ?string $text = null, - - /** @var FileDto Преаью */ public ?FileDto $image = null, - ) {} + ) { + } } diff --git a/app/src/News/Dto/NewsFilterVariantsDto.php b/app/src/News/Dto/NewsFilterVariantsDto.php index b3f9f5c..0544560 100644 --- a/app/src/News/Dto/NewsFilterVariantsDto.php +++ b/app/src/News/Dto/NewsFilterVariantsDto.php @@ -2,16 +2,16 @@ namespace App\News\Dto; -use App\Shared\Abstraction\DtoInterface; -use App\Shared\Dto\DtoCollection; +use App\News\Collection\NewsCategoryCollection; -class NewsFilterVariantsDto implements DtoInterface +class NewsFilterVariantsDto { /** - * @param DtoCollection $category + * @param NewsCategoryCollection $category */ public function __construct( - public DtoCollection $category, - ) {} + public NewsCategoryCollection $category, + ) { + } } diff --git a/app/src/News/Dto/NewsListDto.php b/app/src/News/Dto/NewsListDto.php index d4ffffd..63da8fa 100644 --- a/app/src/News/Dto/NewsListDto.php +++ b/app/src/News/Dto/NewsListDto.php @@ -2,22 +2,21 @@ namespace App\News\Dto; -use App\Shared\Abstraction\DtoInterface; -use App\Shared\Dto\DtoCollection; +use App\Shared\Abstraction\AbstractListDto; +use App\Shared\Collection\ListingCollection; use App\Shared\Dto\PaginationDto; -class NewsListDto implements DtoInterface +class NewsListDto extends AbstractListDto { /** * @param PaginationDto $pagination - * @param DtoCollection $list + * @param ListingCollection $list * @param NewsFilterVariantsDto $filterVariants */ public function __construct( - public PaginationDto $pagination, - - public DtoCollection $list, - + public PaginationDto $pagination, + public ListingCollection $list, public NewsFilterVariantsDto $filterVariants, - ) {} + ) { + } } diff --git a/app/src/News/Dto/NewsListingElementDto.php b/app/src/News/Dto/NewsListingElementDto.php index dd6c610..a291ea1 100644 --- a/app/src/News/Dto/NewsListingElementDto.php +++ b/app/src/News/Dto/NewsListingElementDto.php @@ -2,30 +2,26 @@ namespace App\News\Dto; -use App\Shared\Abstraction\DtoInterface; +use App\Shared\Abstraction\AbstractListingElementDto; use App\Shared\Dto\FileDto; -class NewsListingElementDto implements DtoInterface +class NewsListingElementDto extends AbstractListingElementDto { /** * @param string $id * @param string $name - * @param string $create_at - * @param string $detail_link + * @param string $createAt + * @param string $detailLink * @param string|null $description * @param FileDto|null $image Превью */ public function __construct( public string $id, - public string $name, - public string $createAt, - public string $detailLink, - public ?string $description = null, - public ?FileDto $image = null, - ) {} + ) { + } } diff --git a/app/src/News/DtoFactory/NewsCategoryDtoFactory.php b/app/src/News/DtoFactory/NewsCategoryDtoFactory.php index f5726a8..b9388fc 100644 --- a/app/src/News/DtoFactory/NewsCategoryDtoFactory.php +++ b/app/src/News/DtoFactory/NewsCategoryDtoFactory.php @@ -2,8 +2,8 @@ namespace App\News\DtoFactory; +use App\News\Collection\NewsCategoryCollection; use App\News\Dto\NewsCategoryDto; -use App\Shared\Dto\DtoCollection; use App\Shared\Entity\NewsCategories; use Ramsey\Collection\Collection; @@ -20,16 +20,15 @@ class NewsCategoryDtoFactory /** * @param Collection $categories - * @return DtoCollection + * @return NewsCategoryCollection */ - public function createCollection(Collection $categories): DtoCollection + public function createCollection(Collection $categories): NewsCategoryCollection { $categoriesDto = $categories->map(function(NewsCategories $category) { return $this->create($category); }); - return new DtoCollection( - NewsCategoryDto::class, + return new NewsCategoryCollection( $categoriesDto->toArray() ); } diff --git a/app/src/News/DtoFactory/NewsDetailElementDtoFactory.php b/app/src/News/DtoFactory/NewsDetailElementDtoFactory.php index c4a82b0..ad29868 100644 --- a/app/src/News/DtoFactory/NewsDetailElementDtoFactory.php +++ b/app/src/News/DtoFactory/NewsDetailElementDtoFactory.php @@ -10,7 +10,8 @@ class NewsDetailElementDtoFactory { public function __construct( private readonly FileDtoFactory $fileFactory - ) {} + ) { + } public function create(News $news): NewsDetailElementDto { @@ -18,7 +19,7 @@ class NewsDetailElementDtoFactory id: $news->getId(), name: $news->getName(), description: $news->getPreviewText(), - createAt: $news->getCreatedAt()->format('Y-m-d H:i:s'), + createAt: $news->getCreatedAt()?->format('d.m.Y'), text: $news->getDetailText(), image: $news->getPreviewImage() !== null ? $this->fileFactory->create($news->getPreviewImage()) diff --git a/app/src/News/DtoFactory/NewsFilterVariantsDtoFactory.php b/app/src/News/DtoFactory/NewsFilterVariantsDtoFactory.php index 80f54fe..ab0c5ff 100644 --- a/app/src/News/DtoFactory/NewsFilterVariantsDtoFactory.php +++ b/app/src/News/DtoFactory/NewsFilterVariantsDtoFactory.php @@ -2,17 +2,17 @@ namespace App\News\DtoFactory; +use App\News\Collection\NewsCategoryCollection; use App\News\Dto\NewsCategoryDto; use App\News\Dto\NewsFilterVariantsDto; -use App\Shared\Dto\DtoCollection; class NewsFilterVariantsDtoFactory { /** - * @param DtoCollection $categories + * @param NewsCategoryCollection $categories * @return NewsFilterVariantsDto */ - public function create(DtoCollection $categories): NewsFilterVariantsDto + public function create(NewsCategoryCollection $categories): NewsFilterVariantsDto { return new NewsFilterVariantsDto( $categories diff --git a/app/src/News/DtoFactory/NewsListDtoFactory.php b/app/src/News/DtoFactory/NewsListDtoFactory.php index 40f714f..25613a7 100644 --- a/app/src/News/DtoFactory/NewsListDtoFactory.php +++ b/app/src/News/DtoFactory/NewsListDtoFactory.php @@ -4,17 +4,16 @@ namespace App\News\DtoFactory; use App\News\Dto\NewsFilterVariantsDto; use App\News\Dto\NewsListDto; -use App\Shared\Dto\DtoCollection; +use App\Shared\Collection\ListingCollection; use App\Shared\Dto\PaginationDto; class NewsListDtoFactory { public function create( PaginationDto $pagination, - DtoCollection $list, + ListingCollection $list, NewsFilterVariantsDto $filters, - ): NewsListDto - { + ): NewsListDto { return new NewsListDto( pagination: $pagination, list: $list, diff --git a/app/src/News/DtoFactory/NewsListingElementDtoFactory.php b/app/src/News/DtoFactory/NewsListingElementDtoFactory.php index 668b6ac..11d4412 100644 --- a/app/src/News/DtoFactory/NewsListingElementDtoFactory.php +++ b/app/src/News/DtoFactory/NewsListingElementDtoFactory.php @@ -3,7 +3,7 @@ namespace App\News\DtoFactory; use App\News\Dto\NewsListingElementDto; -use App\Shared\Dto\DtoCollection; +use App\Shared\Collection\ListingCollection; use App\Shared\DtoFactory\FileDtoFactory; use App\Shared\Entity\News; use Ramsey\Collection\Collection; @@ -12,34 +12,34 @@ class NewsListingElementDtoFactory { public function __construct( private readonly FileDtoFactory $fileFactory, - ) {} + ) { + } public function create(News $news): NewsListingElementDto { return new NewsListingElementDto( id: $news->getId(), name: $news->getName(), - createAt: $news->getCreatedAt()->format('Y-m-d H:i:s'), + createAt: $news->getCreatedAt()?->format('d.m.Y'), detailLink: "api/v1/news/" . $news->getId(), description: $news->getPreviewText(), image: $news->getPreviewImage() !== null ? $this->fileFactory->create($news->getPreviewImage()) - :null, + : null, ); } /** * @param Collection $news - * @return DtoCollection + * @return ListingCollection */ - public function createCollection(Collection $news): DtoCollection + public function createCollection(Collection $news): ListingCollection { - return new DtoCollection( - NewsListingElementDto::class, - $news->map(function(News $oneNews) { + return new ListingCollection( + $news->map(function (News $oneNews) { return $this->create($oneNews); }) - ->toArray() + ->toArray() ); } } diff --git a/app/src/News/Request/NewsListingRequest.php b/app/src/News/Request/NewsListingRequest.php deleted file mode 100644 index 31d99f6..0000000 --- a/app/src/News/Request/NewsListingRequest.php +++ /dev/null @@ -1,31 +0,0 @@ -getRequest(); - foreach ($request->query->getIterator() as $property => $value) { - if (property_exists($this, $property)) { - $this->{$property} = $value; - } - } - } -} diff --git a/app/src/News/Service/NewsDetailService.php b/app/src/News/Service/NewsDetailService.php deleted file mode 100644 index 8397215..0000000 --- a/app/src/News/Service/NewsDetailService.php +++ /dev/null @@ -1,31 +0,0 @@ -news->find($request->detailId); - - return $news !== null - ? $this->dtoFactory->create($news) - : throw new NotFoundError('News not found'); - } -} diff --git a/app/src/News/Service/NewsListingService.php b/app/src/News/Service/NewsListingService.php deleted file mode 100644 index fd32d9f..0000000 --- a/app/src/News/Service/NewsListingService.php +++ /dev/null @@ -1,57 +0,0 @@ -news->getCountWithFilters($request->news_category); - - $pagination = $this->paginationFactory->create( - $request->page, - $request->limit, - $countOfNews - ); - - $offset = min($pagination->pageSize, $countOfNews) * ($pagination->currentPage - 1); - - $list = $this->listFactory->createCollection( - $this->news->getWithFilters($request->news_category, $request->limit, $offset) - ); - - $categoriesDto = $this->categoryFactory->createCollection( - $this->newsCategories->getAll() - ); - - $filters = $this->filterFactory->create($categoriesDto); - - return $this->listingFactory->create( - $pagination, - $list, - $filters - ); - } -} diff --git a/app/src/News/Service/NewsMainNewsService.php b/app/src/News/Service/NewsMainNewsService.php deleted file mode 100644 index 784633d..0000000 --- a/app/src/News/Service/NewsMainNewsService.php +++ /dev/null @@ -1,22 +0,0 @@ -dtoFactory->create($this->news->getMainNews()); - } -} diff --git a/app/src/News/Service/NewsPrepareResponseService.php b/app/src/News/Service/NewsPrepareResponseService.php new file mode 100644 index 0000000..dbdd17c --- /dev/null +++ b/app/src/News/Service/NewsPrepareResponseService.php @@ -0,0 +1,114 @@ +|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); + + $countOfNews = $this->news->getCountWithFilters( + $filters['news_category'] + ); + + $pagination = $this->paginationFactory->create( + $page, + $limit, + $countOfNews + ); + + $offset = min( + $pagination->pageSize, + $countOfNews + ) * ($pagination->currentPage - 1); + + $list = $this->listFactory->createCollection( + $this->news->getWithFilters( + $filters['news_category'], + $limit, + $offset + ) + ); + + $filters = $this->filterFactory->create( + $this->categoryFactory->createCollection( + $this->newsCategories->getAll() + ) + ); + + return $this->listingFactory->create( + $pagination, + $list, + $filters + ); + } + + public function bornMainNews(): NewsListingElementDto + { + return $this->listFactory->create($this->news->getMainNews()); + } + + public function bornDetailMainNews(): NewsDetailElementDto + { + 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); + + if ($news === null) { + throw new NotFoundError('News not found'); + } + + return $this->detailElementFactory->create($news); + } +} diff --git a/app/src/News/Service/NewsSearchService.php b/app/src/News/Service/NewsSearchService.php deleted file mode 100644 index 391d3c9..0000000 --- a/app/src/News/Service/NewsSearchService.php +++ /dev/null @@ -1,22 +0,0 @@ -dtoFactory->create($this->news->getMainNews()); - } -} diff --git a/app/src/Restaurants/Collection/KitchenCollection.php b/app/src/Restaurants/Collection/KitchenCollection.php new file mode 100644 index 0000000..a2eb60a --- /dev/null +++ b/app/src/Restaurants/Collection/KitchenCollection.php @@ -0,0 +1,16 @@ +collectionType, $data); + } +} diff --git a/app/src/Restaurants/Collection/RestaurantTypeCollection.php b/app/src/Restaurants/Collection/RestaurantTypeCollection.php new file mode 100644 index 0000000..8f4091f --- /dev/null +++ b/app/src/Restaurants/Collection/RestaurantTypeCollection.php @@ -0,0 +1,16 @@ +collectionType, $data); + } +} diff --git a/app/src/Restaurants/Collection/TagCollection.php b/app/src/Restaurants/Collection/TagCollection.php new file mode 100644 index 0000000..dec7c1c --- /dev/null +++ b/app/src/Restaurants/Collection/TagCollection.php @@ -0,0 +1,16 @@ +collectionType, $data); + } +} diff --git a/app/src/Restaurants/Controller/RestaurantsController.php b/app/src/Restaurants/Controller/RestaurantsController.php index 5893ecb..eea4f2d 100644 --- a/app/src/Restaurants/Controller/RestaurantsController.php +++ b/app/src/Restaurants/Controller/RestaurantsController.php @@ -2,13 +2,11 @@ namespace App\Restaurants\Controller; -use App\Restaurants\Request\RestaurantListingRequest; -use App\Restaurants\Service\RestaurantDetailService; -use App\Restaurants\Service\RestaurantListingService; +use App\Restaurants\Service\RestaurantPrepareRequestService; use App\Shared\Abstraction\AbstractController; use App\Shared\DtoFactory\ErrorDtoFactory; -use App\Shared\Request\DetailRequest; use Symfony\Component\HttpFoundation\JsonResponse; +use Symfony\Component\HttpKernel\Attribute\MapQueryParameter; use Symfony\Component\Routing\Attribute\Route; #[Route('/api/v1/restaurants')] @@ -16,22 +14,33 @@ class RestaurantsController extends AbstractController { public function __construct( ErrorDtoFactory $errorFactory, - - private readonly RestaurantListingService $listingService, - private readonly RestaurantDetailService $detailService, + private readonly RestaurantPrepareRequestService $responsePrepareService, ) { parent::__construct($errorFactory); } #[Route('/', name: 'restaurants', methods: ['GET'])] - public function restaurants(RestaurantListingRequest $request): JsonResponse - { - return $this->handle($this->listingService, $request); + 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, + ); } #[Route('/{detailId}', name: 'restaurant', methods: ['GET'])] - public function restaurant(DetailRequest $request): JsonResponse - { - return $this->handle($this->detailService, $request); + public function restaurant(#[MapQueryParameter] string $detailId = null + ): JsonResponse { + return $this->handleDetailElement( + $this->responsePrepareService, + $detailId, + ); } } diff --git a/app/src/Restaurants/Dto/KitchenTypeDto.php b/app/src/Restaurants/Dto/KitchenTypeDto.php index e326132..da4c576 100644 --- a/app/src/Restaurants/Dto/KitchenTypeDto.php +++ b/app/src/Restaurants/Dto/KitchenTypeDto.php @@ -2,15 +2,11 @@ namespace App\Restaurants\Dto; -use App\Shared\Abstraction\DtoInterface; - -class KitchenTypeDto implements DtoInterface +class KitchenTypeDto { public function __construct( public string $id, - public string $name, - public string $code, ) {} } diff --git a/app/src/Restaurants/Dto/RestaurantDetailElementDto.php b/app/src/Restaurants/Dto/RestaurantDetailElementDto.php index 41139b8..bf4d83f 100644 --- a/app/src/Restaurants/Dto/RestaurantDetailElementDto.php +++ b/app/src/Restaurants/Dto/RestaurantDetailElementDto.php @@ -2,11 +2,14 @@ namespace App\Restaurants\Dto; -use App\Shared\Abstraction\DtoInterface; -use App\Shared\Dto\DtoCollection; +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 implements DtoInterface +class RestaurantDetailElementDto extends AbstractDetailElement { /** * @param string $id @@ -16,44 +19,30 @@ class RestaurantDetailElementDto implements DtoInterface * @param RestaurantTypeDto $type * @param string|null $check * @param string|null $check_info - * @param DtoCollection|null $kitchen - * @param DtoCollection|null $phone - * @param DtoCollection|null $email - * @param DtoCollection|null $address - * @param DtoCollection|null $tags + * @param KitchenCollection|null $kitchen + * @param StringCollection|null $phone + * @param StringCollection|null $email + * @param StringCollection|null $address + * @param TagCollection|null $tags * @param string|null $site * @param FileDto|null $image Превью - * @param DtoCollection|null $gallery + * @param FileCollection|null $gallery */ public function __construct( public string $id, - public string $name, - public string $code, - public string $coordinates, - public RestaurantTypeDto $type, - public ?string $check = null, - public ?string $check_info = null, - - public ?DtoCollection $kitchen = null, - - public ?DtoCollection $phone = null, - - public ?DtoCollection $email = null, - - public ?DtoCollection $address = null, - - public ?DtoCollection $tags = null, - + public ?KitchenCollection $kitchen = null, + public ?StringCollection $phone = null, + public ?StringCollection $email = null, + public ?StringCollection $address = null, + public ?TagCollection $tags = null, public ?string $site = null, - public ?FileDto $image = null, - - public ?DtoCollection $gallery = null, + public ?FileCollection $gallery = null, ) {} } diff --git a/app/src/Restaurants/Dto/RestaurantFilterVariantsDto.php b/app/src/Restaurants/Dto/RestaurantFilterVariantsDto.php index e4a15c1..d0e0e65 100644 --- a/app/src/Restaurants/Dto/RestaurantFilterVariantsDto.php +++ b/app/src/Restaurants/Dto/RestaurantFilterVariantsDto.php @@ -2,18 +2,17 @@ namespace App\Restaurants\Dto; -use App\Shared\Abstraction\DtoInterface; -use App\Shared\Dto\DtoCollection; +use App\Restaurants\Collection\KitchenCollection; +use App\Restaurants\Collection\RestaurantTypeCollection; -class RestaurantFilterVariantsDto implements DtoInterface +class RestaurantFilterVariantsDto { /** - * @param DtoCollection $type - * @param DtoCollection $kitchen + * @param RestaurantTypeCollection $type + * @param KitchenCollection $kitchen */ public function __construct( - public DtoCollection $type, - - public DtoCollection $kitchen, + public RestaurantTypeCollection $type, + public KitchenCollection $kitchen, ) {} } diff --git a/app/src/Restaurants/Dto/RestaurantListDto.php b/app/src/Restaurants/Dto/RestaurantListDto.php index 9d7f022..79354d8 100644 --- a/app/src/Restaurants/Dto/RestaurantListDto.php +++ b/app/src/Restaurants/Dto/RestaurantListDto.php @@ -2,22 +2,20 @@ namespace App\Restaurants\Dto; -use App\Shared\Abstraction\DtoInterface; -use App\Shared\Dto\DtoCollection; +use App\Shared\Abstraction\AbstractListDto; +use App\Shared\Collection\ListingCollection; use App\Shared\Dto\PaginationDto; -class RestaurantListDto implements DtoInterface +class RestaurantListDto extends AbstractListDto { /** * @param PaginationDto $pagination - * @param DtoCollection $list + * @param ListingCollection $list * @param RestaurantFilterVariantsDto $filterVariants */ public function __construct( public PaginationDto $pagination, - - public DtoCollection $list, - + public ListingCollection $list, public RestaurantFilterVariantsDto $filterVariants, ) {} } diff --git a/app/src/Restaurants/Dto/RestaurantListingElementDto.php b/app/src/Restaurants/Dto/RestaurantListingElementDto.php index b9b212e..4d7f3dc 100644 --- a/app/src/Restaurants/Dto/RestaurantListingElementDto.php +++ b/app/src/Restaurants/Dto/RestaurantListingElementDto.php @@ -2,24 +2,18 @@ namespace App\Restaurants\Dto; -use App\Shared\Abstraction\DtoInterface; +use App\Shared\Abstraction\AbstractListingElementDto; use App\Shared\Dto\FileDto; -class RestaurantListingElementDto implements DtoInterface +class RestaurantListingElementDto extends AbstractListingElementDto { public function __construct( public string $id, - public string $name, - public string $code, - public RestaurantTypeDto $type, - public string $detailLink, - public ?string $check = null, - public ?FileDto $image = null, ) {} } diff --git a/app/src/Restaurants/Dto/RestaurantTypeDto.php b/app/src/Restaurants/Dto/RestaurantTypeDto.php index c6747da..c8a2b95 100644 --- a/app/src/Restaurants/Dto/RestaurantTypeDto.php +++ b/app/src/Restaurants/Dto/RestaurantTypeDto.php @@ -2,15 +2,11 @@ namespace App\Restaurants\Dto; -use App\Shared\Abstraction\DtoInterface; - -class RestaurantTypeDto implements DtoInterface +class RestaurantTypeDto { public function __construct( public string $id, - public string $name, - public string $code, ) {} } diff --git a/app/src/Restaurants/Dto/TagDto.php b/app/src/Restaurants/Dto/TagDto.php index eb8d11c..2a1369c 100644 --- a/app/src/Restaurants/Dto/TagDto.php +++ b/app/src/Restaurants/Dto/TagDto.php @@ -2,18 +2,16 @@ namespace App\Restaurants\Dto; -use App\Shared\Abstraction\DtoInterface; -use App\Shared\Dto\DtoCollection; +use App\Shared\Collection\StringCollection; -class TagDto implements DtoInterface +class TagDto { /** * @param string $name - * @param DtoCollection $list + * @param StringCollection $list */ public function __construct( public string $name, - - public DtoCollection $list, + public StringCollection $list, ) {} } diff --git a/app/src/Restaurants/DtoFactory/KitchenTypeDtoFactory.php b/app/src/Restaurants/DtoFactory/KitchenTypeDtoFactory.php index 04c7260..7b0d391 100644 --- a/app/src/Restaurants/DtoFactory/KitchenTypeDtoFactory.php +++ b/app/src/Restaurants/DtoFactory/KitchenTypeDtoFactory.php @@ -2,11 +2,11 @@ namespace App\Restaurants\DtoFactory; +use App\Restaurants\Collection\KitchenCollection; use App\Restaurants\Dto\KitchenTypeDto; -use App\Shared\Dto\DtoCollection; use App\Shared\Entity\Kitchens; -use Ramsey\Collection\Collection as RamseyCollection; use Doctrine\Common\Collections\Collection as DoctrineCollection; +use Ramsey\Collection\Collection as RamseyCollection; class KitchenTypeDtoFactory { @@ -21,17 +21,15 @@ class KitchenTypeDtoFactory /** * @param DoctrineCollection|RamseyCollection $kitchens - * @return DtoCollection + * @return KitchenCollection */ - public function createCollection(DoctrineCollection|RamseyCollection $kitchens): DtoCollection - { - $kitchenCollection = $kitchens->map(function(Kitchens $kitchen) { + public function createCollection( + DoctrineCollection|RamseyCollection $kitchens + ): KitchenCollection { + $kitchenCollection = $kitchens->map(function (Kitchens $kitchen) { return $this->create($kitchen); }); - return new DtoCollection( - KitchenTypeDto::class, - $kitchenCollection->toArray() - ); + return new KitchenCollection($kitchenCollection->toArray()); } } diff --git a/app/src/Restaurants/DtoFactory/RestaurantDetailElementDtoFactory.php b/app/src/Restaurants/DtoFactory/RestaurantDetailElementDtoFactory.php index 343b892..39e8d67 100644 --- a/app/src/Restaurants/DtoFactory/RestaurantDetailElementDtoFactory.php +++ b/app/src/Restaurants/DtoFactory/RestaurantDetailElementDtoFactory.php @@ -3,9 +3,10 @@ namespace App\Restaurants\DtoFactory; use App\Restaurants\Dto\RestaurantDetailElementDto; -use App\Shared\Dto\DtoCollection; +use App\Shared\Collection\StringCollection; use App\Shared\DtoFactory\FileDtoFactory; use App\Shared\Entity\Restaurants; +use JsonException; class RestaurantDetailElementDtoFactory { @@ -14,7 +15,12 @@ class RestaurantDetailElementDtoFactory private readonly KitchenTypeDtoFactory $kitchenFactory, private readonly TagDtoFactory $tagFactory, private readonly FileDtoFactory $fileFactory, - ) {} + ) { + } + + /** + * @throws JsonException + */ public function create(Restaurants $restaurant): RestaurantDetailElementDto { return new RestaurantDetailElementDto( @@ -25,26 +31,29 @@ class RestaurantDetailElementDtoFactory type: $this->typeFactory->create($restaurant->getType()), check: $restaurant->getReceipt(), check_info: $restaurant->getReceiptInfo(), - kitchen: $this->kitchenFactory->createCollection($restaurant->getKitchens()), + kitchen: $this->kitchenFactory->createCollection( + $restaurant->getKitchens() + ), phone: $this->decodeJson($restaurant->getPhone()), email: $this->decodeJson($restaurant->getEmail()), address: $this->decodeJson($restaurant->getAddress()), tags: $this->tagFactory->createCollection($restaurant->getTags()), site: $restaurant->getSite(), image: $this->fileFactory->create($restaurant->getPreviewImage()), - gallery: $this->fileFactory->createCollection($restaurant->getGallery()), + gallery: $this->fileFactory->createCollection( + $restaurant->getGallery() + ), ); } /** - * @throws \JsonException + * @throws JsonException */ - private function decodeJson(string $json) + private function decodeJson(?string $json): ?StringCollection { return $json === null ? null - : new DtoCollection( - 'string', + : new StringCollection( json_decode( $json, true, @@ -53,4 +62,4 @@ class RestaurantDetailElementDtoFactory ), ); } -} \ No newline at end of file +} diff --git a/app/src/Restaurants/DtoFactory/RestaurantFilterVariantsDtoFactory.php b/app/src/Restaurants/DtoFactory/RestaurantFilterVariantsDtoFactory.php index 91624eb..74cabf7 100644 --- a/app/src/Restaurants/DtoFactory/RestaurantFilterVariantsDtoFactory.php +++ b/app/src/Restaurants/DtoFactory/RestaurantFilterVariantsDtoFactory.php @@ -2,14 +2,15 @@ namespace App\Restaurants\DtoFactory; +use App\Restaurants\Collection\KitchenCollection; +use App\Restaurants\Collection\RestaurantTypeCollection; use App\Restaurants\Dto\RestaurantFilterVariantsDto; -use App\Shared\Dto\DtoCollection; class RestaurantFilterVariantsDtoFactory { public function create( - DtoCollection $types, - DtoCollection $kitchens + RestaurantTypeCollection $types, + KitchenCollection $kitchens ): RestaurantFilterVariantsDto { return new RestaurantFilterVariantsDto( $types, diff --git a/app/src/Restaurants/DtoFactory/RestaurantListDtoFactory.php b/app/src/Restaurants/DtoFactory/RestaurantListDtoFactory.php index 3a372b9..66ca9e8 100644 --- a/app/src/Restaurants/DtoFactory/RestaurantListDtoFactory.php +++ b/app/src/Restaurants/DtoFactory/RestaurantListDtoFactory.php @@ -5,20 +5,20 @@ namespace App\Restaurants\DtoFactory; use App\Restaurants\Dto\RestaurantFilterVariantsDto; use App\Restaurants\Dto\RestaurantListDto; use App\Restaurants\Dto\RestaurantListingElementDto; -use App\Shared\Dto\DtoCollection; +use App\Shared\Collection\ListingCollection; use App\Shared\Dto\PaginationDto; class RestaurantListDtoFactory { /** * @param PaginationDto $pagination - * @param DtoCollection $list + * @param ListingCollection $list * @param RestaurantFilterVariantsDto $filters * @return RestaurantListDto */ public function create( PaginationDto $pagination, - DtoCollection $list, + ListingCollection $list, RestaurantFilterVariantsDto $filters ): RestaurantListDto { return new RestaurantListDto( diff --git a/app/src/Restaurants/DtoFactory/RestaurantListingElementDtoFactory.php b/app/src/Restaurants/DtoFactory/RestaurantListingElementDtoFactory.php index d46d87b..6fa858d 100644 --- a/app/src/Restaurants/DtoFactory/RestaurantListingElementDtoFactory.php +++ b/app/src/Restaurants/DtoFactory/RestaurantListingElementDtoFactory.php @@ -3,7 +3,7 @@ namespace App\Restaurants\DtoFactory; use App\Restaurants\Dto\RestaurantListingElementDto; -use App\Shared\Dto\DtoCollection; +use App\Shared\Collection\ListingCollection; use App\Shared\DtoFactory\FileDtoFactory; use App\Shared\Entity\Restaurants; use Ramsey\Collection\Collection; @@ -29,17 +29,14 @@ class RestaurantListingElementDtoFactory /** * @param Collection $restaurants - * @return DtoCollection + * @return ListingCollection */ - public function createCollection(Collection $restaurants): DtoCollection + public function createCollection(Collection $restaurants): ListingCollection { $restaurantsDto = $restaurants->map(function(Restaurants $restaurant) { return $this->create($restaurant); }); - return new DtoCollection( - RestaurantListingElementDto::class, - $restaurantsDto->toArray(), - ); + return new ListingCollection($restaurantsDto->toArray()); } } diff --git a/app/src/Restaurants/DtoFactory/RestaurantTypeDtoFactory.php b/app/src/Restaurants/DtoFactory/RestaurantTypeDtoFactory.php index 30b6ebc..b0578d1 100644 --- a/app/src/Restaurants/DtoFactory/RestaurantTypeDtoFactory.php +++ b/app/src/Restaurants/DtoFactory/RestaurantTypeDtoFactory.php @@ -2,8 +2,8 @@ namespace App\Restaurants\DtoFactory; +use App\Restaurants\Collection\RestaurantTypeCollection; use App\Restaurants\Dto\RestaurantTypeDto; -use App\Shared\Dto\DtoCollection; use App\Shared\Entity\RestaurantTypes; use Ramsey\Collection\Collection; @@ -20,16 +20,15 @@ class RestaurantTypeDtoFactory /** * @param Collection $types - * @return DtoCollection + * @return RestaurantTypeCollection */ - public function createCollection(Collection $types): DtoCollection + public function createCollection(Collection $types): RestaurantTypeCollection { $typesDto = $types->map(function(RestaurantTypes $type) { return $this->create($type); }); - return new DtoCollection( - RestaurantTypeDto::class, + return new RestaurantTypeCollection( $typesDto->toArray() ); } diff --git a/app/src/Restaurants/DtoFactory/TagDtoFactory.php b/app/src/Restaurants/DtoFactory/TagDtoFactory.php index 174e26e..d0f1fae 100644 --- a/app/src/Restaurants/DtoFactory/TagDtoFactory.php +++ b/app/src/Restaurants/DtoFactory/TagDtoFactory.php @@ -2,8 +2,9 @@ namespace App\Restaurants\DtoFactory; +use App\Restaurants\Collection\TagCollection; use App\Restaurants\Dto\TagDto; -use App\Shared\Dto\DtoCollection; +use App\Shared\Collection\StringCollection; use JsonException; use Ramsey\Collection\Collection as RamseyCollection; @@ -11,10 +12,10 @@ class TagDtoFactory { /** * @param string $json - * @return DtoCollection + * @return TagCollection * @throws JsonException */ - public function createCollection(string $json): DtoCollection + public function createCollection(string $json): TagCollection { $jsonCollection = new RamseyCollection( 'array', @@ -26,11 +27,11 @@ class TagDtoFactory ) ); - $tagCollection = new DtoCollection(TagDto::class); + $tagCollection = new TagCollection(); foreach ($jsonCollection as $name => $jsonItem) { $tagCollection->add( - new TagDto($name, new DtoCollection('string', $jsonItem)) + new TagDto($name, new StringCollection($jsonItem)) ); } diff --git a/app/src/Restaurants/Request/RestaurantListingRequest.php b/app/src/Restaurants/Request/RestaurantListingRequest.php deleted file mode 100644 index b85da7f..0000000 --- a/app/src/Restaurants/Request/RestaurantListingRequest.php +++ /dev/null @@ -1,36 +0,0 @@ -getRequest(); - foreach ($request->query->getIterator() as $property => $value) { - if (property_exists($this, $property)) { - $this->{$property} = $value; - } - } - } -} diff --git a/app/src/Restaurants/Service/RestaurantDetailService.php b/app/src/Restaurants/Service/RestaurantDetailService.php deleted file mode 100644 index b8dda37..0000000 --- a/app/src/Restaurants/Service/RestaurantDetailService.php +++ /dev/null @@ -1,34 +0,0 @@ -restaurants->find($request->detailId); - - if ($restaurant === null) { - throw new NotFoundError('Restaurant not found'); - } - - return $this->dtoFactory->create($restaurant); - } -} diff --git a/app/src/Restaurants/Service/RestaurantListingService.php b/app/src/Restaurants/Service/RestaurantListingService.php deleted file mode 100644 index a56b29f..0000000 --- a/app/src/Restaurants/Service/RestaurantListingService.php +++ /dev/null @@ -1,71 +0,0 @@ -restaurants - ->getCountWithFilters( - $request->restaurant_type_id, - $request->kitchen_id - ); - - $pagination = $this->paginationFactory->create( - $request->page, - $request->limit, - $countOfRestaurants, - ); - - $offset = min($pagination->pageSize, $countOfRestaurants) * ($pagination->currentPage - 1); - - $list = $this->listFactory->createCollection( - $this->restaurants->getWithFilters( - $request->kitchen_id, - $request->restaurant_type_id, - $pagination->pageSize, - $offset, - ), - ); - - - $filters = $this->filterFactory->create( - $this->typeFactory->createCollection($this->types->getAll()), - $this->kitchenFactory->createCollection($this->kitchens->getAll()) - ); - - return $this->listingFactory->create( - $pagination, - $list, - $filters - ); - } -} diff --git a/app/src/Restaurants/Service/RestaurantPrepareRequestService.php b/app/src/Restaurants/Service/RestaurantPrepareRequestService.php new file mode 100644 index 0000000..742d4c4 --- /dev/null +++ b/app/src/Restaurants/Service/RestaurantPrepareRequestService.php @@ -0,0 +1,122 @@ +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); + + $countOfRestaurants = $this + ->restaurants + ->getCountWithFilters( + $filters['restaurant_type_id'], + $filters['kitchen_id'] + ); + + $pagination = $this->paginationFactory->create( + $page, + $limit, + $countOfRestaurants, + ); + + $offset = min( + $pagination->pageSize, + $countOfRestaurants + ) * ($pagination->currentPage - 1); + + $list = $this->listFactory->createCollection( + $this->restaurants->getWithFilters( + $filters['kitchen_id'], + $filters['restaurant_type_id'], + $pagination->pageSize, + $offset, + ), + ); + + + $filters = $this->filterFactory->create( + $this->typeFactory->createCollection($this->types->getAll()), + $this->kitchenFactory->createCollection($this->kitchens->getAll()) + ); + + return $this->listingFactory->create( + $pagination, + $list, + $filters + ); + } + + /** + * @param string $detailId + * @return AbstractDetailElement + * @throws JsonException + */ + public + function bornDetailElement( + string $detailId + ): AbstractDetailElement { + if (!$this->validate->isValidUuid($detailId)) { + throw new NonValidUuidError(); + } + + $restaurant = $this->restaurants->find($detailId); + + if ($restaurant === null) { + throw new NotFoundError('Restaurant not found'); + } + + return $this->detailFactory->create($restaurant); + } +} diff --git a/app/src/Shared/Abstraction/AbstractController.php b/app/src/Shared/Abstraction/AbstractController.php index 3d40cc8..cfd97b4 100644 --- a/app/src/Shared/Abstraction/AbstractController.php +++ b/app/src/Shared/Abstraction/AbstractController.php @@ -2,32 +2,52 @@ namespace App\Shared\Abstraction; -use App\Shared\Dto\ErrorDto; use App\Shared\DtoFactory\ErrorDtoFactory; use App\Shared\Error\BaseError; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController as BundleController; use Symfony\Component\HttpFoundation\JsonResponse; +use Symfony\Component\HttpFoundation\Response; use Throwable; abstract class AbstractController extends BundleController { public function __construct( protected readonly ErrorDtoFactory $errorFactory, - ) {} + ) { + } + + /** + * @param ServiceInterface $service + * @param int $page + * @param int $limit + * @param array|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); - protected function handle(ServiceInterface $service, AbstractRequest $request): JsonResponse - { -// try { - return new JsonResponse($service->serve($request)); -// -// } catch (BaseError $error) { -// $errorDto = $this->errorFactory->create($error); -// -// return new JsonResponse($errorDto, $errorDto->status); -// -// } catch (Throwable $exception) { -// $error = new ErrorDto(); -// return new JsonResponse($error, $error->status); -// } + 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 new file mode 100644 index 0000000..6386d81 --- /dev/null +++ b/app/src/Shared/Abstraction/AbstractDetailElement.php @@ -0,0 +1,9 @@ + */ + public ListingCollection $list; +} diff --git a/app/src/Shared/Abstraction/AbstractListingElementDto.php b/app/src/Shared/Abstraction/AbstractListingElementDto.php new file mode 100644 index 0000000..9c676fd --- /dev/null +++ b/app/src/Shared/Abstraction/AbstractListingElementDto.php @@ -0,0 +1,10 @@ +populate(); - - if (self::AUTO_VALIDATE) { - $this->validate(); - } - } - - /** - * Маппинг реквеста - * @return void - */ - protected function populate(): void - { - foreach ($this->getRequest()->toArray() as $property => $value) { - if (property_exists($this, $property)) { - $this->{$property} = $value; - } - } - } - - /** - * Валидация и выброкса ошибки при валидации - * @return void - */ - public function validate(): void - { - $errors = $this->validator->validate($this); - - - $messages = new DtoCollection(ValidateErrorDto::class); - - foreach ($errors as $error) { - $messages->add((new ValidateErrorDtoFactory($error))->create()); - } - - if ($messages->count() > 0) { - $response = new JsonResponse($messages, 422); - $response->send(); - - throw new ValidatorException('Validation failed', $messages); - } - } - - /** - * Возвращает HttpFoundation реквест - * @return Request - */ - public function getRequest(): Request - { - return Request::createFromGlobals(); - } -} diff --git a/app/src/Shared/Abstraction/DtoInterface.php b/app/src/Shared/Abstraction/DtoInterface.php deleted file mode 100644 index 60a39cd..0000000 --- a/app/src/Shared/Abstraction/DtoInterface.php +++ /dev/null @@ -1,5 +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/Dto/DtoCollection.php b/app/src/Shared/Collection/DtoCollection.php similarity index 87% rename from app/src/Shared/Dto/DtoCollection.php rename to app/src/Shared/Collection/DtoCollection.php index 1e27e88..c8fcf69 100644 --- a/app/src/Shared/Dto/DtoCollection.php +++ b/app/src/Shared/Collection/DtoCollection.php @@ -1,13 +1,13 @@ collectionType, $data); + } +} diff --git a/app/src/Shared/Collection/ListingCollection.php b/app/src/Shared/Collection/ListingCollection.php new file mode 100644 index 0000000..9234de7 --- /dev/null +++ b/app/src/Shared/Collection/ListingCollection.php @@ -0,0 +1,15 @@ +collectionType, $data); + } +} diff --git a/app/src/Shared/Collection/StringCollection.php b/app/src/Shared/Collection/StringCollection.php new file mode 100644 index 0000000..0efaec1 --- /dev/null +++ b/app/src/Shared/Collection/StringCollection.php @@ -0,0 +1,13 @@ +collectionType, $data); + } +} diff --git a/app/src/Shared/Dto/ErrorDto.php b/app/src/Shared/Dto/ErrorDto.php index fe14352..31b8ccb 100644 --- a/app/src/Shared/Dto/ErrorDto.php +++ b/app/src/Shared/Dto/ErrorDto.php @@ -2,15 +2,12 @@ namespace App\Shared\Dto; -use App\Shared\Abstraction\DtoInterface; - -class ErrorDto implements DtoInterface +class ErrorDto { public function __construct( public string $status = '500', - public string $message = 'Something went wrong', - public string $code = '100', - ) {} + ) { + } } diff --git a/app/src/Shared/Dto/FileDto.php b/app/src/Shared/Dto/FileDto.php index 1cff98b..b232359 100644 --- a/app/src/Shared/Dto/FileDto.php +++ b/app/src/Shared/Dto/FileDto.php @@ -2,21 +2,15 @@ namespace App\Shared\Dto; -use App\Shared\Abstraction\DtoInterface; - -class FileDto implements DtoInterface +class FileDto { public function __construct( public string $id, - public string $name, - public string $description, - public int $size, - public string $type, - public string $url, - ) {} + ) { + } } diff --git a/app/src/Shared/Dto/PaginationDto.php b/app/src/Shared/Dto/PaginationDto.php index 8433d9b..a68ad9d 100644 --- a/app/src/Shared/Dto/PaginationDto.php +++ b/app/src/Shared/Dto/PaginationDto.php @@ -2,13 +2,12 @@ namespace App\Shared\Dto; -use App\Shared\Abstraction\DtoInterface; - -class PaginationDto implements DtoInterface +class PaginationDto { public function __construct( public int $currentPage, public int $pages, public int $pageSize, - ) {} + ) { + } } diff --git a/app/src/Shared/Dto/ValidateErrorDto.php b/app/src/Shared/Dto/ValidateErrorDto.php index 02a6766..f3d31a1 100644 --- a/app/src/Shared/Dto/ValidateErrorDto.php +++ b/app/src/Shared/Dto/ValidateErrorDto.php @@ -2,15 +2,12 @@ namespace App\Shared\Dto; -use App\Shared\Abstraction\DtoInterface; - -class ValidateErrorDto implements DtoInterface +class ValidateErrorDto { public function __construct( public string $message, - public string $code, - public string $status, - ) {} + ) { + } } diff --git a/app/src/Shared/DtoFactory/ErrorDtoFactory.php b/app/src/Shared/DtoFactory/ErrorDtoFactory.php index f2bc249..f917e26 100644 --- a/app/src/Shared/DtoFactory/ErrorDtoFactory.php +++ b/app/src/Shared/DtoFactory/ErrorDtoFactory.php @@ -9,10 +9,10 @@ class ErrorDtoFactory { public function create(BaseError $error): ErrorDto { - return new ErrorDto( - status: $error->getStatusCode(), - message: $error->getMessage(), - code: $error->getCode(), - ); + return new ErrorDto( + status: $error->getStatusCode(), + message: $error->getMessage(), + code: $error->getCode(), + ); } } diff --git a/app/src/Shared/DtoFactory/FileDtoFactory.php b/app/src/Shared/DtoFactory/FileDtoFactory.php index 6fbc5c9..7aa47a0 100644 --- a/app/src/Shared/DtoFactory/FileDtoFactory.php +++ b/app/src/Shared/DtoFactory/FileDtoFactory.php @@ -2,7 +2,7 @@ namespace App\Shared\DtoFactory; -use App\Shared\Dto\DtoCollection; +use App\Shared\Collection\FileCollection; use App\Shared\Dto\FileDto; use App\Shared\Entity\File; use Doctrine\Common\Collections\Collection; @@ -23,16 +23,15 @@ class FileDtoFactory /** * @param Collection $files - * @return DtoCollection + * @return FileCollection */ - public function createCollection(Collection $files): DtoCollection + public function createCollection(Collection $files): FileCollection { - $filesDto = $files->map(function(File $file) { + $filesDto = $files->map(function (File $file) { return $this->create($file); }); - return new DtoCollection( - FileDto::class, + return new FileCollection( $filesDto->toArray() ); } diff --git a/app/src/Shared/DtoFactory/PaginationDtoFactory.php b/app/src/Shared/DtoFactory/PaginationDtoFactory.php index 933de0c..7973036 100644 --- a/app/src/Shared/DtoFactory/PaginationDtoFactory.php +++ b/app/src/Shared/DtoFactory/PaginationDtoFactory.php @@ -12,6 +12,6 @@ class PaginationDtoFactory $pages = ceil($total / $pageSize); $page = ($page > 1) && ($page <= $pages) ? $page : 1; - return new PaginationDto($page, $pages, $pageSize); + return new PaginationDto($page, $pages, $pageSize); } } diff --git a/app/src/Shared/DtoFactory/ValidateErrorDtoFactory.php b/app/src/Shared/DtoFactory/ValidateErrorDtoFactory.php index 9cb815a..8947518 100644 --- a/app/src/Shared/DtoFactory/ValidateErrorDtoFactory.php +++ b/app/src/Shared/DtoFactory/ValidateErrorDtoFactory.php @@ -4,13 +4,12 @@ namespace App\Shared\DtoFactory; use App\Shared\Dto\ValidateErrorDto; use App\Shared\Error\ErrorCode; -use http\Message; use Symfony\Component\Validator\ConstraintViolationInterface; class ValidateErrorDtoFactory { - public function create(ConstraintViolationInterface $error): ValidateErrorDto - { + public function create(ConstraintViolationInterface $error + ): ValidateErrorDto { return new ValidateErrorDto( message: $error->getPropertyPath() . ' : ' . $error->getMessage(), code: ErrorCode::ValidateFailed->value, diff --git a/app/src/Shared/Entity/File.php b/app/src/Shared/Entity/File.php index 5c448b7..0063008 100644 --- a/app/src/Shared/Entity/File.php +++ b/app/src/Shared/Entity/File.php @@ -2,7 +2,6 @@ namespace App\Shared\Entity; -use App\Shared\Abstraction\EntityInterface; use App\Shared\Repository\FileRepository; use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\Collection; @@ -10,7 +9,7 @@ use Doctrine\DBAL\Types\Types; use Doctrine\ORM\Mapping as ORM; #[ORM\Entity(repositoryClass: FileRepository::class)] -class File implements EntityInterface +class File { #[ORM\Id] #[ORM\Column(type: Types::GUID)] @@ -53,7 +52,6 @@ class File implements EntityInterface * @var Collection */ #[ORM\OneToMany(targetEntity: News::class, mappedBy: 'detailImage')] - private Collection $newsDetail; /** @@ -151,8 +149,8 @@ class File implements EntityInterface return $this->restaurantsPreview; } - public function addRestaurantsPreview(Restaurants $restaurantsPreview): static - { + public function addRestaurantsPreview(Restaurants $restaurantsPreview + ): static { if (!$this->restaurantsPreview->contains($restaurantsPreview)) { $this->restaurantsPreview->add($restaurantsPreview); $restaurantsPreview->setPreviewImage($this); @@ -161,8 +159,8 @@ class File implements EntityInterface return $this; } - public function removeRestaurantsPreview(Restaurants $restaurantsPreview): static - { + public function removeRestaurantsPreview(Restaurants $restaurantsPreview + ): static { if ($this->restaurantsPreview->removeElement($restaurantsPreview)) { // set the owning side to null (unless already changed) if ($restaurantsPreview->getPreviewImage() === $this) { @@ -191,8 +189,8 @@ class File implements EntityInterface return $this; } - public function removeRestaurantsDetail(Restaurants $restaurantsDetail): static - { + public function removeRestaurantsDetail(Restaurants $restaurantsDetail + ): static { if ($this->restaurantsDetail->removeElement($restaurantsDetail)) { // set the owning side to null (unless already changed) if ($restaurantsDetail->getDetailImage() === $this) { @@ -211,8 +209,8 @@ class File implements EntityInterface return $this->restaurantsGallery; } - public function addRestaurantsGallery(Restaurants $restaurantsGallery): static - { + public function addRestaurantsGallery(Restaurants $restaurantsGallery + ): static { if (!$this->restaurantsGallery->contains($restaurantsGallery)) { $this->restaurantsGallery->add($restaurantsGallery); $restaurantsGallery->addGallery($this); @@ -221,8 +219,8 @@ class File implements EntityInterface return $this; } - public function removeRestaurantsGallery(Restaurants $restaurantsGallery): static - { + public function removeRestaurantsGallery(Restaurants $restaurantsGallery + ): static { if ($this->restaurantsGallery->removeElement($restaurantsGallery)) { $restaurantsGallery->removeGallery($this); } diff --git a/app/src/Shared/Entity/Kitchens.php b/app/src/Shared/Entity/Kitchens.php index b6eb7b1..b10ba1c 100644 --- a/app/src/Shared/Entity/Kitchens.php +++ b/app/src/Shared/Entity/Kitchens.php @@ -2,7 +2,6 @@ namespace App\Shared\Entity; -use App\Shared\Abstraction\EntityInterface; use App\Shared\Repository\KitchensRepository; use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\Collection; @@ -10,7 +9,7 @@ use Doctrine\DBAL\Types\Types; use Doctrine\ORM\Mapping as ORM; #[ORM\Entity(repositoryClass: KitchensRepository::class)] -class Kitchens implements EntityInterface +class Kitchens { #[ORM\Id] #[ORM\Column(type: Types::GUID)] diff --git a/app/src/Shared/Entity/News.php b/app/src/Shared/Entity/News.php index 9640253..d88a733 100644 --- a/app/src/Shared/Entity/News.php +++ b/app/src/Shared/Entity/News.php @@ -2,10 +2,6 @@ namespace App\Shared\Entity; -use App\News\Dto\NewsDetailElementDto; -use App\News\Dto\NewsListingElementDto; -use App\Shared\Abstraction\DtoInterface; -use App\Shared\Abstraction\EntityInterface; use App\Shared\Repository\NewsRepository; use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\Collection; diff --git a/app/src/Shared/Entity/NewsCategories.php b/app/src/Shared/Entity/NewsCategories.php index f4b30cb..e22e0cf 100644 --- a/app/src/Shared/Entity/NewsCategories.php +++ b/app/src/Shared/Entity/NewsCategories.php @@ -2,9 +2,6 @@ namespace App\Shared\Entity; -use App\News\Dto\NewsCategoryDto; -use App\Shared\Abstraction\DtoInterface; -use App\Shared\Abstraction\EntityInterface; use App\Shared\Repository\NewsCategoriesRepository; use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\Collection; @@ -12,7 +9,7 @@ use Doctrine\DBAL\Types\Types; use Doctrine\ORM\Mapping as ORM; #[ORM\Entity(repositoryClass: NewsCategoriesRepository::class)] -class NewsCategories implements EntityInterface +class NewsCategories { #[ORM\Id] #[ORM\Column(type: Types::GUID)] diff --git a/app/src/Shared/Entity/RestaurantTypes.php b/app/src/Shared/Entity/RestaurantTypes.php index 8081330..49043fc 100644 --- a/app/src/Shared/Entity/RestaurantTypes.php +++ b/app/src/Shared/Entity/RestaurantTypes.php @@ -2,7 +2,6 @@ namespace App\Shared\Entity; -use App\Shared\Abstraction\EntityInterface; use App\Shared\Repository\RestaurantTypesRepository; use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\Collection; @@ -10,7 +9,7 @@ use Doctrine\DBAL\Types\Types; use Doctrine\ORM\Mapping as ORM; #[ORM\Entity(repositoryClass: RestaurantTypesRepository::class)] -class RestaurantTypes implements EntityInterface +class RestaurantTypes { #[ORM\Id] #[ORM\Column(type: Types::GUID)] diff --git a/app/src/Shared/Entity/Restaurants.php b/app/src/Shared/Entity/Restaurants.php index a88eba4..11033eb 100644 --- a/app/src/Shared/Entity/Restaurants.php +++ b/app/src/Shared/Entity/Restaurants.php @@ -2,23 +2,15 @@ namespace App\Shared\Entity; -use App\Restaurants\Dto\KitchenTypeDto; -use App\Restaurants\Dto\RestaurantDetailElementDto; -use App\Restaurants\Dto\RestaurantListingElementDto; -use App\Restaurants\Dto\TagDto; -use App\Shared\Abstraction\DtoInterface; -use App\Shared\Abstraction\EntityInterface; -use App\Shared\Dto\DtoCollection; -use App\Shared\Dto\FileDto; use App\Shared\Repository\RestaurantsRepository; +use DateTimeImmutable; use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\Collection; use Doctrine\DBAL\Types\Types; use Doctrine\ORM\Mapping as ORM; -use Ramsey\Collection\Collection as RamseyCollection; #[ORM\Entity(repositoryClass: RestaurantsRepository::class)] -class Restaurants implements EntityInterface +class Restaurants { #[ORM\Id] #[ORM\Column(type: Types::GUID)] @@ -31,10 +23,10 @@ class Restaurants implements EntityInterface private ?int $sort = null; #[ORM\Column] - private ?\DateTimeImmutable $createdAt = null; + private ?DateTimeImmutable $createdAt = null; #[ORM\Column] - private ?\DateTimeImmutable $updateAt = null; + private ?DateTimeImmutable $updateAt = null; #[ORM\ManyToOne(inversedBy: 'restaurantsTypes')] private ?RestaurantTypes $type = null; @@ -138,24 +130,24 @@ class Restaurants implements EntityInterface return $this; } - public function getCreatedAt(): ?\DateTimeImmutable + public function getCreatedAt(): ?DateTimeImmutable { return $this->createdAt; } - public function setCreatedAt(\DateTimeImmutable $createdAt): static + public function setCreatedAt(DateTimeImmutable $createdAt): static { $this->createdAt = $createdAt; return $this; } - public function getUpdateAt(): ?\DateTimeImmutable + public function getUpdateAt(): ?DateTimeImmutable { return $this->updateAt; } - public function setUpdateAt(\DateTimeImmutable $updateAt): static + public function setUpdateAt(DateTimeImmutable $updateAt): static { $this->updateAt = $updateAt; diff --git a/app/src/Shared/Error/BaseError.php b/app/src/Shared/Error/BaseError.php index 7a297f7..62eb87c 100644 --- a/app/src/Shared/Error/BaseError.php +++ b/app/src/Shared/Error/BaseError.php @@ -2,12 +2,13 @@ namespace App\Shared\Error; +use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Exception\HttpException; class BaseError extends HttpException { public function __construct( - int $statusCode = 500, + int $statusCode = Response::HTTP_INTERNAL_SERVER_ERROR, string $message = 'Something bad happening', ErrorCode $code = ErrorCode::NotExcepted ) { diff --git a/app/src/Shared/Error/ErrorCode.php b/app/src/Shared/Error/ErrorCode.php index a9495f4..9217c1f 100644 --- a/app/src/Shared/Error/ErrorCode.php +++ b/app/src/Shared/Error/ErrorCode.php @@ -7,4 +7,6 @@ enum ErrorCode: int case EntityNotFound = 10; case ValidateFailed = 20; case NotExcepted = 50; + + case UuidInvalid = 60; } diff --git a/app/src/Shared/Error/NonValidUuidError.php b/app/src/Shared/Error/NonValidUuidError.php new file mode 100644 index 0000000..9e5b6b8 --- /dev/null +++ b/app/src/Shared/Error/NonValidUuidError.php @@ -0,0 +1,16 @@ +getRequest()->getUri(); - $index = strrpos($requestUrl, "/") + 1; - - $detailId = substr($requestUrl, $index); - - if (property_exists($this, "detailId")) { - $this->{"detailId"} = $detailId; - } - } -} diff --git a/app/src/Shared/Request/EmptyRequest.php b/app/src/Shared/Request/EmptyRequest.php deleted file mode 100644 index 2f2ab8e..0000000 --- a/app/src/Shared/Request/EmptyRequest.php +++ /dev/null @@ -1,12 +0,0 @@ -