2015-12-13 04:42:28 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\API;
|
|
|
|
|
|
|
|
use App\Http\Requests\API\UserStoreRequest;
|
|
|
|
use App\Http\Requests\API\UserUpdateRequest;
|
2015-12-14 13:22:39 +00:00
|
|
|
use App\Models\User;
|
2018-08-22 19:40:04 +00:00
|
|
|
use Illuminate\Contracts\Hashing\Hasher as Hash;
|
2020-09-13 22:04:07 +00:00
|
|
|
use Illuminate\Http\Response;
|
2015-12-13 04:42:28 +00:00
|
|
|
|
|
|
|
class UserController extends Controller
|
|
|
|
{
|
2018-08-22 19:40:04 +00:00
|
|
|
private $hash;
|
|
|
|
|
|
|
|
public function __construct(Hash $hash)
|
|
|
|
{
|
|
|
|
$this->hash = $hash;
|
|
|
|
}
|
|
|
|
|
2015-12-13 04:42:28 +00:00
|
|
|
public function store(UserStoreRequest $request)
|
|
|
|
{
|
|
|
|
return response()->json(User::create([
|
2016-08-03 10:42:11 +00:00
|
|
|
'name' => $request->name,
|
|
|
|
'email' => $request->email,
|
2018-08-22 19:40:04 +00:00
|
|
|
'password' => $this->hash->make($request->password),
|
2020-06-13 15:19:47 +00:00
|
|
|
'is_admin' => $request->is_admin,
|
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
|
|
|
{
|
2020-06-13 15:19:47 +00:00
|
|
|
$data = $request->only('name', 'email', 'is_admin');
|
2015-12-13 04:42:28 +00:00
|
|
|
|
2016-08-03 10:42:11 +00:00
|
|
|
if ($request->password) {
|
2018-08-22 19:40:04 +00:00
|
|
|
$data['password'] = $this->hash->make($request->password);
|
2015-12-13 04:42:28 +00:00
|
|
|
}
|
|
|
|
|
2018-09-03 12:41:49 +00:00
|
|
|
$user->update($data);
|
|
|
|
|
2020-06-13 15:19:47 +00:00
|
|
|
return response()->json($user);
|
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);
|
2015-12-13 04:42:28 +00:00
|
|
|
|
2018-09-03 12:41:49 +00:00
|
|
|
$user->delete();
|
|
|
|
|
2020-09-13 22:04:07 +00:00
|
|
|
return response()->json(null, Response::HTTP_NO_CONTENT);
|
2015-12-13 04:42:28 +00:00
|
|
|
}
|
|
|
|
}
|