diff --git a/composer.json b/composer.json index fbf9fbc8e1a2f9ab98dd6b2153136facbbf7fd48..72f280cdff26c1c1e856b0cc4ce451c0827b89d3 100644 --- a/composer.json +++ b/composer.json @@ -43,6 +43,7 @@ "symfony/uid": "7.0.*", "symfony/ux-turbo": "^2.16", "symfony/validator": "6.4.*", + "symfony/var-exporter": "6.4.5", "symfony/web-link": "7.0.*", "symfony/yaml": "7.0.*", "twig/extra-bundle": "^2.12|^3.0", diff --git a/composer.lock b/composer.lock index ec268ac13b60dfe0f89b728f281acc235d663f87..c4fecf9d01c96e8ba34983e105231aa442cb0212 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "b57c193bd8eed3bbe092186cd3648c51", + "content-hash": "814cfe23efd2a11a49bcfec687d342ba", "packages": [ { "name": "composer/semver", diff --git a/src/Controller/RestaurantController.php b/src/Controller/RestaurantController.php index d1dc2fce8d1363a801f295e02d0a43efd36ad276..9f665fca780f9a51c79387eb78f327a4ae7106eb 100644 --- a/src/Controller/RestaurantController.php +++ b/src/Controller/RestaurantController.php @@ -6,9 +6,11 @@ namespace App\Controller; use App\Service\RestaurantService; use Nelmio\ApiDocBundle\Annotation\Model; -use OpenApi\Annotations as OA; +use OpenApi\Attributes as OA; +use OpenApi\Attributes\Schema; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\HttpFoundation\Request; use Symfony\Component\Routing\Attribute\Route; use App\Model\RestaurantList; @@ -17,22 +19,50 @@ class RestaurantController extends AbstractController { public function __construct(private RestaurantService $restaurantService) {} - /** - @OA\OpenApi\Response(response=200, description="ЛиÑтинг реÑторанов") - * @OA\Parameter( - * name="page", - * in="query", - * description="Ðомер Ñтраницы", - * @OA\Schema(type="integer") - * ) - * @Model(type=RestaurantList::class) - */ #[Route('/restaurants', name: 'restaurants', methods: ['GET'])] - public function index(int $page): Response + #[OA\Response(response: 200, description: "ЛиÑтинг реÑторанов")] + #[OA\Parameter( + name: "page", + description: "Ðомер Ñтраницы", + in: "query", + schema: new Schema(type: "integer", default: 1, example: 1) + )] + #[OA\Parameter( + name: "limit", + description: "Лимит", + in: "query", + schema: new Schema(type: "integer", default: 12, example: 12) + )] + #[OA\Parameter( + name: "restaurant_type_id", + description: "Идентификатор типа реÑторанов", + in: "query", + schema: new Schema(type: "integer") + )] + #[OA\Parameter( + name: "kitchen_id", + description: "Идентификатор кухни", + in: "query", + schema: new Schema(type: "integer") + )] + #[Model(type: RestaurantList::class)] + public function restaurants(Request $request): Response { - $restaurantsList = $this->restaurantService->getRestaurants(); + $page = $request->query->getInt('page'); + $limit = $request->query->getInt('limit'); + $restaurantTypeId = $request->query->getInt('restaurant_type_id'); + $kitchenId = $request->query->getInt('kitchen_id'); + $restaurantsList = $this->restaurantService->getRestaurants( + $page, $limit, $restaurantTypeId, $kitchenId + ); return $this->json($restaurantsList); } - + #[Route('/restaurants/{restaurantId}', name: 'restaurant', methods: ['GET'])] + public function restaurant(Request $request): Response + { + $restaurantId = (int)$request->get('restaurantId'); + $restaurant = $this->restaurantService->getRestaurant($restaurantId); + return $this->json($restaurant); + } } diff --git a/src/Mapper/RestaurantMapper.php b/src/Mapper/RestaurantMapper.php index 53a7e122ce13e556b76b7d693430c0e55973425f..5f9b6ae42db0d68d344d5d477a7a028be11be029 100644 --- a/src/Mapper/RestaurantMapper.php +++ b/src/Mapper/RestaurantMapper.php @@ -2,13 +2,20 @@ namespace App\Mapper; +use App\Entity\Address; +use App\Entity\Email; use App\Entity\Kitchen; +use App\Entity\Phone; use App\Entity\Restaurant; use App\Entity\RestaurantType; +use App\Entity\Tags; use App\Model\File; use App\Model\KitchenType; +use App\Model\RestaurantDetailElement; use App\Model\RestaurantListingElement; use App\Model\RestaurantType as RestaurantTypeModel; +use App\Model\Tag; +use Ramsey\Collection\Collection; class RestaurantMapper { @@ -36,12 +43,56 @@ class RestaurantMapper ); } + public static function mapToDetailElement(Restaurant $restaurant): RestaurantDetailElement + { + $file = new File( + 1, + "name", + "description", + 10, + "jpg", + $restaurant->getPreviewImage() + ); + return new RestaurantDetailElement( + $restaurant->getId(), + $restaurant->getName(), + $restaurant->getCode(), + implode(',', $restaurant->getCoordinates()), + //self::mapToRestaurantType($restaurant->getTypeId()), + $restaurant->getCheckPrice(), + $restaurant->getCheckInfo(), + new Collection( + KitchenType::class, array_map( + function (Kitchen $kitchen) { + return self::mapToKitchenType($kitchen); + }, $restaurant->getKitchen()->toArray()), + ), + new Collection(Phone::class, $restaurant->getPhone()->toArray()), + new Collection(Email::class, $restaurant->getEmail()->toArray()), + new Collection(Address::class, $restaurant->getAddress()->toArray()), + new Collection(Tag::class, array_map( + function (Tags $tag) { + return new Tag( + "группа тегов 1", + new Collection($tag->getName()), + ); + }, $restaurant->getTags()->toArray() + )), + $restaurant->getSite(), + $file, + new Collection(File::class, [$file]), + "Отель Ðрктика", + "otel arktika", + "otel arktika" + ); + } + public static function mapToRestaurantType(RestaurantType $restaurantType): RestaurantTypeModel { return new RestaurantTypeModel( $restaurantType->getId(), $restaurantType->getName(), - $restaurantType->getCode(), + $restaurantType->getCode() ); } diff --git a/src/Model/RestaurantDetailElement.php b/src/Model/RestaurantDetailElement.php index face2bf58efb0ca8701e45a8bdca5ff1665cf969..01e3e6711119cdcf78631aa5d63af504cc1cd8a1 100644 --- a/src/Model/RestaurantDetailElement.php +++ b/src/Model/RestaurantDetailElement.php @@ -10,7 +10,7 @@ class RestaurantDetailElement private string $name; private string $code; private string $coordinates; - private RestaurantType $type; + //private RestaurantType $type; private string $check; private string $checkInfo; /** @@ -48,7 +48,7 @@ class RestaurantDetailElement string $name, string $code, string $coordinates, - RestaurantType $type, + //RestaurantType $type, string $check, string $checkInfo, Collection $kitchen, @@ -67,7 +67,7 @@ class RestaurantDetailElement $this->name = $name; $this->code = $code; $this->coordinates = $coordinates; - $this->type = $type; + //$this->type = $type; $this->check = $check; $this->checkInfo = $checkInfo; $this->kitchen = $kitchen; @@ -103,10 +103,10 @@ class RestaurantDetailElement return $this->coordinates; } - public function getType(): RestaurantType - { - return $this->type; - } +// public function getType(): RestaurantType +// { +// return $this->type; +// } public function getCheck(): string { diff --git a/src/Repository/Interface/RestaurantRepositoryInterface.php b/src/Repository/Interface/RestaurantRepositoryInterface.php index f5fa2f2efd34014320a60add5b161155168b458d..6bffa21c923bf20812078b6158dbd4c1b24f1188 100644 --- a/src/Repository/Interface/RestaurantRepositoryInterface.php +++ b/src/Repository/Interface/RestaurantRepositoryInterface.php @@ -2,7 +2,13 @@ namespace App\Repository\Interface; +use App\Entity\Restaurant; + interface RestaurantRepositoryInterface { - public function getAll(): array; + public function getAll($page, $limit, $restaurantTypeId, $kitchenId): array; + + public function getCount(): int; + + public function getById(int $id): Restaurant; } \ No newline at end of file diff --git a/src/Repository/RestaurantRepository.php b/src/Repository/RestaurantRepository.php index 361489d4e998a8c0d45421da5014961d20eb9239..53c05619521538b98d960dfaa865ca45ecfad1ba 100644 --- a/src/Repository/RestaurantRepository.php +++ b/src/Repository/RestaurantRepository.php @@ -22,8 +22,31 @@ class RestaurantRepository extends ServiceEntityRepository implements Restaurant parent::__construct($registry, Restaurant::class); } - public function getAll(): array + public function getAll($page, $limit, $restaurantTypeId, $kitchenId): array { - return $this->findAll(); + $query = $this->createQueryBuilder('r'); + $query->select('r'); + if ($restaurantTypeId !== 0) { + $query->andWhere('r.typeId = :restaurantTypeId') + ->setParameter('restaurantTypeId', $restaurantTypeId); + } + if ($kitchenId !== 0) { + $query->join('r.kitchen', 'k') + ->andWhere('k.id = :kitchenId') + ->setParameter('kitchenId', $kitchenId); + } + $query->setMaxResults($limit) + ->setFirstResult(($page - 1) * $limit); + return $query->getQuery()->getResult(); + } + + public function getCount(): int + { + return $this->count(); + } + + public function getById(int $id): Restaurant + { + return $this->find($id); } } diff --git a/src/Service/RestaurantService.php b/src/Service/RestaurantService.php index d9f26f91b583ba7a0127991dc16bdc2133c79cf3..7e1866dd93121bb35c6d7e8bdbe7ac3b5e0e8ca6 100644 --- a/src/Service/RestaurantService.php +++ b/src/Service/RestaurantService.php @@ -2,12 +2,18 @@ namespace App\Service; +use App\Entity\Address; +use App\Entity\Email; use App\Entity\Kitchen; +use App\Entity\Phone; use App\Entity\Restaurant; use App\Entity\RestaurantType; +use App\Entity\Tags; use App\Mapper\RestaurantMapper; +use App\Model\File; use App\Model\KitchenType; use App\Model\Pagination; +use App\Model\RestaurantDetailElement; use App\Model\RestaurantFilterVariants; use App\Model\RestaurantList; use App\Model\RestaurantListingElement; @@ -25,14 +31,22 @@ class RestaurantService private KitchenRepositoryInterface $kitchenRepository, ) {} - public function getRestaurants(): RestaurantList + public function getRestaurants( + $page, + $limit, + $restaurantTypeId, + $kitchenId): RestaurantList { - $restaurants = $this->restaurantRepository->getAll(); + $restaurants = $this->restaurantRepository->getAll( + $page, + $limit, + $restaurantTypeId, + $kitchenId); + $count = $this->restaurantRepository->getCount(); $restaurantTypes = $this->restaurantTypeRepository->getAll(); $kitchens = $this->kitchenRepository->getAll(); - return new RestaurantList( - new Pagination(1, floor(count($restaurants)) / 5, 16), + new Pagination($page, ceil($count / $limit), $limit), new Collection(RestaurantListingElement::class, array_map( function (Restaurant $restaurant) { return RestaurantMapper::mapToListElement($restaurant); @@ -55,4 +69,10 @@ class RestaurantService ) ); } + + public function getRestaurant(int $id): RestaurantDetailElement + { + $restaurant = $this->restaurantRepository->getById($id); + return RestaurantMapper::mapToDetailElement($restaurant); + } } \ No newline at end of file