| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- <?php
- namespace App\Services;
- use App\Enums\UserTypeEnum;
- use App\Models\User;
- use Illuminate\Database\Eloquent\Collection;
- use Illuminate\Support\Facades\Auth;
- class UserService
- {
- public function authUser(): ?User
- {
- $user = Auth::user();
- return $user;
- }
- public function getAll(): Collection
- {
- return User::with(['position', 'sector'])->orderBy("created_at", "desc")->get();
- }
- public function getAllPaginated(array $filters = [], int $perPage = 10): \Illuminate\Pagination\LengthAwarePaginator
- {
- $query = User::with(['position', 'sector'])->orderBy('created_at', 'desc');
- if (!empty($filters['type'])) {
- $query->where('type', $filters['type']);
- }
- if (!empty($filters['status'])) {
- $query->where('status', $filters['status']);
- }
- if (!empty($filters['search'])) {
- $search = $filters['search'];
- $query->where(function ($q) use ($search) {
- $q->where('name', 'like', "%{$search}%")
- ->orWhere('email', 'like', "%{$search}%")
- ->orWhere('cpf', 'like', "%{$search}%")
- ->orWhere('registration', 'like', "%{$search}%");
- });
- }
- return $query->paginate($perPage);
- }
- public function findById(int $id): ?User
- {
- return User::find($id);
- }
- public function create(array $data): User
- {
- return User::create($data);
- }
- public function update(int $id, array $data): ?User
- {
- $model = $this->findById($id);
- if (!$model) {
- return null;
- }
- $model->update($data);
- return $model->fresh();
- }
- public function delete(int $id): bool
- {
- $model = $this->findById($id);
- if (!$model) {
- return false;
- }
- return $model->delete();
- }
- public function getUserTypes(): array
- {
- return UserTypeEnum::toArray();
- }
- }
|