|
@@ -5,25 +5,31 @@
|
|
|
use App\Models\City;
|
|
use App\Models\City;
|
|
|
use App\DTO\CityDTO;
|
|
use App\DTO\CityDTO;
|
|
|
use Illuminate\Database\Eloquent\Collection;
|
|
use Illuminate\Database\Eloquent\Collection;
|
|
|
|
|
+use Illuminate\Support\Facades\Cache;
|
|
|
|
|
|
|
|
class CityRepository implements CityRepositoryInterface
|
|
class CityRepository implements CityRepositoryInterface
|
|
|
{
|
|
{
|
|
|
public function __construct(
|
|
public function __construct(
|
|
|
- protected City $model
|
|
|
|
|
|
|
+ protected City $model,
|
|
|
|
|
+ readonly protected string $cacheKey = 'cities.all',
|
|
|
|
|
+ readonly protected int $cacheTtl = 2592000 // one month in seconds
|
|
|
) {}
|
|
) {}
|
|
|
|
|
|
|
|
public function all(): Collection
|
|
public function all(): Collection
|
|
|
{
|
|
{
|
|
|
- return $this->model->with('state', 'country')->get();
|
|
|
|
|
|
|
+ return Cache::remember($this->cacheKey, $this->cacheTtl, function () {
|
|
|
|
|
+ return $this->model->with('state:id,name', 'country:id,name')->get();
|
|
|
|
|
+ });
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public function find(int $id): ?City
|
|
public function find(int $id): ?City
|
|
|
{
|
|
{
|
|
|
- return $this->model->with('state', 'country')->find($id);
|
|
|
|
|
|
|
+ return $this->model->with('state:id,name', 'country:id,name')->find($id);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public function create(CityDTO $dto): City
|
|
public function create(CityDTO $dto): City
|
|
|
{
|
|
{
|
|
|
|
|
+ Cache::forget($this->cacheKey);
|
|
|
return $this->model->create($dto->toArray());
|
|
return $this->model->create($dto->toArray());
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -37,12 +43,13 @@ public function update(int $id, CityDTO $dto, array $fieldsToUpdate): City
|
|
|
);
|
|
);
|
|
|
|
|
|
|
|
$record->update($updateFields);
|
|
$record->update($updateFields);
|
|
|
-
|
|
|
|
|
|
|
+ Cache::forget($this->cacheKey);
|
|
|
return $record->fresh();
|
|
return $record->fresh();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public function delete(int $id): bool
|
|
public function delete(int $id): bool
|
|
|
{
|
|
{
|
|
|
|
|
+ Cache::forget($this->cacheKey);
|
|
|
return $this->model->destroy($id) > 0;
|
|
return $this->model->destroy($id) > 0;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|