master_template/app/Http/Controllers/UsersController.php

138 lines
3.6 KiB
PHP
Raw Normal View History

2024-06-20 10:57:33 +00:00
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
2024-06-23 11:17:56 +00:00
use App\Repositories\RoleInterface;
use App\Repositories\RoleRepository;
use App\Repositories\UserRepository;
2024-06-20 10:57:33 +00:00
use Illuminate\Http\Request;
2024-06-23 11:17:56 +00:00
use Illuminate\Http\RedirectResponse;
use Brian2694\Toastr\Facades\Toastr;
2024-06-20 10:57:33 +00:00
class UsersController extends Controller
{
2024-06-23 11:17:56 +00:00
protected $userRepository;
protected $employeeRepository;
protected $roleRepository;
2024-06-20 10:57:33 +00:00
2024-06-23 11:17:56 +00:00
public function __construct(UserRepository $userRepository, RoleRepository $roleRepository)
2024-06-20 10:57:33 +00:00
{
2024-06-23 11:17:56 +00:00
$this->userRepository = $userRepository;
$this->roleRepository = $roleRepository;
2024-06-20 10:57:33 +00:00
}
2024-06-23 11:17:56 +00:00
public function index()
2024-06-20 10:57:33 +00:00
{
2024-06-23 11:17:56 +00:00
$data['users'] = $this->userRepository->findAll();
$data['editable'] = false;
$data['roleLists'] = $this->roleRepository->pluck();
return view('crud.generated.users.index', $data);
2024-06-20 10:57:33 +00:00
}
2024-06-23 11:17:56 +00:00
/**
* Show the form for creating a new resource.
*/
public function create()
2024-06-20 10:57:33 +00:00
{
2024-06-23 11:17:56 +00:00
$data['title'] = "Create User";
$data['editable'] = false;
$data['roleLists'] = $this->roleRepository->pluck();
return view('crud.generated.users.create', $data);
2024-06-20 10:57:33 +00:00
}
2024-06-23 11:17:56 +00:00
/**
* Store a newly created resource in storage.
*/
public function store(Request $request): RedirectResponse
2024-06-20 10:57:33 +00:00
{
2024-06-23 11:17:56 +00:00
// try {
2024-06-20 10:57:33 +00:00
2024-06-23 11:17:56 +00:00
$validated = $request->validate([
'name' => 'required|min:5',
'email' => 'required',
'password' => 'required',
]);
2024-06-20 10:57:33 +00:00
2024-06-23 11:17:56 +00:00
$validated['password'] = bcrypt($validated['password']);
2024-06-20 10:57:33 +00:00
2024-06-23 11:17:56 +00:00
$this->userRepository->create($validated, $request->role);
2024-06-20 10:57:33 +00:00
2024-06-23 11:17:56 +00:00
Toastr::success('User has been created successfully.', 'Title', ["positionClass" => "toast-top-center"]);
// } catch (\Throwable $th) {
2024-06-20 10:57:33 +00:00
2024-06-23 11:17:56 +00:00
// echo $th->getMessage();
2024-06-20 10:57:33 +00:00
2024-06-23 11:17:56 +00:00
// toastr()->error($th->getMessage());
// }
return redirect()->route('user.index');
2024-06-20 10:57:33 +00:00
}
2024-06-23 11:17:56 +00:00
/**
* Show the specified resource.
*/
2024-06-20 10:57:33 +00:00
2024-06-23 11:17:56 +00:00
public function show($id)
{
$data['user'] = $this->userRepository->getUserById($id);
return view('user::user.show');
}
2024-06-20 10:57:33 +00:00
2024-06-23 11:17:56 +00:00
/**
* Show the form for editing the specified resource.
*/
public function edit($id)
{
$data['title'] = "Edit User";
$data['editable'] = true;
$data['roleLists'] = $this->roleRepository->pluck();
$data['user'] = $this->userRepository->getUserById($id);
return view('crud.generated.users.edit', $data);
2024-06-20 10:57:33 +00:00
}
2024-06-23 11:17:56 +00:00
/**
* Update the specified resource in storage.
*/
public function update(Request $request, $id): RedirectResponse
2024-06-20 10:57:33 +00:00
{
2024-06-23 11:17:56 +00:00
try {
$validated = $request->validate([
'name' => 'required|min:5',
'email' => 'required',
'password' => 'required',
]);
$validated['password'] = bcrypt($validated['password']);
$this->userRepository->update($id, $validated, $request->role);
Toastr::success('User has been updated', 'Title', ["positionClass" => "toast-top-center"]);
} catch (\Throwable $th) {
toastr()->error($th->getMessage());
2024-06-20 10:57:33 +00:00
}
2024-06-23 11:17:56 +00:00
return redirect()->route('user.index');
2024-06-20 10:57:33 +00:00
}
2024-06-23 11:17:56 +00:00
/**
* Remove the specified resource from storage.
*/
public function destroy($id)
2024-06-20 10:57:33 +00:00
{
2024-06-23 11:17:56 +00:00
try {
$this->userRepository->delete($id);
Toastr::success('User has been deleted', 'Title', ["positionClass" => "toast-top-center"]);
} catch (\Throwable $th) {
Toastr::success('Error deleting user', 'Title', ["positionClass" => "toast-top-center"]);
2024-06-20 10:57:33 +00:00
}
}
}