2015-12-13 04:42:28 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\API;
|
|
|
|
|
2023-08-20 22:35:58 +00:00
|
|
|
use App\Exceptions\UserProspectUpdateDeniedException;
|
2022-07-29 06:47:10 +00:00
|
|
|
use App\Http\Controllers\Controller;
|
2015-12-13 04:42:28 +00:00
|
|
|
use App\Http\Requests\API\UserStoreRequest;
|
|
|
|
use App\Http\Requests\API\UserUpdateRequest;
|
2022-06-10 10:47:46 +00:00
|
|
|
use App\Http\Resources\UserResource;
|
2015-12-14 13:22:39 +00:00
|
|
|
use App\Models\User;
|
2022-06-10 10:47:46 +00:00
|
|
|
use App\Repositories\UserRepository;
|
|
|
|
use App\Services\UserService;
|
2023-08-20 22:35:58 +00:00
|
|
|
use Illuminate\Http\Response;
|
2015-12-13 04:42:28 +00:00
|
|
|
|
|
|
|
class UserController extends Controller
|
|
|
|
{
|
2024-04-18 14:36:28 +00:00
|
|
|
public function __construct(
|
|
|
|
private readonly UserRepository $userRepository,
|
|
|
|
private readonly UserService $userService
|
|
|
|
) {
|
2022-06-10 10:47:46 +00:00
|
|
|
}
|
2018-08-22 19:40:04 +00:00
|
|
|
|
2022-06-10 10:47:46 +00:00
|
|
|
public function index()
|
2018-08-22 19:40:04 +00:00
|
|
|
{
|
2022-06-10 10:47:46 +00:00
|
|
|
$this->authorize('admin', User::class);
|
|
|
|
|
|
|
|
return UserResource::collection($this->userRepository->getAll());
|
2018-08-22 19:40:04 +00:00
|
|
|
}
|
|
|
|
|
2015-12-13 04:42:28 +00:00
|
|
|
public function store(UserStoreRequest $request)
|
|
|
|
{
|
2022-06-10 10:47:46 +00:00
|
|
|
$this->authorize('admin', User::class);
|
|
|
|
|
|
|
|
return UserResource::make($this->userService->createUser(
|
|
|
|
$request->name,
|
|
|
|
$request->email,
|
|
|
|
$request->password,
|
|
|
|
$request->get('is_admin') ?: false
|
|
|
|
));
|
2015-12-13 04:42:28 +00:00
|
|
|
}
|
|
|
|
|
2015-12-15 00:45:10 +00:00
|
|
|
public function update(UserUpdateRequest $request, User $user)
|
2015-12-13 04:42:28 +00:00
|
|
|
{
|
2022-06-10 10:47:46 +00:00
|
|
|
$this->authorize('admin', User::class);
|
|
|
|
|
2023-08-20 22:35:58 +00:00
|
|
|
try {
|
|
|
|
return UserResource::make($this->userService->updateUser(
|
2024-03-19 22:48:12 +00:00
|
|
|
user: $user,
|
|
|
|
name: $request->name,
|
|
|
|
email: $request->email,
|
|
|
|
password: $request->password,
|
|
|
|
isAdmin: $request->get('is_admin') ?: false
|
2023-08-20 22:35:58 +00:00
|
|
|
));
|
|
|
|
} catch (UserProspectUpdateDeniedException) {
|
|
|
|
abort(Response::HTTP_FORBIDDEN, 'Cannot update a user prospect.');
|
|
|
|
}
|
2015-12-13 04:42:28 +00:00
|
|
|
}
|
|
|
|
|
2015-12-14 16:27:26 +00:00
|
|
|
public function destroy(User $user)
|
2015-12-13 04:42:28 +00:00
|
|
|
{
|
2016-09-26 06:30:00 +00:00
|
|
|
$this->authorize('destroy', $user);
|
2022-06-10 10:47:46 +00:00
|
|
|
$this->userService->deleteUser($user);
|
2018-09-03 12:41:49 +00:00
|
|
|
|
2021-12-06 17:07:43 +00:00
|
|
|
return response()->noContent();
|
2015-12-13 04:42:28 +00:00
|
|
|
}
|
|
|
|
}
|