CityRepository.php 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. <?php
  2. namespace App\Repositories;
  3. use App\Models\City;
  4. use App\DTO\CityDTO;
  5. use Illuminate\Database\Eloquent\Collection;
  6. class CityRepository implements CityRepositoryInterface
  7. {
  8. public function __construct(
  9. protected City $model
  10. ) {}
  11. public function all(): Collection
  12. {
  13. return $this->model->with('state', 'country')->get();
  14. }
  15. public function find(int $id): ?City
  16. {
  17. return $this->model->with('state', 'country')->find($id);
  18. }
  19. public function create(CityDTO $dto): City
  20. {
  21. return $this->model->create($dto->toArray());
  22. }
  23. public function update(int $id, CityDTO $dto, array $fieldsToUpdate): City
  24. {
  25. $record = $this->find($id);
  26. $updateFields = array_intersect_key(
  27. $dto->toArray(),
  28. array_flip($fieldsToUpdate)
  29. );
  30. $record->update($updateFields);
  31. return $record->fresh();
  32. }
  33. public function delete(int $id): bool
  34. {
  35. return $this->model->destroy($id) > 0;
  36. }
  37. }