From d2e016facd33136945c73c0ca3da7b95eea1b715 Mon Sep 17 00:00:00 2001 From: AlexP Date: Fri, 10 May 2024 07:48:16 +0500 Subject: [PATCH] STA-960 | controller&handle for request detail info --- app/src/Controller/AbstractController.php | 23 ++++++++++---- .../Controller/RestaurantDetailController.php | 24 +++++++++++++++ app/src/Dto/PaginationDto.php | 6 ++-- app/src/Entity/Restaurants.php | 12 ++++---- app/src/Error/AbstractError.php | 10 +++++++ app/src/Error/NotFoundError.php | 16 ++++++++++ .../RestaurantsRepositoryInterface.php | 6 ++++ app/src/Request/RestaurantDetailRequest.php | 25 ++++++++++++++++ app/src/Service/RestaurantDetailService.php | 30 +++++++++++++++++++ 9 files changed, 137 insertions(+), 15 deletions(-) create mode 100644 app/src/Controller/RestaurantDetailController.php create mode 100644 app/src/Error/AbstractError.php create mode 100644 app/src/Error/NotFoundError.php create mode 100644 app/src/Request/RestaurantDetailRequest.php create mode 100644 app/src/Service/RestaurantDetailService.php diff --git a/app/src/Controller/AbstractController.php b/app/src/Controller/AbstractController.php index 534d4dd..82b5969 100644 --- a/app/src/Controller/AbstractController.php +++ b/app/src/Controller/AbstractController.php @@ -3,13 +3,14 @@ namespace App\Controller; use App\Dto\ErrorDto; +use App\Error\AbstractError; use App\Request\AbstractRequest; use App\Service\ServiceInterface; use Symfony\Component\HttpFoundation\JsonResponse; use Throwable; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController as BundleController; -class AbstractController extends BundleController +abstract class AbstractController extends BundleController { public function __construct( private readonly ServiceInterface $service @@ -17,11 +18,21 @@ class AbstractController extends BundleController protected function build(AbstractRequest $request): JsonResponse { -// try { + try { return new JsonResponse($this->service->serve($request)); -// } catch (Throwable $exception) { -// $error = new ErrorDto(); -// return new JsonResponse($error, $error->status); -// } + + } catch (AbstractError $error) { + $errorDto = new ErrorDto(); + + $errorDto->message = $error->message; + $errorDto->code = $error->code; + $errorDto->status = $error->status; + + return new JsonResponse($errorDto, $error->status); + + } catch (Throwable $exception) { + $error = new ErrorDto(); + return new JsonResponse($error, $error->status); + } } } diff --git a/app/src/Controller/RestaurantDetailController.php b/app/src/Controller/RestaurantDetailController.php new file mode 100644 index 0000000..b297bac --- /dev/null +++ b/app/src/Controller/RestaurantDetailController.php @@ -0,0 +1,24 @@ +build($request); + } +} \ No newline at end of file diff --git a/app/src/Dto/PaginationDto.php b/app/src/Dto/PaginationDto.php index abfd5eb..a784dfd 100644 --- a/app/src/Dto/PaginationDto.php +++ b/app/src/Dto/PaginationDto.php @@ -10,12 +10,12 @@ class PaginationDto implements DtoInterface public function __construct(int $page, int $limit, int $total) { - $this->pages = ceil($total/$limit); + $this->page_size = $limit > 0 ? $limit : 12; + + $this->pages = ceil($total / $this->page_size); if (($page > 1) && ($page <= $this->pages)) { $this->current_page = $page; } - - $this->page_size = $limit; } } diff --git a/app/src/Entity/Restaurants.php b/app/src/Entity/Restaurants.php index ec816f7..a6208fe 100644 --- a/app/src/Entity/Restaurants.php +++ b/app/src/Entity/Restaurants.php @@ -441,9 +441,9 @@ class Restaurants implements PrototypeElementDto ->toArray() ); - $dto->phone = $this->decode($this->getPhone(), 'phone'); - $dto->email = $this->decode($this->getEmail(), 'email'); - $dto->address = $this->decode($this->getAddress(), 'address'); + $dto->phone = $this->decode($this->getPhone()); + $dto->email = $this->decode($this->getEmail()); + $dto->address = $this->decode($this->getAddress()); $dto->tags = $this->decodeTags($this->getTags()); $dto->site = $this->getSite(); $dto->image = $this->getPreviewImage()?->getDto(); @@ -463,19 +463,19 @@ class Restaurants implements PrototypeElementDto * @throws \JsonException Если получен неправильный json с бд * @return ?DtoCollection */ - private function decode(?string $jsonString, string $name): ?DtoCollection + private function decode(?string $jsonString): ?DtoCollection { if ($jsonString === null) { return null; } return new DtoCollection( - 'string|null', + 'string', json_decode( $jsonString, true, 512, JSON_THROW_ON_ERROR - )[$name] + ) ); } diff --git a/app/src/Error/AbstractError.php b/app/src/Error/AbstractError.php new file mode 100644 index 0000000..0827a2e --- /dev/null +++ b/app/src/Error/AbstractError.php @@ -0,0 +1,10 @@ +message = $message . ' ' . $this->message; + parent::__construct($this->message); + } +} diff --git a/app/src/Repository/RestaurantsRepositoryInterface.php b/app/src/Repository/RestaurantsRepositoryInterface.php index d2337ad..e5e2f2f 100644 --- a/app/src/Repository/RestaurantsRepositoryInterface.php +++ b/app/src/Repository/RestaurantsRepositoryInterface.php @@ -3,8 +3,14 @@ namespace App\Repository; use App\Entity\Restaurants; +use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; use Ramsey\Collection\Collection; +/** + * @extends ServiceEntityRepository + * + * @method Restaurants|null find($id, $lockMode = null, $lockVersion = null) + */ interface RestaurantsRepositoryInterface { public function getCountWithFilters(?string $kitchenId, string $typeId): int; diff --git a/app/src/Request/RestaurantDetailRequest.php b/app/src/Request/RestaurantDetailRequest.php new file mode 100644 index 0000000..05b07e0 --- /dev/null +++ b/app/src/Request/RestaurantDetailRequest.php @@ -0,0 +1,25 @@ +getRequest()->getUri(); + $index = strrpos($requestUrl, "/") + 1; + + $restaurantId = substr($requestUrl, $index); + + if (property_exists($this, "restaurantId")) { + $this->{"restaurantId"} = $restaurantId; + } + } +} diff --git a/app/src/Service/RestaurantDetailService.php b/app/src/Service/RestaurantDetailService.php new file mode 100644 index 0000000..5cc7111 --- /dev/null +++ b/app/src/Service/RestaurantDetailService.php @@ -0,0 +1,30 @@ +restaurants->find($request->restaurantId); + + if ($restaurant === null) { + throw new NotFoundError('Restaurant'); + } + + return $restaurant->getExtendedDto(); + } +} -- GitLab