Loading app/src/Controller/AbstractController.php +17 −6 Original line number Diff line number Diff line Loading @@ -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 Loading @@ -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); } } } app/src/Controller/RestaurantDetailController.php 0 → 100644 +24 −0 Original line number Diff line number Diff line <?php namespace App\Controller; use App\Request\RestaurantDetailRequest; use App\Service\RestaurantDetailService; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\Routing\Attribute\Route; #[Route('/api/v1/restaurants/')] class RestaurantDetailController extends AbstractController { public function __construct(RestaurantDetailService $service) { parent::__construct($service); } /** Листинг ресторанов */ #[Route('{restaurantId}', name: 'restaurant')] public function index(RestaurantDetailRequest $request): JsonResponse { return $this->build($request); } } No newline at end of file app/src/Dto/PaginationDto.php +3 −3 Original line number Diff line number Diff line Loading @@ -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; } } app/src/Entity/Restaurants.php +6 −6 Original line number Diff line number Diff line Loading @@ -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(); Loading @@ -463,19 +463,19 @@ class Restaurants implements PrototypeElementDto * @throws \JsonException Если получен неправильный json с бд * @return ?DtoCollection<string> */ 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] ) ); } Loading app/src/Error/AbstractError.php 0 → 100644 +10 −0 Original line number Diff line number Diff line <?php namespace App\Error; class AbstractError extends \Exception { public string $status = '500'; public $message = ''; public $code = ''; } Loading
app/src/Controller/AbstractController.php +17 −6 Original line number Diff line number Diff line Loading @@ -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 Loading @@ -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); } } }
app/src/Controller/RestaurantDetailController.php 0 → 100644 +24 −0 Original line number Diff line number Diff line <?php namespace App\Controller; use App\Request\RestaurantDetailRequest; use App\Service\RestaurantDetailService; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\Routing\Attribute\Route; #[Route('/api/v1/restaurants/')] class RestaurantDetailController extends AbstractController { public function __construct(RestaurantDetailService $service) { parent::__construct($service); } /** Листинг ресторанов */ #[Route('{restaurantId}', name: 'restaurant')] public function index(RestaurantDetailRequest $request): JsonResponse { return $this->build($request); } } No newline at end of file
app/src/Dto/PaginationDto.php +3 −3 Original line number Diff line number Diff line Loading @@ -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; } }
app/src/Entity/Restaurants.php +6 −6 Original line number Diff line number Diff line Loading @@ -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(); Loading @@ -463,19 +463,19 @@ class Restaurants implements PrototypeElementDto * @throws \JsonException Если получен неправильный json с бд * @return ?DtoCollection<string> */ 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] ) ); } Loading
app/src/Error/AbstractError.php 0 → 100644 +10 −0 Original line number Diff line number Diff line <?php namespace App\Error; class AbstractError extends \Exception { public string $status = '500'; public $message = ''; public $code = ''; }