siakad-itm/app/Http/Controllers/Admin/Users/LecturerController.php
Yoga Pangestu 07a00a92a2
Some checks failed
tests / ci (pull_request) Has been cancelled
Refactor lecturer management to support multiple departments
- Updated LecturerController to load multiple departments for lecturers.
- Modified LecturerRequest to accept an array of department IDs instead of a single department ID.
- Changed Department model to establish a many-to-many relationship with Lecturer.
- Adjusted Lecturer model to reflect the new many-to-many relationship with Department.
- Updated LecturerService to handle multiple departments during creation and updates.
- Created LecturerFactory and StudentFactory for generating test data.
- Added a migration for the new lecturer_department pivot table.
- Refactored seeder classes to accommodate the new structure for lecturers and departments.
- Enhanced frontend components for creating and editing lecturers to support multiple department selection.
2026-08-30 12:18:13 +07:00

104 lines
3.4 KiB
PHP

<?php
namespace App\Http\Controllers\Admin\Users;
use App\Exports\LecturersExport;
use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\UpdateStatusRequest;
use App\Http\Requests\Admin\Users\LecturerRequest;
use App\Http\Requests\PaginatedRequest;
use App\Models\User;
use App\Services\Admin\Master\DepartmentService;
use App\Services\Admin\Users\LecturerService;
use Illuminate\Http\RedirectResponse;
use Inertia\Inertia;
use Inertia\Response;
use Maatwebsite\Excel\Facades\Excel;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
class LecturerController extends Controller
{
public function __construct(
private readonly LecturerService $service,
private readonly DepartmentService $departmentService,
) {}
public function index(PaginatedRequest $request): Response
{
return Inertia::render('admin/users/lecturers/index', [
'lecturers' => $this->service->paginated(
...$request->validatedWithDefaults(),
gender: $request->validated('gender'),
departmentId: $request->validated('department_id'),
),
'departments' => $this->departmentService->getAllForSelect(),
'filters' => $request->only(['gender', 'department_id']),
]);
}
public function create(): Response
{
return Inertia::render('admin/users/lecturers/create', [
'departments' => $this->departmentService->getAllForSelect(),
]);
}
public function store(LecturerRequest $request): RedirectResponse
{
$this->service->create($request->validated());
Inertia::flash('toast', ['type' => 'success', 'message' => 'Dosen berhasil ditambahkan.']);
return to_route('admin.users.lecturers.index');
}
public function edit(User $user): Response
{
$user->load(['profile', 'lecturer.departments']);
return Inertia::render('admin/users/lecturers/edit', [
'user' => $user,
'departments' => $this->departmentService->getAllForSelect(),
]);
}
public function update(LecturerRequest $request, User $user): RedirectResponse
{
$this->service->update($user, $request->validated());
Inertia::flash('toast', ['type' => 'success', 'message' => 'Dosen berhasil diperbarui.']);
return to_route('admin.users.lecturers.index');
}
public function destroy(User $user): RedirectResponse
{
$this->service->delete($user);
return Inertia::flash('toast', ['type' => 'success', 'message' => 'Dosen berhasil dihapus.'])->back();
}
public function resetPassword(User $user): RedirectResponse
{
$this->service->resetPassword($user);
return Inertia::flash('toast', ['type' => 'success', 'message' => 'Kata sandi berhasil direset.'])->back();
}
public function updateUserStatus(UpdateStatusRequest $request, User $user): RedirectResponse
{
$this->service->updateUserStatus($user, $request->boolean('is_active'));
return Inertia::flash('toast', ['type' => 'success', 'message' => 'Status akun berhasil diperbarui.'])->back();
}
public function export(PaginatedRequest $request): BinaryFileResponse
{
return Excel::download(new LecturersExport(
search: $request->validated('search') ?? '',
gender: $request->validated('gender'),
departmentId: $request->validated('department_id'),
), 'dosen.xlsx');
}
}