| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- <?php
- namespace App\Repositories;
- use App\Models\User;
- use Illuminate\Database\Eloquent\Collection;
- use App\DTO\UserDTO;
- use App\DTO\UserLanguageDTO;
- 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(attributes: [
- 'name' => $userDTO->name,
- 'email' => $userDTO->email,
- 'password' => bcrypt(value: $userDTO->password),
- 'type' => $userDTO->type,
- ]);
- }
- public function update(UserDTO $userDTO, int $id): ?User
- {
- $user = $this->model->findOrFail(id: $id);
- $user->update(attributes: [
- 'name' => $userDTO->name,
- 'email' => $userDTO->email,
- 'password' => bcrypt(value: $userDTO->password),
- 'type' => $userDTO->type,
- ]);
- return $user;
- }
- 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();
- }
- public function updateLanguage(UserLanguageDTO $languageDTO, int $id): ?User
- {
- $user = $this->model->findOrFail(id: $id);
- $user->update(attributes: [
- 'language' => $languageDTO->language,
- ]);
- return $user;
- }
- }
|