143 lines
3.7 KiB
PHP
143 lines
3.7 KiB
PHP
|
<?php
|
||
|
|
||
|
namespace Modules\User\Http\Controllers;
|
||
|
|
||
|
use App\Http\Controllers\Controller;
|
||
|
use App\Models\User;
|
||
|
use Illuminate\Http\RedirectResponse;
|
||
|
use Illuminate\Http\Request;
|
||
|
use Modules\Employee\Repositories\EmployeeInterface;
|
||
|
use Modules\User\Repositories\RoleInterface;
|
||
|
use Modules\User\Repositories\UserInterface;
|
||
|
|
||
|
class UserController extends Controller
|
||
|
{
|
||
|
/**
|
||
|
* Display a listing of the resource.
|
||
|
*/
|
||
|
|
||
|
protected $userRepository;
|
||
|
protected $employeeRepository;
|
||
|
protected $roleRepository;
|
||
|
|
||
|
public function __construct(UserInterface $userRepository, EmployeeInterface $employeeRepository, RoleInterface $roleRepository)
|
||
|
{
|
||
|
$this->userRepository = $userRepository;
|
||
|
$this->employeeRepository = $employeeRepository;
|
||
|
$this->roleRepository = $roleRepository;
|
||
|
}
|
||
|
|
||
|
public function index()
|
||
|
{
|
||
|
$data['editable'] = false;
|
||
|
$data['users'] = $this->userRepository->findAll();
|
||
|
$data['roleLists'] = $this->roleRepository->pluck();
|
||
|
$data['employeeLists'] = $this->employeeRepository->pluck();
|
||
|
return view('user::user.index', $data);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Show the form for creating a new resource.
|
||
|
*/
|
||
|
public function create()
|
||
|
{
|
||
|
$data['title'] = "Create User";
|
||
|
$data['roleLists'] = $this->roleRepository->pluck();
|
||
|
$data['employeeLists'] = $this->employeeRepository->pluck();
|
||
|
return view('user::user.create', $data);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Store a newly created resource in storage.
|
||
|
*/
|
||
|
public function store(Request $request): RedirectResponse
|
||
|
{
|
||
|
try {
|
||
|
|
||
|
$validatedData = $request->validate([
|
||
|
'name' => 'required|min:5',
|
||
|
'email' => 'required',
|
||
|
'password' => 'required',
|
||
|
'employee_id' => 'required',
|
||
|
]);
|
||
|
|
||
|
$user = $this->userRepository->create($validatedData, $request->role);
|
||
|
|
||
|
toastr()->success('User has been created!');
|
||
|
|
||
|
} catch (\Throwable $th) {
|
||
|
|
||
|
toastr()->error($th->getMessage());
|
||
|
|
||
|
}
|
||
|
|
||
|
return redirect()->route('user.index');
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Show the specified resource.
|
||
|
*/
|
||
|
|
||
|
public function show($id)
|
||
|
{
|
||
|
$data['user'] = $this->userRepository->getUserById($id);
|
||
|
return view('user::user.show');
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Show the form for editing the specified resource.
|
||
|
*/
|
||
|
public function edit($id)
|
||
|
{
|
||
|
$data['title'] = "Edit User";
|
||
|
$data['roleLists'] = $this->roleRepository->pluck();
|
||
|
$data['employeeLists'] = $this->employeeRepository->pluck();
|
||
|
$data['user'] = $this->userRepository->getUserById($id);
|
||
|
return view('user::user.edit', $data);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Update the specified resource in storage.
|
||
|
*/
|
||
|
public function update(Request $request, $id): RedirectResponse
|
||
|
{
|
||
|
try {
|
||
|
|
||
|
$validatedData = $request->validate([
|
||
|
'name' => 'required|min:5',
|
||
|
'email' => 'required',
|
||
|
'password' => 'required',
|
||
|
'employee_id' => 'required',
|
||
|
]);
|
||
|
|
||
|
$user = $this->userRepository->update($id, $validatedData, $request->role);
|
||
|
|
||
|
toastr()->success('User has been updated!');
|
||
|
|
||
|
} catch (\Throwable $th) {
|
||
|
|
||
|
toastr()->error($th->getMessage());
|
||
|
|
||
|
}
|
||
|
|
||
|
return redirect()->route('user.index');
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Remove the specified resource from storage.
|
||
|
*/
|
||
|
public function destroy($id)
|
||
|
{
|
||
|
try {
|
||
|
|
||
|
$this->userRepository->delete($id);
|
||
|
|
||
|
toastr()->success('User has been deleted!');
|
||
|
|
||
|
} catch (\Throwable $th) {
|
||
|
|
||
|
toastr()->error($th->getMessage());
|
||
|
}
|
||
|
}
|
||
|
}
|