employee crud

This commit is contained in:
2024-04-10 15:15:24 +05:45
parent d92366b1f4
commit b5c603ceec
13 changed files with 420 additions and 194 deletions

View File

@ -3,18 +3,25 @@
namespace Modules\Employee\Http\Controllers;
use App\Http\Controllers\Controller;
use App\Models\Role;
use Carbon\Carbon;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Modules\Employee\Repositories\EmployeeInterface;
use Modules\User\Repositories\UserInterface;
class EmployeeController extends Controller
{
private EmployeeInterface $employeeRepository;
private $employeeRepository;
private $userRepository;
public function __construct(EmployeeInterface $employeeRepository)
public function __construct(EmployeeInterface $employeeRepository, UserInterface $userRepository)
{
$this->employeeRepository = $employeeRepository;
$this->userRepository = $userRepository;
}
/**
* Display a listing of the resource.
@ -22,6 +29,8 @@ class EmployeeController extends Controller
public function index()
{
$data['employees'] = $this->employeeRepository->findAll();
$data['roleLists'] = Role::pluck('name', 'id');
// dd($data['employees']->toArray());
return view('employee::index', $data);
}
@ -31,6 +40,11 @@ class EmployeeController extends Controller
public function create()
{
$data['title'] = 'Create Employee';
$data['departmentList'] = [];
$data['designationList'] = [];
$data['genderList'] = [];
$data['nationalityList'] = [];
return view('employee::create', $data);
}
@ -39,22 +53,20 @@ class EmployeeController extends Controller
*/
public function store(Request $request)
{
$inputData = $request->only(['first_name', 'last_name']);
// try {
$inputData = $request->all();
try {
if ($request->hasFile('profile_pic')) {
$fileName = time() . '_' . $request->profile_pic->getClientOriginalName();
$filePath = $request->file('profile_pic')->storeAs('uploads', $fileName, 'public');
$inputData['profile_picture'] = time() . '_' . $request->profile_pic->getClientOriginalName();
// $fileModel->file_path = '/storage/' . $filePath;
// $fileModel->save();
if ($request->hasFile('profile_pic')) {
$fileName = time() . '_' . $request->profile_pic->getClientOriginalName();
$filePath = $request->file('profile_pic')->storeAs('uploads', $fileName, 'public');
$inputData['profile_picture'] = time() . '_' . $request->profile_pic->getClientOriginalName();
}
$this->employeeRepository->create($inputData);
toastr()->success('Employee Created Succesfully');
} catch (\Throwable $th) {
toastr()->error($th->getMessage());
}
$this->employeeRepository->create($inputData);
toastr()->success('Employee Created Succesfully');
// } catch (\Throwable $th) {
// toastr()->error($th->getMessage());
// }
return redirect()->route('employee.index');
}
@ -63,8 +75,8 @@ class EmployeeController extends Controller
*/
public function show($id)
{
return view('employee::show');
$data['employee'] = $this->employeeRepository->getEmployeeById($id);
return view('employee::show', $data);
}
/**
@ -82,20 +94,20 @@ class EmployeeController extends Controller
*/
public function update(Request $request, $id): RedirectResponse
{
$inputData = $request->only(['first_name', 'last_name']);
// try {
$inputData = $request->except(['_method', '_token']);
try {
if ($request->hasFile('profile_pic')) {
$fileName = time() . '_' . $request->profile_pic->getClientOriginalName();
$filePath = $request->file('profile_pic')->storeAs('uploads', $fileName, 'public');
$inputData['profile_picture'] = time() . '_' . $request->profile_pic->getClientOriginalName();
if ($request->hasFile('profile_pic')) {
$fileName = time() . '_' . $request->profile_pic->getClientOriginalName();
$filePath = $request->file('profile_pic')->storeAs('uploads', $fileName, 'public');
$inputData['profile_picture'] = time() . '_' . $request->profile_pic->getClientOriginalName();
}
$this->employeeRepository->update($id, $inputData);
toastr()->success('Employee Created Succesfully');
} catch (\Throwable $th) {
toastr()->error($th->getMessage());
}
$this->employeeRepository->update($id, $inputData);
toastr()->success('Employee Created Succesfully');
// } catch (\Throwable $th) {
// toastr()->error($th->getMessage());
// }
return redirect()->route('employee.index');
}
@ -104,6 +116,41 @@ class EmployeeController extends Controller
*/
public function destroy($id)
{
//
try {
$employeeModel = $this->employeeRepository->getEmployeeById($id);
$employeeModel->user->roles()->detach();
$employeeModel->user->delete();
$employeeModel->delete();
toastr()->success('Employee Delete Succesfully');
} catch (\Throwable $th) {
toastr()->error($th->getMessage());
}
return redirect()->route('employee.index');
}
public function assignRole(Request $request)
{
try {
$employeeModel = $this->employeeRepository->getEmployeeByEmail($request->email);
$inputData = [
'name' => $employeeModel->first_name . ' ' . $employeeModel->last_name,
'email' => $request->email,
'password' => Hash::make('password'),
'email_verified_at' => Carbon::now(),
];
$userModel = $this->userRepository->create($inputData, [$request->role_id]);
$employeeModel->users_id = $userModel->id;
$employeeModel->save();
toastr()->success('Role Assigned Succesfully');
} catch (\Throwable $th) {
toastr()->error($th->getMessage());
}
return redirect()->route('employee.index');
}
}