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;
|
2017-06-04 01:30:45 +00:00
|
|
|
use Exception;
|
2015-12-13 04:42:28 +00:00
|
|
|
use Hash;
|
2017-06-04 01:30:45 +00:00
|
|
|
use Illuminate\Auth\Access\AuthorizationException;
|
|
|
|
use Illuminate\Http\JsonResponse;
|
|
|
|
use RuntimeException;
|
2015-12-13 04:42:28 +00:00
|
|
|
|
|
|
|
class UserController extends Controller
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Create a new user.
|
|
|
|
*
|
|
|
|
* @param UserStoreRequest $request
|
|
|
|
*
|
2017-06-04 01:30:45 +00:00
|
|
|
* @throws RuntimeException
|
2016-08-03 10:42:39 +00:00
|
|
|
*
|
2017-06-04 01:30:45 +00:00
|
|
|
* @return JsonResponse
|
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
|
|
|
*
|
2017-06-04 01:30:45 +00:00
|
|
|
* @throws RuntimeException
|
2016-08-03 10:42:39 +00:00
|
|
|
*
|
2017-06-04 01:30:45 +00:00
|
|
|
* @return JsonResponse
|
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
|
|
|
*
|
2017-06-04 01:30:45 +00:00
|
|
|
* @throws Exception
|
|
|
|
* @throws AuthorizationException
|
2016-08-03 10:42:39 +00:00
|
|
|
*
|
2017-06-04 01:30:45 +00:00
|
|
|
* @return JsonResponse
|
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
|
|
|
|
2015-12-14 16:27:26 +00:00
|
|
|
return response()->json($user->delete());
|
2015-12-13 04:42:28 +00:00
|
|
|
}
|
|
|
|
}
|