| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- <?php
- namespace App\Repositories;
- use App\Models\User;
- use Illuminate\Database\Eloquent\Collection;
- use App\DTO\UserDTO;
- use Illuminate\Support\Facades\Auth;
- class UserRepository implements UserRepositoryInterface
- {
- public function __construct(
- protected User $model,
- ) {}
- public function me(): ?User
- {
- return Auth::user();
- }
- public function all(): ?Collection
- {
- return $this->model->all();
- }
- public function store(UserDTO $userDTO): User
- {
- return $this->model->create($userDTO->toArray());
- }
- public function update(int $id, UserDTO $dto, array $fieldsToUpdate): User
- {
- $user = User::findOrFail($id);
- $updateFields = array_intersect_key(
- $dto->toArray(),
- array_flip($fieldsToUpdate)
- );
- $user->update($updateFields);
- return $user->fresh();
- }
- public function delete(int $id): bool
- {
- return $this->model->destroy(ids: $id);
- }
- public function find(int $id): ?User
- {
- return $this->model->findOrFail(id: $id);
- }
- public function findByEmail(string $email): ?User
- {
- return $this->model->where(column: 'email', operator: $email)->first();
- }
- }
|