feat: update StudentsExport and StudentService; inject User dependency and refine data retrieval methods
This commit is contained in:
parent
7ea4fb067d
commit
ceeb1e325d
@ -18,6 +18,7 @@ class StudentsExport implements FromCollection, ShouldAutoSize, WithEvents, With
|
|||||||
use StyledHeadingRow;
|
use StyledHeadingRow;
|
||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
|
private readonly User $user,
|
||||||
private readonly string $search = '',
|
private readonly string $search = '',
|
||||||
private readonly ?string $gender = null,
|
private readonly ?string $gender = null,
|
||||||
private readonly ?int $departmentId = null,
|
private readonly ?int $departmentId = null,
|
||||||
@ -32,7 +33,7 @@ public function title(): string
|
|||||||
|
|
||||||
public function collection(): Enumerable
|
public function collection(): Enumerable
|
||||||
{
|
{
|
||||||
return app(StudentService::class)->forExport($this->search, $this->gender, $this->departmentId, $this->status, $this->enrollmentYear);
|
return app(StudentService::class)->forExport($this->user, $this->search, $this->gender, $this->departmentId, $this->status, $this->enrollmentYear);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function headings(): array
|
public function headings(): array
|
||||||
|
|||||||
@ -31,6 +31,7 @@ public function index(PaginatedRequest $request): Response
|
|||||||
{
|
{
|
||||||
return Inertia::render('admin/users/students/index', [
|
return Inertia::render('admin/users/students/index', [
|
||||||
'students' => $this->service->paginated(
|
'students' => $this->service->paginated(
|
||||||
|
$request->user(),
|
||||||
...$request->validatedWithDefaults(),
|
...$request->validatedWithDefaults(),
|
||||||
gender: $request->validated('gender'),
|
gender: $request->validated('gender'),
|
||||||
departmentId: $request->validated('department_id'),
|
departmentId: $request->validated('department_id'),
|
||||||
@ -112,6 +113,7 @@ public function updateUserStatus(UpdateStatusRequest $request, User $user): Redi
|
|||||||
public function export(PaginatedRequest $request): BinaryFileResponse
|
public function export(PaginatedRequest $request): BinaryFileResponse
|
||||||
{
|
{
|
||||||
return Excel::download(new StudentsExport(
|
return Excel::download(new StudentsExport(
|
||||||
|
user: $request->user(),
|
||||||
search: $request->validated('search') ?? '',
|
search: $request->validated('search') ?? '',
|
||||||
gender: $request->validated('gender'),
|
gender: $request->validated('gender'),
|
||||||
departmentId: $request->validated('department_id'),
|
departmentId: $request->validated('department_id'),
|
||||||
|
|||||||
@ -14,9 +14,9 @@
|
|||||||
|
|
||||||
class StudentService
|
class StudentService
|
||||||
{
|
{
|
||||||
public function paginated(int $perPage = 25, string $search = '', ?string $gender = null, ?int $departmentId = null, ?string $status = null, ?int $enrollmentYear = null): LengthAwarePaginator
|
public function paginated(User $user, int $perPage = 25, string $search = '', ?string $gender = null, ?int $departmentId = null, ?string $status = null, ?int $enrollmentYear = null): LengthAwarePaginator
|
||||||
{
|
{
|
||||||
return $this->filteredQuery($search, $gender, $departmentId, $status, $enrollmentYear)
|
return $this->filteredQuery($user, $search, $gender, $departmentId, $status, $enrollmentYear)
|
||||||
->latest()
|
->latest()
|
||||||
->paginate($perPage, ['id', 'username', 'email', 'is_active']);
|
->paginate($perPage, ['id', 'username', 'email', 'is_active']);
|
||||||
}
|
}
|
||||||
@ -109,9 +109,9 @@ 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, ?string $status = null, ?int $enrollmentYear = null): Collection
|
public function forExport(User $user, string $search = '', ?string $gender = null, ?int $departmentId = null, ?string $status = null, ?int $enrollmentYear = null): Collection
|
||||||
{
|
{
|
||||||
return $this->filteredQuery($search, $gender, $departmentId, $status, $enrollmentYear)
|
return $this->filteredQuery($user, $search, $gender, $departmentId, $status, $enrollmentYear)
|
||||||
->orderBy('created_at', 'desc')
|
->orderBy('created_at', 'desc')
|
||||||
->get(['id', 'username', 'email', 'is_active']);
|
->get(['id', 'username', 'email', 'is_active']);
|
||||||
}
|
}
|
||||||
@ -142,14 +142,19 @@ public function getEnrollmentYears(): array
|
|||||||
->all();
|
->all();
|
||||||
}
|
}
|
||||||
|
|
||||||
private function filteredQuery(string $search, ?string $gender, ?int $departmentId, ?string $status, ?int $enrollmentYear = null): Builder
|
private function filteredQuery(User $user, string $search, ?string $gender, ?int $departmentId, ?string $status, ?int $enrollmentYear = null): Builder
|
||||||
{
|
{
|
||||||
|
$taughtDepartmentIds = $user->hasRole(UserRole::Dosen->value)
|
||||||
|
? $user->lecturer?->departments()->pluck('departments.id')->all() ?? []
|
||||||
|
: null;
|
||||||
|
|
||||||
return User::with([
|
return User::with([
|
||||||
'profile:id,user_id,full_name,phone_number,gender,birth_place,birth_date,address',
|
'profile:id,user_id,full_name,phone_number,gender,birth_place,birth_date,address',
|
||||||
'student:id,user_id,student_number,department_id,enrollment_year,current_semester,status',
|
'student:id,user_id,student_number,department_id,enrollment_year,current_semester,status',
|
||||||
'student.department:id,name',
|
'student.department:id,name',
|
||||||
])
|
])
|
||||||
->whereHas('roles', fn ($q) => $q->where('name', UserRole::Mahasiswa->value))
|
->whereHas('roles', fn ($q) => $q->where('name', UserRole::Mahasiswa->value))
|
||||||
|
->when($taughtDepartmentIds !== null, fn ($q) => $q->whereHas('student', fn ($q) => $q->whereIn('department_id', $taughtDepartmentIds)))
|
||||||
->when($search, fn ($q) => $q->where(function ($query) use ($search) {
|
->when($search, fn ($q) => $q->where(function ($query) use ($search) {
|
||||||
$query->where('username', 'like', "%{$search}%")
|
$query->where('username', 'like', "%{$search}%")
|
||||||
->orWhere('email', 'like', "%{$search}%")
|
->orWhere('email', 'like', "%{$search}%")
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user