New-OMIS/Modules/Leave/app/Http/Controllers/LeaveController.php

112 lines
3.0 KiB
PHP
Raw Normal View History

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