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;
|
2015-12-13 04:42:28 +00:00
|
|
|
use Hash;
|
|
|
|
|
|
|
|
class UserController extends Controller
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Create a new user.
|
|
|
|
*
|
|
|
|
* @param UserStoreRequest $request
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\JsonResponse
|
2016-08-03 10:42:11 +00:00
|
|
|
* @throws \RuntimeException
|
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,
|
|
|
|
'password' => Hash::make($request->password),
|
2015-12-13 04:42:28 +00:00
|
|
|
]));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update a user.
|
|
|
|
*
|
|
|
|
* @param UserUpdateRequest $request
|
2015-12-15 10:35:46 +00:00
|
|
|
* @param User $user
|
2015-12-13 04:42:28 +00:00
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\JsonResponse
|
2016-08-03 10:42:11 +00:00
|
|
|
* @throws \RuntimeException
|
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
|
|
|
{
|
|
|
|
$data = $request->only('name', 'email');
|
|
|
|
|
2016-08-03 10:42:11 +00:00
|
|
|
if ($request->password) {
|
|
|
|
$data['password'] = Hash::make($request->password);
|
2015-12-13 04:42:28 +00:00
|
|
|
}
|
|
|
|
|
2015-12-14 16:27:26 +00:00
|
|
|
return response()->json($user->update($data));
|
2015-12-13 04:42:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete a user.
|
|
|
|
*
|
2015-12-15 10:35:46 +00:00
|
|
|
* @param User $user
|
2015-12-13 04:42:28 +00:00
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\JsonResponse
|
2016-08-03 10:42:11 +00:00
|
|
|
* @throws \Exception
|
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException
|
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
|
|
|
{
|
2015-12-14 16:27:26 +00:00
|
|
|
$this->authorize($user);
|
2015-12-13 04:42:28 +00:00
|
|
|
|
2015-12-14 16:27:26 +00:00
|
|
|
return response()->json($user->delete());
|
2015-12-13 04:42:28 +00:00
|
|
|
}
|
|
|
|
}
|