From 6eddb018589eaf98002e59a12e42517c0f6c7816 Mon Sep 17 00:00:00 2001 From: Timofey Date: Wed, 7 May 2025 14:37:19 +0500 Subject: [PATCH] fix: get cities by code refactoring --- app/DTO/CityByCodeDTO.php | 32 +++++++++++++++++---- app/Http/Controllers/DeliveryController.php | 6 ++-- app/Models/Terminal.php | 2 +- app/Repositories/TerminalRepository.php | 4 +-- routes/api.php | 2 +- 5 files changed, 34 insertions(+), 12 deletions(-) diff --git a/app/DTO/CityByCodeDTO.php b/app/DTO/CityByCodeDTO.php index c0349db..c5b9949 100644 --- a/app/DTO/CityByCodeDTO.php +++ b/app/DTO/CityByCodeDTO.php @@ -8,15 +8,37 @@ class CityByCodeDTO { - private string $code; + private ?int $id; + private ?int $tdd_city_code; - public function __construct(string $code) + public function __construct(?int $tdd_city_code = null, ?int $id = null) { - $this->code = $code; + $this->tdd_city_code = $tdd_city_code; + $this->id = $id; + } + + public static function fromId(int $id): self + { + return new self(null, $id); + } + + public static function fromCode(int $code): self + { + return new self($code); } public function toGetListCityRequest(): GetListCityRequest { - return new GetListCityRequest($this->code); + $params = []; + + if ($this->id !== null) { + $params['id'] = $this->id; + } + + if ($this->tdd_city_code !== null) { + $params['tdd_city_code'] = (string)$this->tdd_city_code; + } + + return new GetListCityRequest(...$params); } -} +} \ No newline at end of file diff --git a/app/Http/Controllers/DeliveryController.php b/app/Http/Controllers/DeliveryController.php index 75b9ca8..b58c44e 100644 --- a/app/Http/Controllers/DeliveryController.php +++ b/app/Http/Controllers/DeliveryController.php @@ -96,13 +96,13 @@ public function searchCities(Request $request): JsonResponse } /** - * @param string $code + * @param int $tdd_city_code * @return JsonResponse */ - public function getCityByCode(string $code): JsonResponse + public function getCityByCode(int $tdd_city_code): JsonResponse { try { - $dto = new CityByCodeDTO($code); + $dto = new CityByCodeDTO($tdd_city_code); $city = $this->kitService->getCityByCode($dto); return response()->json($city, Response::HTTP_OK); } catch (\Exception $e) { diff --git a/app/Models/Terminal.php b/app/Models/Terminal.php index c895958..d3287f8 100644 --- a/app/Models/Terminal.php +++ b/app/Models/Terminal.php @@ -24,4 +24,4 @@ class Terminal extends Model 'email', 'value', ]; -} \ No newline at end of file +} diff --git a/app/Repositories/TerminalRepository.php b/app/Repositories/TerminalRepository.php index 3760d25..8961706 100644 --- a/app/Repositories/TerminalRepository.php +++ b/app/Repositories/TerminalRepository.php @@ -29,7 +29,7 @@ public function search(?string $query = null): array $allTerminals = $this->getAllTerminals(); - return array_filter($allTerminals, function($terminal) use ($query) { + return array_filter($allTerminals, function ($terminal) use ($query) { $query = strtolower($query); return stripos(strtolower($terminal['city_name'] ?? ''), $query) !== false || stripos(strtolower($terminal['address_code'] ?? ''), $query) !== false; @@ -98,4 +98,4 @@ public function clearCache(): void Log::info('Кеш терминалов полностью очищен, удалено ' . count($keys) . ' ключей'); } -} \ No newline at end of file +} diff --git a/routes/api.php b/routes/api.php index ab991b1..6b54a7c 100644 --- a/routes/api.php +++ b/routes/api.php @@ -11,4 +11,4 @@ Route::get('/cities/code/{code}', [DeliveryController::class, 'getCityByCode']); Route::get('/terminals/search', [TerminalController::class, 'search']); Route::get('/terminals', [DeliveryController::class, 'getAllTerminals']); -}); \ No newline at end of file +});