koel/app/Http/Controllers/API/UserController.php

59 lines
1.5 KiB
PHP
Raw Normal View History

2015-12-13 04:42:28 +00:00
<?php
namespace App\Http\Controllers\API;
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;
2015-12-13 04:42:28 +00:00
class UserController extends Controller
{
2022-06-10 10:47:46 +00:00
public function __construct(private UserRepository $userRepository, private UserService $userService)
{
}
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);
return UserResource::make($this->userService->updateUser(
$user,
$request->name,
$request->email,
$request->password,
$request->get('is_admin') ?: false
));
2015-12-13 04:42:28 +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);
return response()->noContent();
2015-12-13 04:42:28 +00:00
}
}