| @@ 11-146 (lines=136) @@ | ||
| 8 | /** |
|
| 9 | * Tasks Controller. |
|
| 10 | */ |
|
| 11 | class TaskController extends BaseController |
|
| 12 | { |
|
| 13 | /** |
|
| 14 | * Constructor of the class. |
|
| 15 | * |
|
| 16 | * @param \Slim\Container $container |
|
| 17 | */ |
|
| 18 | public function __construct(\Slim\Container $container) |
|
| 19 | { |
|
| 20 | $this->logger = $container->get('logger'); |
|
| 21 | $this->database = $container->get('db'); |
|
| 22 | } |
|
| 23 | ||
| 24 | /** |
|
| 25 | * Get all tasks. |
|
| 26 | * |
|
| 27 | * @param Request $request |
|
| 28 | * @param Response $response |
|
| 29 | * @param array $args |
|
| 30 | * @return array |
|
| 31 | */ |
|
| 32 | public function getTasks($request, $response, $args) |
|
| 33 | { |
|
| 34 | $this->setParams($request, $response, $args); |
|
| 35 | $service = new TaskService($this->database); |
|
| 36 | $result = $service->getTasks(); |
|
| 37 | ||
| 38 | return $this->jsonResponse('success', $result, 200); |
|
| 39 | } |
|
| 40 | ||
| 41 | /** |
|
| 42 | * Get one task by id. |
|
| 43 | * |
|
| 44 | * @param Request $request |
|
| 45 | * @param Response $response |
|
| 46 | * @param array $args |
|
| 47 | * @return array |
|
| 48 | */ |
|
| 49 | public function getTask($request, $response, $args) |
|
| 50 | { |
|
| 51 | try { |
|
| 52 | $this->setParams($request, $response, $args); |
|
| 53 | $service = new TaskService($this->database); |
|
| 54 | $result = $service->getTask($this->args['id']); |
|
| 55 | ||
| 56 | return $this->jsonResponse('success', $result, 200); |
|
| 57 | } catch (\Exception $ex) { |
|
| 58 | return $this->jsonResponse('error', $ex->getMessage(), $ex->getCode()); |
|
| 59 | } |
|
| 60 | } |
|
| 61 | ||
| 62 | /** |
|
| 63 | * Search tasks by name. |
|
| 64 | * |
|
| 65 | * @param Request $request |
|
| 66 | * @param Response $response |
|
| 67 | * @param array $args |
|
| 68 | * @return array |
|
| 69 | */ |
|
| 70 | public function searchTasks($request, $response, $args) |
|
| 71 | { |
|
| 72 | try { |
|
| 73 | $this->setParams($request, $response, $args); |
|
| 74 | $service = new TaskService($this->database); |
|
| 75 | $result = $service->searchTasks($this->args['query']); |
|
| 76 | ||
| 77 | return $this->jsonResponse('success', $result, 200); |
|
| 78 | } catch (\Exception $ex) { |
|
| 79 | return $this->jsonResponse('error', $ex->getMessage(), $ex->getCode()); |
|
| 80 | } |
|
| 81 | } |
|
| 82 | ||
| 83 | /** |
|
| 84 | * Create a task. |
|
| 85 | * |
|
| 86 | * @param Request $request |
|
| 87 | * @param Response $response |
|
| 88 | * @param array $args |
|
| 89 | * @return array |
|
| 90 | */ |
|
| 91 | public function createTask($request, $response, $args) |
|
| 92 | { |
|
| 93 | try { |
|
| 94 | $this->setParams($request, $response, $args); |
|
| 95 | $service = new TaskService($this->database); |
|
| 96 | $input = $this->request->getParsedBody(); |
|
| 97 | $result = $service->createTask($input); |
|
| 98 | ||
| 99 | return $this->jsonResponse('success', $result, 200); |
|
| 100 | } catch (\Exception $ex) { |
|
| 101 | return $this->jsonResponse('error', $ex->getMessage(), $ex->getCode()); |
|
| 102 | } |
|
| 103 | } |
|
| 104 | ||
| 105 | /** |
|
| 106 | * Update a task. |
|
| 107 | * |
|
| 108 | * @param Request $request |
|
| 109 | * @param Response $response |
|
| 110 | * @param array $args |
|
| 111 | * @return array |
|
| 112 | */ |
|
| 113 | public function updateTask($request, $response, $args) |
|
| 114 | { |
|
| 115 | $this->setParams($request, $response, $args); |
|
| 116 | $input = $this->request->getParsedBody(); |
|
| 117 | $service = new TaskService($this->database); |
|
| 118 | try { |
|
| 119 | $result = $service->updateTask($input, $this->args['id']); |
|
| 120 | return $this->jsonResponse('success', $result, 200); |
|
| 121 | } catch (\Exception $ex) { |
|
| 122 | return $this->jsonResponse('error', $ex->getMessage(), $ex->getCode()); |
|
| 123 | } |
|
| 124 | } |
|
| 125 | ||
| 126 | /** |
|
| 127 | * Delete a task. |
|
| 128 | * |
|
| 129 | * @param Request $request |
|
| 130 | * @param Response $response |
|
| 131 | * @param array $args |
|
| 132 | * @return array |
|
| 133 | */ |
|
| 134 | public function deleteTask($request, $response, $args) |
|
| 135 | { |
|
| 136 | try { |
|
| 137 | $this->setParams($request, $response, $args); |
|
| 138 | $service = new TaskService($this->database); |
|
| 139 | $result = $service->deleteTask($this->args['id']); |
|
| 140 | ||
| 141 | return $this->jsonResponse('success', $result, 200); |
|
| 142 | } catch (\Exception $ex) { |
|
| 143 | return $this->jsonResponse('error', $ex->getMessage(), $ex->getCode()); |
|
| 144 | } |
|
| 145 | } |
|
| 146 | } |
|
| 147 | ||
| @@ 11-146 (lines=136) @@ | ||
| 8 | /** |
|
| 9 | * Users Controller. |
|
| 10 | */ |
|
| 11 | class UserController extends BaseController |
|
| 12 | { |
|
| 13 | /** |
|
| 14 | * Get all users. |
|
| 15 | * |
|
| 16 | * @param Request $request |
|
| 17 | * @param Response $response |
|
| 18 | * @param array $args |
|
| 19 | * @return array |
|
| 20 | */ |
|
| 21 | public function getUsers($request, $response, $args) |
|
| 22 | { |
|
| 23 | $this->setParams($request, $response, $args); |
|
| 24 | $service = new UserService($this->database); |
|
| 25 | $result = $service->getUsers(); |
|
| 26 | ||
| 27 | return $this->jsonResponse('success', $result, 200); |
|
| 28 | } |
|
| 29 | ||
| 30 | /** |
|
| 31 | * Get one user by id. |
|
| 32 | * |
|
| 33 | * @param Request $request |
|
| 34 | * @param Response $response |
|
| 35 | * @param array $args |
|
| 36 | * @return array |
|
| 37 | */ |
|
| 38 | public function getUser($request, $response, $args) |
|
| 39 | { |
|
| 40 | try { |
|
| 41 | $this->setParams($request, $response, $args); |
|
| 42 | $service = new UserService($this->database); |
|
| 43 | $result = $service->getUser($this->args['id']); |
|
| 44 | ||
| 45 | return $this->jsonResponse('success', $result, 200); |
|
| 46 | } catch (\Exception $ex) { |
|
| 47 | return $this->jsonResponse('error', $ex->getMessage(), $ex->getCode()); |
|
| 48 | } |
|
| 49 | } |
|
| 50 | ||
| 51 | /** |
|
| 52 | * Search users by name. |
|
| 53 | * |
|
| 54 | * @param Request $request |
|
| 55 | * @param Response $response |
|
| 56 | * @param array $args |
|
| 57 | * @return array |
|
| 58 | */ |
|
| 59 | public function searchUsers($request, $response, $args) |
|
| 60 | { |
|
| 61 | try { |
|
| 62 | $this->setParams($request, $response, $args); |
|
| 63 | $service = new UserService($this->database); |
|
| 64 | $result = $service->searchUsers($this->args['query']); |
|
| 65 | ||
| 66 | return $this->jsonResponse('success', $result, 200); |
|
| 67 | } catch (\Exception $ex) { |
|
| 68 | return $this->jsonResponse('error', $ex->getMessage(), $ex->getCode()); |
|
| 69 | } |
|
| 70 | } |
|
| 71 | ||
| 72 | /** |
|
| 73 | * Create a user. |
|
| 74 | * |
|
| 75 | * @param Request $request |
|
| 76 | * @param Response $response |
|
| 77 | * @param array $args |
|
| 78 | * @return array |
|
| 79 | */ |
|
| 80 | public function createUser($request, $response, $args) |
|
| 81 | { |
|
| 82 | try { |
|
| 83 | $this->setParams($request, $response, $args); |
|
| 84 | $service = new UserService($this->database); |
|
| 85 | $input = $this->request->getParsedBody(); |
|
| 86 | $result = $service->createUser($input); |
|
| 87 | ||
| 88 | return $this->jsonResponse('success', $result, 200); |
|
| 89 | } catch (\Exception $ex) { |
|
| 90 | return $this->jsonResponse('error', $ex->getMessage(), $ex->getCode()); |
|
| 91 | } |
|
| 92 | } |
|
| 93 | ||
| 94 | /** |
|
| 95 | * Update a user. |
|
| 96 | * |
|
| 97 | * @param Request $request |
|
| 98 | * @param Response $response |
|
| 99 | * @param array $args |
|
| 100 | * @return array |
|
| 101 | */ |
|
| 102 | public function updateUser($request, $response, $args) |
|
| 103 | { |
|
| 104 | $this->setParams($request, $response, $args); |
|
| 105 | $input = $this->request->getParsedBody(); |
|
| 106 | $service = new UserService($this->database); |
|
| 107 | try { |
|
| 108 | $result = $service->updateUser($input, $this->args['id']); |
|
| 109 | return $this->jsonResponse('success', $result, 200); |
|
| 110 | } catch (\Exception $ex) { |
|
| 111 | return $this->jsonResponse('error', $ex->getMessage(), $ex->getCode()); |
|
| 112 | } |
|
| 113 | } |
|
| 114 | ||
| 115 | /** |
|
| 116 | * Delete a user. |
|
| 117 | * |
|
| 118 | * @param Request $request |
|
| 119 | * @param Response $response |
|
| 120 | * @param array $args |
|
| 121 | * @return array |
|
| 122 | */ |
|
| 123 | public function deleteUser($request, $response, $args) |
|
| 124 | { |
|
| 125 | try { |
|
| 126 | $this->setParams($request, $response, $args); |
|
| 127 | $service = new UserService($this->database); |
|
| 128 | $result = $service->deleteUser($this->args['id']); |
|
| 129 | ||
| 130 | return $this->jsonResponse('success', $result, 200); |
|
| 131 | } catch (\Exception $ex) { |
|
| 132 | return $this->jsonResponse('error', $ex->getMessage(), $ex->getCode()); |
|
| 133 | } |
|
| 134 | } |
|
| 135 | ||
| 136 | /** |
|
| 137 | * Constructor of the class. |
|
| 138 | * |
|
| 139 | * @param \Slim\Container $container |
|
| 140 | */ |
|
| 141 | public function __construct(\Slim\Container $container) |
|
| 142 | { |
|
| 143 | $this->logger = $container->get('logger'); |
|
| 144 | $this->database = $container->get('db'); |
|
| 145 | } |
|
| 146 | } |
|
| 147 | ||