refactor: streamline LecturerService methods and enhance role assignment with enum
This commit is contained in:
parent
ad5fe33ec6
commit
8f9eb5f77a
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
namespace App\Services\Admin\Users;
|
namespace App\Services\Admin\Users;
|
||||||
|
|
||||||
|
use App\Enums\UserRole;
|
||||||
use App\Models\Lecturer;
|
use App\Models\Lecturer;
|
||||||
use App\Models\User;
|
use App\Models\User;
|
||||||
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
|
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
|
||||||
@ -12,51 +13,11 @@
|
|||||||
|
|
||||||
class LecturerService
|
class LecturerService
|
||||||
{
|
{
|
||||||
public function getAllForSelect(): Collection
|
|
||||||
{
|
|
||||||
return Lecturer::select(['lecturers.id', 'lecturers.user_id', 'lecturers.lecturer_number'])
|
|
||||||
->join('users', 'users.id', '=', 'lecturers.user_id')
|
|
||||||
->join('user_profiles', 'user_profiles.user_id', '=', 'users.id')
|
|
||||||
->with([
|
|
||||||
'user:id,username',
|
|
||||||
'user.profile:id,user_id,full_name',
|
|
||||||
'departments:id,name',
|
|
||||||
])
|
|
||||||
->orderBy('user_profiles.full_name')
|
|
||||||
->get();
|
|
||||||
}
|
|
||||||
|
|
||||||
public function paginated(int $perPage = 25, string $search = '', ?string $gender = null, ?int $departmentId = null): LengthAwarePaginator
|
public function paginated(int $perPage = 25, string $search = '', ?string $gender = null, ?int $departmentId = null): LengthAwarePaginator
|
||||||
{
|
{
|
||||||
return $this->filteredQuery($search, $gender, $departmentId)
|
return $this->filteredQuery($search, $gender, $departmentId)
|
||||||
->latest()
|
->latest()
|
||||||
->paginate($perPage);
|
->paginate($perPage, ['id', 'username', 'email', 'is_active']);
|
||||||
}
|
|
||||||
|
|
||||||
public function forExport(string $search = '', ?string $gender = null, ?int $departmentId = null): Collection
|
|
||||||
{
|
|
||||||
return $this->filteredQuery($search, $gender, $departmentId)
|
|
||||||
->orderBy('created_at', 'desc')
|
|
||||||
->get();
|
|
||||||
}
|
|
||||||
|
|
||||||
private function filteredQuery(string $search, ?string $gender, ?int $departmentId): Builder
|
|
||||||
{
|
|
||||||
return User::select(['id', 'username', 'email', 'is_active'])
|
|
||||||
->with([
|
|
||||||
'profile:id,user_id,full_name,phone_number,gender,birth_place,birth_date,address',
|
|
||||||
'lecturer:id,user_id,lecturer_number',
|
|
||||||
'lecturer.departments:id,name',
|
|
||||||
])
|
|
||||||
->whereHas('roles', fn ($q) => $q->where('name', 'dosen'))
|
|
||||||
->when($search, fn ($q) => $q->where(function ($query) use ($search) {
|
|
||||||
$query->where('username', 'like', "%{$search}%")
|
|
||||||
->orWhere('email', 'like', "%{$search}%")
|
|
||||||
->orWhereHas('profile', fn ($q) => $q->where('full_name', 'like', "%{$search}%"))
|
|
||||||
->orWhereHas('lecturer', fn ($q) => $q->where('lecturer_number', 'like', "%{$search}%"));
|
|
||||||
}))
|
|
||||||
->when($gender, fn ($q) => $q->whereHas('profile', fn ($q) => $q->where('gender', $gender)))
|
|
||||||
->when($departmentId, fn ($q) => $q->whereHas('lecturer.departments', fn ($q) => $q->where('departments.id', $departmentId)));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function create(array $data): User
|
public function create(array $data): User
|
||||||
@ -68,7 +29,7 @@ public function create(array $data): User
|
|||||||
'password' => Hash::make(config('app.default_password')),
|
'password' => Hash::make(config('app.default_password')),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$user->assignRole('dosen');
|
$user->assignRole(UserRole::Dosen->value);
|
||||||
|
|
||||||
$user->profile()->create([
|
$user->profile()->create([
|
||||||
'full_name' => $data['full_name'],
|
'full_name' => $data['full_name'],
|
||||||
@ -136,4 +97,42 @@ public function updateUserStatus(User $user, bool $isActive): void
|
|||||||
{
|
{
|
||||||
$user->update(['is_active' => $isActive]);
|
$user->update(['is_active' => $isActive]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function forExport(string $search = '', ?string $gender = null, ?int $departmentId = null): Collection
|
||||||
|
{
|
||||||
|
return $this->filteredQuery($search, $gender, $departmentId)
|
||||||
|
->orderBy('created_at', 'desc')
|
||||||
|
->get(['id', 'username', 'email', 'is_active']);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getAllForSelect(): Collection
|
||||||
|
{
|
||||||
|
return Lecturer::join('users', 'users.id', '=', 'lecturers.user_id')
|
||||||
|
->join('user_profiles', 'user_profiles.user_id', '=', 'users.id')
|
||||||
|
->with([
|
||||||
|
'user:id,username',
|
||||||
|
'user.profile:id,user_id,full_name',
|
||||||
|
'departments:id,name',
|
||||||
|
])
|
||||||
|
->orderBy('user_profiles.full_name')
|
||||||
|
->get(['lecturers.id', 'lecturers.user_id', 'lecturers.lecturer_number']);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function filteredQuery(string $search, ?string $gender, ?int $departmentId): Builder
|
||||||
|
{
|
||||||
|
return User::with([
|
||||||
|
'profile:id,user_id,full_name,phone_number,gender,birth_place,birth_date,address',
|
||||||
|
'lecturer:id,user_id,lecturer_number',
|
||||||
|
'lecturer.departments:id,name',
|
||||||
|
])
|
||||||
|
->whereHas('roles', fn ($q) => $q->where('name', UserRole::Dosen->value))
|
||||||
|
->when($search, fn ($q) => $q->where(function ($query) use ($search) {
|
||||||
|
$query->where('username', 'like', "%{$search}%")
|
||||||
|
->orWhere('email', 'like', "%{$search}%")
|
||||||
|
->orWhereHas('profile', fn ($q) => $q->where('full_name', 'like', "%{$search}%"))
|
||||||
|
->orWhereHas('lecturer', fn ($q) => $q->where('lecturer_number', 'like', "%{$search}%"));
|
||||||
|
}))
|
||||||
|
->when($gender, fn ($q) => $q->whereHas('profile', fn ($q) => $q->where('gender', $gender)))
|
||||||
|
->when($departmentId, fn ($q) => $q->whereHas('lecturer.departments', fn ($q) => $q->where('departments.id', $departmentId)));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user