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

80 lines
1.9 KiB
PHP
Raw Normal View History

2015-12-13 04:42:28 +00:00
<?php
namespace App\Http\Controllers\API;
2015-12-14 13:22:39 +00:00
use App\Http\Requests\API\ProfileUpdateRequest;
2015-12-13 04:42:28 +00:00
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
*/
public function store(UserStoreRequest $request)
{
return response()->json(User::create([
'name' => $request->input('name'),
'email' => $request->input('email'),
'password' => Hash::make($request->input('password')),
]));
}
/**
* 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
*/
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');
if ($password = $request->input('password')) {
$data['password'] = Hash::make($password);
}
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
*/
public function destroy(User $user)
2015-12-13 04:42:28 +00:00
{
$this->authorize($user);
2015-12-13 04:42:28 +00:00
return response()->json($user->delete());
2015-12-13 04:42:28 +00:00
}
/**
* Update the current user's profile.
2015-12-15 10:35:46 +00:00
*
2015-12-13 04:42:28 +00:00
* @param ProfileUpdateRequest $request
*
* @return \Illuminate\Http\JsonResponse
*/
public function updateProfile(ProfileUpdateRequest $request)
{
$data = $request->only('name', 'email');
if ($password = $request->input('password')) {
$data['password'] = Hash::make($password);
}
return response()->json(auth()->user()->update($data));
}
}