From f75396fd908db78e003445bf7ef426df797847bf Mon Sep 17 00:00:00 2001
From: "a.shamavov" <a.shamavov@iqdev.digital>
Date: Wed, 24 Apr 2024 17:27:10 +0500
Subject: [PATCH] =?UTF-8?q?STA-966=20|=20=D0=94=D0=BE=D0=B1=D0=B0=D0=B2?=
 =?UTF-8?q?=D0=B8=D0=BB=20=D0=B2=D1=82=D0=BE=D1=80=D0=BE=D0=B9=20=D0=BC?=
 =?UTF-8?q?=D0=B5=D1=82=D0=BE=D0=B4=20=D0=BA=D0=BE=D0=BD=D1=82=D1=80=D0=BE?=
 =?UTF-8?q?=D0=BB=D0=BB=D0=B5=D1=80=D0=B0?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 composer.json                                 |  1 +
 composer.lock                                 |  2 +-
 src/Controller/RestaurantController.php       | 58 ++++++++++++++-----
 src/Mapper/RestaurantMapper.php               | 53 ++++++++++++++++-
 src/Model/RestaurantDetailElement.php         | 14 ++---
 .../RestaurantRepositoryInterface.php         |  8 ++-
 src/Repository/RestaurantRepository.php       | 27 ++++++++-
 src/Service/RestaurantService.php             | 28 +++++++--
 8 files changed, 161 insertions(+), 30 deletions(-)

diff --git a/composer.json b/composer.json
index fbf9fbc..72f280c 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 ec268ac..c4fecf9 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 d1dc2fc..9f665fc 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 53a7e12..5f9b6ae 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 face2bf..01e3e67 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 f5fa2f2..6bffa21 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 361489d..53c0561 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 d9f26f9..7e1866d 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
-- 
GitLab