New-OMIS/Modules/Attendance/app/Http/Controllers/AttendanceController.php
2024-04-16 17:23:35 +05:45

113 lines
2.9 KiB
PHP

<?php
namespace Modules\Attendance\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Modules\Attendance\Repositories\AttendanceRepository;
class AttendanceController extends Controller
{
private $attendanceRepository;
public function __construct(AttendanceRepository $attendanceRepository)
{
$this->attendanceRepository = $attendanceRepository;
}
/**
* Display a listing of the resource.
*/
public function index()
{
$data['title'] = 'Attendance Lists';
$data['attendanceLists'] = $this->attendanceRepository->findAll();
return view('attendance::attendances.index', $data);
}
/**
* Show the form for creating a new resource.
*/
public function create()
{
$data['title'] = 'Create Attendance';
$data['editable'] = false;
return view('attendance::attendances.create', $data);
}
/**
* Store a newly created resource in storage.
*/
public function store(Request $request): RedirectResponse
{
$request->merge([
'date' => $request->date ? $request->date : now()->format('Y-m-d'),
]);
try {
$this->attendanceRepository->create($request->all());
toastr()->success('Attendance Created Successfully');
} catch (\Throwable $th) {
toastr()->error($th->getMessage());
}
return redirect()->route('attendance.index');
}
/**
* Show the specified resource.
*/
public function show($id)
{
return view('attendance::attendances.show');
}
/**
* Show the form for editing the specified resource.
*/
public function edit($id)
{
try {
$data['title'] = 'Edit Attendance';
$data['editable'] = true;
$data['attendance'] = $this->attendanceRepository->getAttendanceById($id);
} catch (\Throwable $th) {
toastr()->error($th->getMessage());
}
return view('attendance::attendances.edit', $data);
}
/**
* Update the specified resource in storage.
*/
public function update(Request $request, $id): RedirectResponse
{
try {
$this->attendanceRepository->update($id, $request->all());
toastr()->success('Attendance Updated Successfully');
} catch (\Throwable $th) {
toastr()->error($th->getMessage());
}
return redirect()->route('attendance.index');
}
/**
* Remove the specified resource from storage.
*/
public function destroy($id)
{
try {
$this->attendanceRepository->delete($id);
toastr()->success('Attendance Deleted Successfully');
} catch (\Throwable $th) {
toastr()->error($th->getMessage());
}
return redirect()->route('attendance.index');
}
}