Skip to content
This repository was archived by the owner on May 13, 2025. It is now read-only.

Commit 5b391c8

Browse files
committed
feat: get cities by code route + refactoring
1 parent 530f392 commit 5b391c8

10 files changed

Lines changed: 182 additions & 51 deletions

File tree

app/Console/Commands/SyncKitGeography.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@ public function handle(): void
2020
UpdateKitGeographyData::dispatchSync();
2121
$this->info('Synchronization completed');
2222
}
23-
}
23+
}

app/DTO/CityByCodeDTO.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\DTO;
6+
7+
use service\KitAPI\Model\Request\Geography\GetListCityRequest;
8+
9+
class CityByCodeDTO
10+
{
11+
private string $code;
12+
13+
public function __construct(string $code)
14+
{
15+
$this->code = $code;
16+
}
17+
18+
public function toGetListCityRequest(): GetListCityRequest
19+
{
20+
return new GetListCityRequest($this->code);
21+
}
22+
}

app/DTO/KitTerminalsRequestDTO.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\DTO;
6+
7+
use service\KitAPI\Model\Request\Geography\GetListAddressRequest;
8+
9+
class KitTerminalsRequestDTO
10+
{
11+
private ?string $cityId;
12+
private bool $withPhone;
13+
private bool $withEmail;
14+
15+
public function __construct(?string $cityId = null, bool $withPhone = true, bool $withEmail = true)
16+
{
17+
$this->cityId = $cityId;
18+
$this->withPhone = $withPhone;
19+
$this->withEmail = $withEmail;
20+
}
21+
22+
public function toGetListAddressRequest(): GetListAddressRequest
23+
{
24+
if ($this->cityId) {
25+
return new GetListAddressRequest($this->cityId, $this->withPhone, $this->withEmail);
26+
}
27+
28+
return new GetListAddressRequest(withPhone: $this->withPhone, withEmail: $this->withEmail);
29+
}
30+
}

app/DTO/SearchCitiesDTO.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\DTO;
6+
7+
use service\KitAPI\Model\Request\Tdd\SearchByNameRequest;
8+
9+
class SearchCitiesDTO
10+
{
11+
private string $query;
12+
13+
public function __construct(string $query)
14+
{
15+
$this->query = $query;
16+
}
17+
18+
public function toSearchByNameRequest(): SearchByNameRequest
19+
{
20+
return new SearchByNameRequest($this->query);
21+
}
22+
}

app/Http/Controllers/DeliveryController.php

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,30 @@
44

55
namespace App\Http\Controllers;
66

7+
use App\DTO\CityByCodeDTO;
8+
use App\DTO\DeliveryCalculationDTO;
9+
use App\DTO\KitTerminalsRequestDTO;
10+
use App\DTO\SearchCitiesDTO;
711
use App\Services\KitDeliveryService;
812
use Illuminate\Http\JsonResponse;
913
use Illuminate\Http\Request;
10-
use Psr\Http\Client\ClientExceptionInterface;
1114
use Symfony\Component\HttpFoundation\Response;
1215

1316
readonly class DeliveryController
1417
{
1518
public function __construct(
1619
private KitDeliveryService $kitService
17-
) {}
20+
) {
21+
}
1822

1923
/**
2024
* @return JsonResponse
21-
* @throws ClientExceptionInterface
2225
*/
2326
public function getAllTerminals(): JsonResponse
2427
{
2528
try {
26-
$terminals = $this->kitService->getTerminals();
29+
$dto = new KitTerminalsRequestDTO();
30+
$terminals = $this->kitService->getTerminals($dto);
2731
return response()->json($terminals, Response::HTTP_OK);
2832
} catch (\Exception $e) {
2933
return response()->json([
@@ -35,12 +39,12 @@ public function getAllTerminals(): JsonResponse
3539
/**
3640
* @param string $cityId
3741
* @return JsonResponse
38-
* @throws ClientExceptionInterface
3942
*/
4043
public function getCityTerminals(string $cityId): JsonResponse
4144
{
4245
try {
43-
$terminals = $this->kitService->getTerminals($cityId);
46+
$dto = new KitTerminalsRequestDTO($cityId);
47+
$terminals = $this->kitService->getTerminals($dto);
4448
return response()->json($terminals, Response::HTTP_OK);
4549
} catch (\Exception $e) {
4650
return response()->json([
@@ -52,12 +56,12 @@ public function getCityTerminals(string $cityId): JsonResponse
5256
/**
5357
* @param Request $request
5458
* @return JsonResponse
55-
* @throws ClientExceptionInterface
5659
*/
5760
public function calculateDelivery(Request $request): JsonResponse
5861
{
5962
try {
60-
$result = $this->kitService->calculateDelivery($request->all());
63+
$dto = DeliveryCalculationDTO::fromArray($request->all());
64+
$result = $this->kitService->calculateDelivery($dto);
6165
return response()->json([
6266
'data' => $result
6367
], Response::HTTP_OK);
@@ -71,7 +75,6 @@ public function calculateDelivery(Request $request): JsonResponse
7175
/**
7276
* @param Request $request
7377
* @return JsonResponse
74-
* @throws ClientExceptionInterface
7578
*/
7679
public function searchCities(Request $request): JsonResponse
7780
{
@@ -82,12 +85,30 @@ public function searchCities(Request $request): JsonResponse
8285
}
8386

8487
try {
85-
$cities = $this->kitService->searchCitiesByName($request->get('query'));
88+
$dto = new SearchCitiesDTO($request->get('query'));
89+
$cities = $this->kitService->searchCitiesByName($dto);
8690
return response()->json($cities, Response::HTTP_OK);
8791
} catch (\Exception $e) {
8892
return response()->json([
8993
'message' => $e->getMessage()
9094
], Response::HTTP_INTERNAL_SERVER_ERROR);
9195
}
9296
}
93-
}
97+
98+
/**
99+
* @param string $code
100+
* @return JsonResponse
101+
*/
102+
public function getCityByCode(string $code): JsonResponse
103+
{
104+
try {
105+
$dto = new CityByCodeDTO($code);
106+
$city = $this->kitService->getCityByCode($dto);
107+
return response()->json($city, Response::HTTP_OK);
108+
} catch (\Exception $e) {
109+
return response()->json([
110+
'message' => $e->getMessage()
111+
], Response::HTTP_INTERNAL_SERVER_ERROR);
112+
}
113+
}
114+
}

app/Http/Controllers/TerminalController.php

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,26 @@
44

55
namespace App\Http\Controllers;
66

7+
use App\Http\Requests\TerminalSearchRequest;
78
use App\Repositories\TerminalRepository;
89
use Illuminate\Http\JsonResponse;
9-
use Illuminate\Http\Request;
1010
use Symfony\Component\HttpFoundation\Response;
1111

1212
readonly class TerminalController
1313
{
1414
public function __construct(
1515
private TerminalRepository $repository
16-
) {}
16+
) {
17+
}
1718

1819
/**
19-
* @param Request $request
20+
* @param TerminalSearchRequest $request
2021
* @return JsonResponse
2122
*/
22-
public function search(Request $request): JsonResponse
23+
public function search(TerminalSearchRequest $request): JsonResponse
2324
{
24-
$validated = $request->validate([
25-
'query' => 'required|string|min:2'
26-
]);
27-
2825
try {
29-
$terminals = $this->repository->search($validated['query']);
26+
$terminals = $this->repository->search($request->validated('query'));
3027
return response()->json([
3128
'data' => $terminals
3229
], Response::HTTP_OK);
@@ -36,4 +33,4 @@ public function search(Request $request): JsonResponse
3633
], Response::HTTP_UNPROCESSABLE_ENTITY);
3734
}
3835
}
39-
}
36+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Http\Requests;
6+
7+
use Illuminate\Foundation\Http\FormRequest;
8+
9+
class TerminalSearchRequest extends FormRequest
10+
{
11+
public function rules(): array
12+
{
13+
return [
14+
'query' => 'required|string|min:2'
15+
];
16+
}
17+
18+
public function authorize(): bool
19+
{
20+
return true;
21+
}
22+
}

app/Jobs/UpdateKitGeographyData.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace App\Jobs;
66

7+
use App\DTO\KitTerminalsRequestDTO;
78
use App\Repositories\TerminalRepository;
89
use App\Services\KitDeliveryService;
910
use Illuminate\Bus\Queueable;
@@ -27,7 +28,8 @@ public function handle(KitDeliveryService $kitService, TerminalRepository $termi
2728

2829
$start = microtime(true);
2930

30-
$terminals = $kitService->getTerminals();
31+
$dto = new KitTerminalsRequestDTO();
32+
$terminals = $kitService->getTerminals($dto);
3133
Log::info('Retrieved ' . count($terminals) . ' terminals from API');
3234

3335
if (count($terminals) === 0) {
@@ -68,4 +70,4 @@ public function handle(KitDeliveryService $kitService, TerminalRepository $termi
6870
Log::error($e->getTraceAsString());
6971
}
7072
}
71-
}
73+
}

0 commit comments

Comments
 (0)