New-OMIS/Modules/Attendance/app/Http/Controllers/AttendanceController.php

113 lines
2.9 KiB
PHP
Raw Permalink Normal View History

2024-04-07 07:28:58 +00:00
<?php
namespace Modules\Attendance\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
2024-04-16 11:38:35 +00:00
use Modules\Attendance\Repositories\AttendanceRepository;
2024-04-07 07:28:58 +00:00
class AttendanceController extends Controller
{
2024-04-16 11:38:35 +00:00
private $attendanceRepository;
public function __construct(AttendanceRepository $attendanceRepository)
{
$this->attendanceRepository = $attendanceRepository;
}
2024-04-07 07:28:58 +00:00
/**
* Display a listing of the resource.
*/
public function index()
{
2024-04-16 11:38:35 +00:00
$data['title'] = 'Attendance Lists';
$data['attendanceLists'] = $this->attendanceRepository->findAll();
return view('attendance::attendances.index', $data);
2024-04-07 07:28:58 +00:00
}
/**
* Show the form for creating a new resource.
*/
public function create()
{
2024-04-16 11:38:35 +00:00
$data['title'] = 'Create Attendance';
$data['editable'] = false;
return view('attendance::attendances.create', $data);
2024-04-07 07:28:58 +00:00
}
/**
* Store a newly created resource in storage.
*/
public function store(Request $request): RedirectResponse
{
2024-04-16 11:38:35 +00:00
$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');
2024-04-07 07:28:58 +00:00
}
/**
* Show the specified resource.
*/
public function show($id)
{
2024-04-16 11:38:35 +00:00
return view('attendance::attendances.show');
2024-04-07 07:28:58 +00:00
}
/**
* Show the form for editing the specified resource.
*/
public function edit($id)
{
2024-04-16 11:38:35 +00:00
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);
2024-04-07 07:28:58 +00:00
}
/**
* Update the specified resource in storage.
*/
public function update(Request $request, $id): RedirectResponse
{
2024-04-16 11:38:35 +00:00
try {
$this->attendanceRepository->update($id, $request->all());
toastr()->success('Attendance Updated Successfully');
} catch (\Throwable $th) {
toastr()->error($th->getMessage());
}
return redirect()->route('attendance.index');
2024-04-07 07:28:58 +00:00
}
/**
* Remove the specified resource from storage.
*/
public function destroy($id)
{
2024-04-16 11:38:35 +00:00
try {
$this->attendanceRepository->delete($id);
toastr()->success('Attendance Deleted Successfully');
} catch (\Throwable $th) {
toastr()->error($th->getMessage());
}
return redirect()->route('attendance.index');
2024-04-07 07:28:58 +00:00
}
}