| @@ 17-78 (lines=62) @@ | ||
| 14 | * |
|
| 15 | * @package Finder\Http\Actions |
|
| 16 | */ |
|
| 17 | class UserCreateAction |
|
| 18 | { |
|
| 19 | /** |
|
| 20 | * @var ResponseFactory |
|
| 21 | */ |
|
| 22 | private $responseFactory; |
|
| 23 | ||
| 24 | /** |
|
| 25 | * @var UserManager |
|
| 26 | */ |
|
| 27 | private $userManager; |
|
| 28 | ||
| 29 | /** |
|
| 30 | * UserCreateAction constructor. |
|
| 31 | * |
|
| 32 | * @param ResponseFactory $responseFactory |
|
| 33 | * @param UserManager $userManager |
|
| 34 | */ |
|
| 35 | public function __construct(ResponseFactory $responseFactory, UserManager $userManager) |
|
| 36 | { |
|
| 37 | $this->responseFactory = $responseFactory; |
|
| 38 | $this->userManager = $userManager; |
|
| 39 | } |
|
| 40 | ||
| 41 | /** |
|
| 42 | * @apiVersion 1.0.0 |
|
| 43 | * @api {post} /v1/users Create |
|
| 44 | * @apiName Create |
|
| 45 | * @apiGroup Users |
|
| 46 | * @apiHeader {String} Accept application/json |
|
| 47 | * @apiHeader {String} Content-type application/json |
|
| 48 | * @apiParamExample {json} Request-Body-Example: |
|
| 49 | * { |
|
| 50 | * "name": "username", |
|
| 51 | * "email": "[email protected]", |
|
| 52 | * "password": "password" |
|
| 53 | * } |
|
| 54 | * @apiSuccessExample {json} Success-Response: |
|
| 55 | * HTTP/1.1 201 Created |
|
| 56 | * { |
|
| 57 | * "id": 1, |
|
| 58 | * "name": "username", |
|
| 59 | * "email": "[email protected]", |
|
| 60 | * "role": "Customer", |
|
| 61 | * "created_at": "2099-12-31T23:59:59+00:00", |
|
| 62 | * "updated_at": "2099-12-31T23:59:59+00:00" |
|
| 63 | * } |
|
| 64 | */ |
|
| 65 | ||
| 66 | /** |
|
| 67 | * Create a user. |
|
| 68 | * |
|
| 69 | * @param Request $request |
|
| 70 | * @return JsonResponse |
|
| 71 | */ |
|
| 72 | public function __invoke(Request $request): JsonResponse |
|
| 73 | { |
|
| 74 | $user = $this->userManager->create($request->all()); |
|
| 75 | ||
| 76 | return $this->responseFactory->json(new UserPlainResource($user), Response::HTTP_CREATED); |
|
| 77 | } |
|
| 78 | } |
|
| 79 | ||
| @@ 16-71 (lines=56) @@ | ||
| 13 | * |
|
| 14 | * @package Finder\Http\Actions |
|
| 15 | */ |
|
| 16 | class UserGetByIdAction |
|
| 17 | { |
|
| 18 | /** |
|
| 19 | * @var ResponseFactory |
|
| 20 | */ |
|
| 21 | private $responseFactory; |
|
| 22 | ||
| 23 | /** |
|
| 24 | * @var UserManager |
|
| 25 | */ |
|
| 26 | private $userManager; |
|
| 27 | ||
| 28 | /** |
|
| 29 | * UserGetByIdAction constructor. |
|
| 30 | * |
|
| 31 | * @param ResponseFactory $responseFactory |
|
| 32 | * @param UserManager $userManager |
|
| 33 | */ |
|
| 34 | public function __construct(ResponseFactory $responseFactory, UserManager $userManager) |
|
| 35 | { |
|
| 36 | $this->responseFactory = $responseFactory; |
|
| 37 | $this->userManager = $userManager; |
|
| 38 | } |
|
| 39 | ||
| 40 | /** |
|
| 41 | * @apiVersion 1.0.0 |
|
| 42 | * @api {get} /v1/users/:user_id Get |
|
| 43 | * @apiName Get |
|
| 44 | * @apiGroup Users |
|
| 45 | * @apiHeader {String} Accept application/json |
|
| 46 | * @apiParam {Integer{1...N}='me'} :user_id Unique resource ID. |
|
| 47 | * @apiSuccessExample {json} Success-Response: |
|
| 48 | * HTTP/1.1 20O OK |
|
| 49 | * { |
|
| 50 | * "id": 1, |
|
| 51 | * "name": "username", |
|
| 52 | * "email": "[email protected]", |
|
| 53 | * "role": "Customer", |
|
| 54 | * "created_at": "2099-12-31T23:59:59+00:00", |
|
| 55 | * "updated_at": "2099-12-31T23:59:59+00:00" |
|
| 56 | * } |
|
| 57 | */ |
|
| 58 | ||
| 59 | /** |
|
| 60 | * Get a user. |
|
| 61 | * |
|
| 62 | * @param mixed $id |
|
| 63 | * @return JsonResponse |
|
| 64 | */ |
|
| 65 | public function __invoke($id): JsonResponse |
|
| 66 | { |
|
| 67 | $user = $this->userManager->getById((int) $id); |
|
| 68 | ||
| 69 | return $this->responseFactory->json(new UserPlainResource($user), Response::HTTP_OK); |
|
| 70 | } |
|
| 71 | } |
|
| 72 | ||
| @@ 17-80 (lines=64) @@ | ||
| 14 | * |
|
| 15 | * @package Finder\Http\Actions |
|
| 16 | */ |
|
| 17 | class UserUpdateByIdAction |
|
| 18 | { |
|
| 19 | /** |
|
| 20 | * @var ResponseFactory |
|
| 21 | */ |
|
| 22 | private $responseFactory; |
|
| 23 | ||
| 24 | /** |
|
| 25 | * @var UserManager |
|
| 26 | */ |
|
| 27 | private $userManager; |
|
| 28 | ||
| 29 | /** |
|
| 30 | * UserUpdateByIdAction constructor. |
|
| 31 | * |
|
| 32 | * @param ResponseFactory $responseFactory |
|
| 33 | * @param UserManager $userManager |
|
| 34 | */ |
|
| 35 | public function __construct(ResponseFactory $responseFactory, UserManager $userManager) |
|
| 36 | { |
|
| 37 | $this->responseFactory = $responseFactory; |
|
| 38 | $this->userManager = $userManager; |
|
| 39 | } |
|
| 40 | ||
| 41 | /** |
|
| 42 | * @apiVersion 1.0.0 |
|
| 43 | * @api {put} /v1/users/:user_id Update |
|
| 44 | * @apiName Update |
|
| 45 | * @apiGroup Users |
|
| 46 | * @apiHeader {String} Accept application/json |
|
| 47 | * @apiHeader {String} Content-type application/json |
|
| 48 | * @apiParam {Integer{1...N}='me'} :user_id Unique resource ID. |
|
| 49 | * @apiParamExample {json} Request-Body-Example: |
|
| 50 | * { |
|
| 51 | * "name": "username", |
|
| 52 | * "email": "[email protected]", |
|
| 53 | * "password": "password" |
|
| 54 | * } |
|
| 55 | * @apiSuccessExample {json} Success-Response: |
|
| 56 | * HTTP/1.1 20O OK |
|
| 57 | * { |
|
| 58 | * "id": 1, |
|
| 59 | * "name": "username", |
|
| 60 | * "email": "[email protected]", |
|
| 61 | * "role": "Customer", |
|
| 62 | * "created_at": "2099-12-31T23:59:59+00:00", |
|
| 63 | * "updated_at": "2099-12-31T23:59:59+00:00" |
|
| 64 | * } |
|
| 65 | */ |
|
| 66 | ||
| 67 | /** |
|
| 68 | * Update a user. |
|
| 69 | * |
|
| 70 | * @param mixed $id |
|
| 71 | * @param Request $request |
|
| 72 | * @return JsonResponse |
|
| 73 | */ |
|
| 74 | public function __invoke($id, Request $request): JsonResponse |
|
| 75 | { |
|
| 76 | $user = $this->userManager->updateById((int) $id, $request->all()); |
|
| 77 | ||
| 78 | return $this->responseFactory->json(new UserPlainResource($user), Response::HTTP_OK); |
|
| 79 | } |
|
| 80 | } |
|
| 81 | ||