diff --git a/src/Controller/NewsController.php b/src/Controller/NewsController.php index 6102cadc0342454f357e627541c1f37869cc5d5d..12d0a6ebb07be1ed59bf192ee8a1df0e67fcd8df 100644 --- a/src/Controller/NewsController.php +++ b/src/Controller/NewsController.php @@ -20,29 +20,34 @@ class NewsController extends AbstractController #[Route('/news', name: 'news', methods: ['GET'])] public function news(NewsListRequest $request): Response { - return $this->json($this->newsService->getNewsByRequest($request)); + return $this->json( + $this->newsService->getNewsByRequest($request) + ); } #[Route('/news/mainNews', name: 'mainNews', methods: ['GET'])] public function mainNews(): Response { - $mainNews = $this->newsService->getMainNews(); - return $this->json($mainNews); + return $this->json( + $this->newsService->getMainNews() + ); } #[Route('/news/search', name: 'newsSearch', methods: ['GET'])] public function newsSearch(): Response { - $newsSearch = $this->newsService->getNewsSearch(); - return $this->json($newsSearch); + return $this->json( + $this->newsService->getNewsSearch() + ); } #[Route('/news/{newsId}', name: 'newsOne', methods: ['GET'])] public function newsOne(Request $request): Response { try { - $news = $this->newsService->getNewsOneByRequest($request); - return $this->json($news); + return $this->json( + $this->newsService->getNewsOneByRequest($request) + ); } catch (NewsNotFoundException $e) { return $this->json([ 'success' => false, diff --git a/src/Controller/RestaurantController.php b/src/Controller/RestaurantController.php index 701ff9322dffa6e1df04d2bb917858334faf83a6..8f842cb2cac290ea6645ee58ffe50422a17844c3 100644 --- a/src/Controller/RestaurantController.php +++ b/src/Controller/RestaurantController.php @@ -20,17 +20,17 @@ class RestaurantController extends AbstractController #[Route('/restaurants', name: 'restaurants', methods: ['GET'])] public function restaurants(RestaurantListRequest $request): Response { - return $this->json($this->restaurantService->getRestaurantsByRequest($request)); + return $this->json( + $this->restaurantService->getRestaurantsByRequest($request) + ); } #[Route('/restaurants/{restaurantId}', name: 'restaurant', methods: ['GET'])] public function restaurant(Request $request): Response { try { - return new Response( + return $this->json( $this->restaurantService->getRestaurantByRequest($request), - Response::HTTP_OK, - ['Content-Type' => 'application/json'] ); } catch (RestaurantNotFoundException $e) { return $this->json([ diff --git a/src/Mapper/NewsMapper.php b/src/Mapper/NewsMapper.php index 420dbf7d59bccb2d1bfcddc29189b44fdead420b..4b06e81280f3f341e99e4fc4fc71fadfa960f6d3 100644 --- a/src/Mapper/NewsMapper.php +++ b/src/Mapper/NewsMapper.php @@ -65,8 +65,9 @@ class NewsMapper ); } - public static function mapToNewsCategory(NewsCategory $newsCategory): NewsCategoryModel - { + public static function mapToNewsCategory( + NewsCategory $newsCategory + ): NewsCategoryModel { return new NewsCategoryModel( $newsCategory->getId(), $newsCategory->getName(), diff --git a/src/Mapper/RestaurantMapper.php b/src/Mapper/RestaurantMapper.php index a300f1f5945660a3a3a1d72d8ef942cda16333ea..fc72b1520568a617d193e7c6712b59f10d28f9e4 100644 --- a/src/Mapper/RestaurantMapper.php +++ b/src/Mapper/RestaurantMapper.php @@ -44,17 +44,18 @@ class RestaurantMapper ), new Collection( KitchenType::class, array_map( - function (Kitchen $kitchen) { - return self::mapToKitchenType($kitchen); - }, $kitchens->toArray() - ), + function (Kitchen $kitchen) { + return self::mapToKitchenType($kitchen); + }, $kitchens->toArray() + ), ) ) ); } - public static function mapToListElement(Restaurant $restaurant): RestaurantListingElement - { + public static function mapToListElement( + Restaurant $restaurant + ): RestaurantListingElement { return new RestaurantListingElement( $restaurant->getId(), $restaurant->getName(), @@ -66,8 +67,10 @@ class RestaurantMapper ); } - public static function mapToDetailElement(Restaurant $restaurant, Collection $gallery): RestaurantDetailElement - { + public static function mapToDetailElement( + Restaurant $restaurant, + Collection $gallery + ): RestaurantDetailElement { return new RestaurantDetailElement( $restaurant->getId(), $restaurant->getName(), @@ -100,8 +103,9 @@ class RestaurantMapper ); } - public static function mapToRestaurantType(RestaurantType $restaurantType): RestaurantTypeModel - { + public static function mapToRestaurantType( + RestaurantType $restaurantType + ): RestaurantTypeModel { return new RestaurantTypeModel( $restaurantType->getId(), $restaurantType->getName(), diff --git a/src/Repository/RestaurantRepository.php b/src/Repository/RestaurantRepository.php index 6ff13152829df7ac4eb0073443e620e50a32fd80..3a23833c0aed436afc3c087a8c01ea53bde11a0e 100644 --- a/src/Repository/RestaurantRepository.php +++ b/src/Repository/RestaurantRepository.php @@ -31,17 +31,20 @@ class RestaurantRepository extends ServiceEntityRepository implements Restaurant ): array { $query = $this->createQueryBuilder('r'); $query->select('r'); + if ($restaurantTypeId !== null) { $query->andWhere('r.typeId = :restaurantTypeId') ->setParameter('restaurantTypeId', $restaurantTypeId); } if ($kitchenId !== null) { $query->join('r.kitchen', 'k') - ->andWhere('k.id = :kitchenId') + ->andWhere('k.id = :kitchenId') ->setParameter('kitchenId', $kitchenId); } + $query->setMaxResults($limit) - ->setFirstResult(($page - 1) * $limit); + ->setFirstResult(($page - 1) * $limit); + return $query->getQuery()->getResult(); } diff --git a/src/Service/NewsService.php b/src/Service/NewsService.php index fca6f8a73598494cbfde35a6857f7e754d4f8adb..52bdd28bfe12389a23346e3cc8504649c7c4fdb4 100644 --- a/src/Service/NewsService.php +++ b/src/Service/NewsService.php @@ -27,9 +27,10 @@ class NewsService public function getNewsByRequest(NewsListRequest $request): NewsList { - $page = $request->getRequest()->query->get('page') ?? self::DEFAULT_PAGE; - $limit = $request->getRequest()->query->get('limit') ?? self::DEFAULT_LIMIT; + $page = $request->getRequest()->query->get('page')?? self::DEFAULT_PAGE; + $limit = $request->getRequest()->query->get('limit')?? self::DEFAULT_LIMIT; $newsCategory = $request->getRequest()->query->get('news_category'); + return $this->getNews($page, $limit, $newsCategory); } @@ -43,27 +44,34 @@ class NewsService $newsCategory ) ); - $newsCategories = new Collection(NewsCategory::class, $this->newsCategoryRepository->getAll()); + $newsCategories = new Collection( + NewsCategory::class, + $this->newsCategoryRepository->getAll() + ); $count = $this->newsRepository->getCount(); + return NewsMapper::mapToNewsList($news, $newsCategories, $page, $limit, $count); } public function getMainNews(): NewsListingElement { - $mainNews = $this->newsRepository->getMainNews(); - return NewsMapper::mapToListingElement($mainNews); + return NewsMapper::mapToListingElement( + $this->newsRepository->getMainNews() + ); } public function getNewsSearch(): NewsDetailElement { - $mainNews = $this->newsRepository->getMainNews(); - return NewsMapper::mapToDetailElement($mainNews); + return NewsMapper::mapToDetailElement( + $this->newsRepository->getMainNews() + ); } public function getNewsOneByRequest(Request $request): NewsDetailElement { - $newsId = $request->get('newsId'); - return $this->getNewsOne($newsId); + return $this->getNewsOne( + $request->get('newsId') + ); } public function getNewsOne(string $newsId): NewsDetailElement @@ -72,6 +80,7 @@ class NewsService if ($news === null) { throw new NewsNotFoundException(); } + return NewsMapper::mapToDetailElement($news); } } \ No newline at end of file diff --git a/src/Service/RestaurantService.php b/src/Service/RestaurantService.php index 199efb49373bd7618b8b7eeb9feec15df2a8dcb2..23ade5987d82fd50c87649347211d974bc1e5640 100644 --- a/src/Service/RestaurantService.php +++ b/src/Service/RestaurantService.php @@ -17,7 +17,6 @@ use App\Repository\Interface\RestaurantRepositoryInterface; use App\Repository\Interface\RestaurantTypeRepositoryInterface; use Ramsey\Collection\Collection; use Symfony\Component\HttpFoundation\Request; -use Symfony\Component\Serializer\SerializerInterface; class RestaurantService { @@ -29,15 +28,16 @@ class RestaurantService private RestaurantTypeRepositoryInterface $restaurantTypeRepository, private KitchenRepositoryInterface $kitchenRepository, private GalleryRepositoryInterface $galleryRepository, - private SerializerInterface $serializer, ) {} - public function getRestaurantsByRequest(RestaurantListRequest $request): RestaurantList - { - $page = $request->getRequest()->query->get('page') ?? self::DEFAULT_PAGE; - $limit = $request->getRequest()->query->get('limit') ?? self::DEFAULT_LIMIT; + public function getRestaurantsByRequest( + RestaurantListRequest $request + ): RestaurantList { + $page = $request->getRequest()->query->get('page')?? self::DEFAULT_PAGE; + $limit = $request->getRequest()->query->get('limit')?? self::DEFAULT_LIMIT; $restaurantTypeId = $request->getRequest()->query->get('restaurant_type_id'); $kitchenId = $request->getRequest()->query->get('kitchen_id'); + return $this->getRestaurants( $page, $limit, $restaurantTypeId, $kitchenId ); @@ -49,7 +49,8 @@ class RestaurantService string|null $restaurantTypeId, string|null $kitchenId ): RestaurantList { - $restaurants = new Collection(Restaurant::class, + $restaurants = new Collection( + Restaurant::class, $this->restaurantRepository->getAll( $page, $limit, @@ -58,8 +59,15 @@ class RestaurantService ) ); $count = $this->restaurantRepository->getCount(); - $restaurantTypes = new Collection(RestaurantType::class, $this->restaurantTypeRepository->getAll()); - $kitchens = new Collection(Kitchen::class, $this->kitchenRepository->getAll()); + $restaurantTypes = new Collection( + RestaurantType::class, + $this->restaurantTypeRepository->getAll() + ); + $kitchens = new Collection( + Kitchen::class, + $this->kitchenRepository->getAll() + ); + return RestaurantMapper::mapToRestaurantList( $restaurants, $restaurantTypes, @@ -70,15 +78,11 @@ class RestaurantService ); } - public function getRestaurantByRequest(Request $request): string + public function getRestaurantByRequest(Request $request): RestaurantDetailElement { - $restaurantId = $request->get('restaurantId'); - $restaurant = $this->getRestaurant($restaurantId); - return $this->serializer->serialize($restaurant, 'json', [ - 'circular_reference_handler' => function ($object) { - return $object->getId(); - } - ]); + return $this->getRestaurant( + $request->get('restaurantId') + ); } public function getRestaurant(string $id): RestaurantDetailElement @@ -87,10 +91,12 @@ class RestaurantService if ($restaurant === null) { throw new RestaurantNotFoundException(); } + $gallery = new Collection( Gallery::class, $this->galleryRepository->getGalleryByRestaurantId($restaurant->getId()) ); + return RestaurantMapper::mapToDetailElement($restaurant, $gallery); } } \ No newline at end of file