2017-01-30 20:40:43 +01:00
|
|
|
<?php
|
2016-02-04 21:35:28 +01:00
|
|
|
|
2017-01-30 20:40:43 +01:00
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
2017-03-08 17:03:35 +01:00
|
|
|
use App\Http\Requests\UserRequest;
|
2017-01-30 20:40:43 +01:00
|
|
|
use App\Http\Requests\CreateUserRequest;
|
|
|
|
use App\Http\Requests\UpdateUserRequest;
|
|
|
|
use App\Models\User;
|
2016-02-04 21:35:28 +01:00
|
|
|
use App\Ninja\Repositories\UserRepository;
|
|
|
|
use App\Ninja\Transformers\UserTransformer;
|
2017-01-30 20:40:43 +01:00
|
|
|
use App\Services\UserService;
|
2016-02-04 21:35:28 +01:00
|
|
|
use Auth;
|
|
|
|
|
|
|
|
class UserApiController extends BaseAPIController
|
|
|
|
{
|
|
|
|
protected $userService;
|
|
|
|
protected $userRepo;
|
|
|
|
|
2016-05-01 22:55:13 +02:00
|
|
|
protected $entityType = ENTITY_USER;
|
|
|
|
|
2016-02-04 21:35:28 +01:00
|
|
|
public function __construct(UserService $userService, UserRepository $userRepo)
|
|
|
|
{
|
2016-03-02 15:36:46 +01:00
|
|
|
parent::__construct();
|
2016-02-04 21:35:28 +01:00
|
|
|
|
|
|
|
$this->userService = $userService;
|
|
|
|
$this->userRepo = $userRepo;
|
|
|
|
}
|
|
|
|
|
2017-03-08 17:03:35 +01:00
|
|
|
/**
|
|
|
|
* @SWG\Get(
|
|
|
|
* path="/users",
|
2017-03-09 17:22:37 +01:00
|
|
|
* summary="List users",
|
|
|
|
* operationId="listUsers",
|
2017-03-08 17:03:35 +01:00
|
|
|
* tags={"user"},
|
|
|
|
* @SWG\Response(
|
|
|
|
* response=200,
|
|
|
|
* description="A list of users",
|
|
|
|
* @SWG\Schema(type="array", @SWG\Items(ref="#/definitions/User"))
|
|
|
|
* ),
|
|
|
|
* @SWG\Response(
|
|
|
|
* response="default",
|
|
|
|
* description="an ""unexpected"" error"
|
|
|
|
* )
|
|
|
|
* )
|
|
|
|
*/
|
2016-02-04 21:35:28 +01:00
|
|
|
public function index()
|
|
|
|
{
|
2016-05-01 22:55:13 +02:00
|
|
|
$users = User::whereAccountId(Auth::user()->account_id)
|
|
|
|
->withTrashed()
|
|
|
|
->orderBy('created_at', 'desc');
|
2017-03-08 17:03:35 +01:00
|
|
|
|
2016-05-02 10:38:01 +02:00
|
|
|
return $this->listResponse($users);
|
2016-02-04 21:35:28 +01:00
|
|
|
}
|
|
|
|
|
2017-03-08 17:03:35 +01:00
|
|
|
/**
|
|
|
|
* @SWG\Get(
|
|
|
|
* path="/users/{user_id}",
|
2017-03-09 17:22:37 +01:00
|
|
|
* summary="Retrieve a user",
|
|
|
|
* operationId="getUser",
|
2017-03-08 17:03:35 +01:00
|
|
|
* tags={"client"},
|
|
|
|
* @SWG\Parameter(
|
|
|
|
* in="path",
|
|
|
|
* name="user_id",
|
|
|
|
* type="integer",
|
2017-03-09 11:11:45 +01:00
|
|
|
* required=true
|
2017-03-08 17:03:35 +01:00
|
|
|
* ),
|
|
|
|
* @SWG\Response(
|
|
|
|
* response=200,
|
|
|
|
* description="A single user",
|
|
|
|
* @SWG\Schema(type="object", @SWG\Items(ref="#/definitions/User"))
|
|
|
|
* ),
|
|
|
|
* @SWG\Response(
|
|
|
|
* response="default",
|
|
|
|
* description="an ""unexpected"" error"
|
|
|
|
* )
|
|
|
|
* )
|
|
|
|
*/
|
|
|
|
public function show(UserRequest $request)
|
|
|
|
{
|
|
|
|
return $this->itemResponse($request->entity());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @SWG\Post(
|
|
|
|
* path="/users",
|
|
|
|
* summary="Create a user",
|
2017-03-09 17:22:37 +01:00
|
|
|
* operationId="createUser",
|
|
|
|
* tags={"user"},
|
2017-03-08 17:03:35 +01:00
|
|
|
* @SWG\Parameter(
|
|
|
|
* in="body",
|
|
|
|
* name="user",
|
|
|
|
* @SWG\Schema(ref="#/definitions/User")
|
|
|
|
* ),
|
|
|
|
* @SWG\Response(
|
|
|
|
* response=200,
|
|
|
|
* description="New user",
|
|
|
|
* @SWG\Schema(type="object", @SWG\Items(ref="#/definitions/User"))
|
|
|
|
* ),
|
|
|
|
* @SWG\Response(
|
|
|
|
* response="default",
|
|
|
|
* description="an ""unexpected"" error"
|
|
|
|
* )
|
|
|
|
* )
|
|
|
|
*/
|
2016-02-04 21:35:28 +01:00
|
|
|
public function store(CreateUserRequest $request)
|
|
|
|
{
|
|
|
|
return $this->save($request);
|
|
|
|
}
|
2016-03-02 14:36:42 +01:00
|
|
|
|
2017-03-08 17:03:35 +01:00
|
|
|
/**
|
|
|
|
* @SWG\Put(
|
|
|
|
* path="/users/{user_id}",
|
|
|
|
* summary="Update a user",
|
2017-03-09 17:22:37 +01:00
|
|
|
* operationId="updateUser",
|
|
|
|
* tags={"user"},
|
2017-03-08 17:03:35 +01:00
|
|
|
* @SWG\Parameter(
|
|
|
|
* in="path",
|
|
|
|
* name="user_id",
|
|
|
|
* type="integer",
|
2017-03-09 11:11:45 +01:00
|
|
|
* required=true
|
2017-03-08 17:03:35 +01:00
|
|
|
* ),
|
|
|
|
* @SWG\Parameter(
|
|
|
|
* in="body",
|
|
|
|
* name="user",
|
|
|
|
* @SWG\Schema(ref="#/definitions/User")
|
|
|
|
* ),
|
|
|
|
* @SWG\Response(
|
|
|
|
* response=200,
|
|
|
|
* description="Updated user",
|
|
|
|
* @SWG\Schema(type="object", @SWG\Items(ref="#/definitions/User"))
|
|
|
|
* ),
|
|
|
|
* @SWG\Response(
|
|
|
|
* response="default",
|
|
|
|
* description="an ""unexpected"" error"
|
|
|
|
* )
|
|
|
|
* )
|
|
|
|
*
|
|
|
|
* @param mixed $userPublicId
|
|
|
|
*/
|
2016-02-04 21:35:28 +01:00
|
|
|
public function update(UpdateUserRequest $request, $userPublicId)
|
|
|
|
{
|
|
|
|
$user = Auth::user();
|
|
|
|
|
|
|
|
if ($request->action == ACTION_ARCHIVE) {
|
|
|
|
$this->userRepo->archive($user);
|
|
|
|
|
|
|
|
$transformer = new UserTransformer(Auth::user()->account, $request->serializer);
|
|
|
|
$data = $this->createItem($user, $transformer, 'users');
|
|
|
|
|
|
|
|
return $this->response($data);
|
|
|
|
} else {
|
|
|
|
return $this->save($request, $user);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function save($request, $user = false)
|
|
|
|
{
|
|
|
|
$user = $this->userRepo->save($request->input(), $user);
|
|
|
|
|
|
|
|
$transformer = new UserTransformer(\Auth::user()->account, $request->serializer);
|
|
|
|
$data = $this->createItem($user, $transformer, 'users');
|
|
|
|
|
|
|
|
return $this->response($data);
|
|
|
|
}
|
2017-03-08 17:03:35 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @SWG\Delete(
|
|
|
|
* path="/users/{user_id}",
|
|
|
|
* summary="Delete a user",
|
2017-03-09 17:22:37 +01:00
|
|
|
* operationId="deleteUser",
|
|
|
|
* tags={"user"},
|
2017-03-08 17:03:35 +01:00
|
|
|
* @SWG\Parameter(
|
|
|
|
* in="path",
|
|
|
|
* name="user_id",
|
|
|
|
* type="integer",
|
2017-03-09 11:11:45 +01:00
|
|
|
* required=true
|
2017-03-08 17:03:35 +01:00
|
|
|
* ),
|
|
|
|
* @SWG\Response(
|
|
|
|
* response=200,
|
|
|
|
* description="Deleted user",
|
|
|
|
* @SWG\Schema(type="object", @SWG\Items(ref="#/definitions/User"))
|
|
|
|
* ),
|
|
|
|
* @SWG\Response(
|
|
|
|
* response="default",
|
|
|
|
* description="an ""unexpected"" error"
|
|
|
|
* )
|
|
|
|
* )
|
|
|
|
*/
|
2017-03-09 11:11:45 +01:00
|
|
|
public function destroy(UpdateUserRequest $request)
|
2017-03-08 17:03:35 +01:00
|
|
|
{
|
|
|
|
$entity = $request->entity();
|
|
|
|
|
|
|
|
$this->userRepo->delete($entity);
|
|
|
|
|
|
|
|
return $this->itemResponse($entity);
|
|
|
|
}
|
2017-01-30 17:05:31 +01:00
|
|
|
}
|