New-OMIS/Modules/Leave/app/Http/Controllers/LeaveController.php
2024-04-11 17:55:56 +05:45

111 lines
3.0 KiB
PHP

<?php
namespace Modules\Leave\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Modules\Employee\Repositories\EmployeeInterface;
use Modules\Leave\Repositories\LeaveInterface;
use Yoeunes\Toastr\Facades\Toastr;
class LeaveController extends Controller
{
private $leaveRepository;
private $employeeRepository;
public function __construct(LeaveInterface $leaveRepository, EmployeeInterface $employeeRepository)
{
$this->leaveRepository = $leaveRepository;
$this->employeeRepository = $employeeRepository;
$this->middleware('role_or_permission:access leaves|create leaves|edit leaves|delete leaves', ['only' => ['index', 'show']]);
$this->middleware('role_or_permission:create leaves', ['only' => ['create', 'store']]);
$this->middleware('role_or_permission:edit leaves', ['only' => ['edit', 'update']]);
$this->middleware('role_or_permission:delete leaves', ['only' => ['destroy']]);
}
/**
* Display a listing of the resource.
*/
public function index()
{
$data['leaves'] = $this->leaveRepository->findAll();
$data['employeeList'] = $this->employeeRepository->pluck();
return view('leave::leave.index', $data);
}
/**
* Show the form for creating a new resource.
*/
public function create()
{
$data['title'] = 'Create Leave';
$data['employeeList'] = $this->employeeRepository->pluck();
return view('leave::leave.create', $data);
}
/**
* Store a newly created resource in storage.
*/
public function store(Request $request): RedirectResponse
{
$inputData = $request->all();
try {
$this->leaveRepository->create($inputData);
toastr()->success('Leave Created Succesfully');
} catch (\Throwable $th) {
toastr()->error($th->getMessage());
}
return redirect()->route('leave.index');
}
/**
* Show the specified resource.
*/
public function show($id)
{
return view('leave::leave.show');
}
/**
* Show the form for editing the specified resource.
*/
public function edit($id)
{
$data['title'] = 'Edit Leave';
$data['leave'] = $this->leaveRepository->getLeaveById($id);
return view('leave::leave.edit', $data);
}
/**
* Update the specified resource in storage.
*/
public function update(Request $request, $id): RedirectResponse
{
$inputData = $request->all();
try {
$this->leaveRepository->update($id, $inputData);
toastr()->success('Leave Updated Succesfully');
} catch (\Throwable $th) {
toastr()->error($th->getMessage());
}
return redirect()->route('leave.index');
}
/**
* Remove the specified resource from storage.
*/
public function destroy($id)
{
$this->leaveRepository->delete($id);
toastr()->success('Leave Deleted Succesfully');
}
}