| @@ 12-127 (lines=116) @@ | ||
| 9 | /** |
|
| 10 | * Tasks Repository. |
|
| 11 | */ |
|
| 12 | class TaskRepository extends BaseRepository |
|
| 13 | { |
|
| 14 | /** |
|
| 15 | * @param object $database |
|
| 16 | */ |
|
| 17 | public function __construct(\PDO $database = null) |
|
| 18 | { |
|
| 19 | $this->database = $database; |
|
| 20 | } |
|
| 21 | ||
| 22 | /** |
|
| 23 | * Check if the task exists. |
|
| 24 | * |
|
| 25 | * @param int $taskId |
|
| 26 | * @return array $task |
|
| 27 | * @throws \Exception |
|
| 28 | */ |
|
| 29 | public function checkTask($taskId) |
|
| 30 | { |
|
| 31 | $statement = $this->database->prepare(TaskQuery::GET_TASK_QUERY); |
|
| 32 | $statement->bindParam('id', $taskId); |
|
| 33 | $statement->execute(); |
|
| 34 | $task = $statement->fetchObject(); |
|
| 35 | if (empty($task)) { |
|
| 36 | throw new TaskException(TaskException::TASK_NOT_FOUND, 404); |
|
| 37 | } |
|
| 38 | ||
| 39 | return $task; |
|
| 40 | } |
|
| 41 | ||
| 42 | /** |
|
| 43 | * Get all tasks. |
|
| 44 | * |
|
| 45 | * @return array |
|
| 46 | */ |
|
| 47 | public function getTasks() |
|
| 48 | { |
|
| 49 | $statement = $this->database->prepare(TaskQuery::GET_TASKS_QUERY); |
|
| 50 | $statement->execute(); |
|
| 51 | ||
| 52 | return $statement->fetchAll(); |
|
| 53 | } |
|
| 54 | ||
| 55 | /** |
|
| 56 | * Search tasks by name. |
|
| 57 | * |
|
| 58 | * @param string $tasksName |
|
| 59 | * @return array |
|
| 60 | * @throws \Exception |
|
| 61 | */ |
|
| 62 | public function searchTasks($tasksName) |
|
| 63 | { |
|
| 64 | $statement = $this->database->prepare(TaskQuery::SEARCH_TASKS_QUERY); |
|
| 65 | $query = '%' . $tasksName . '%'; |
|
| 66 | $statement->bindParam('query', $query); |
|
| 67 | $statement->execute(); |
|
| 68 | $tasks = $statement->fetchAll(); |
|
| 69 | if (!$tasks) { |
|
| 70 | throw new TaskException(TaskException::TASK_NOT_FOUND, 404); |
|
| 71 | } |
|
| 72 | ||
| 73 | return $tasks; |
|
| 74 | } |
|
| 75 | ||
| 76 | /** |
|
| 77 | * Create a task. |
|
| 78 | * |
|
| 79 | * @param array $data |
|
| 80 | * @return array |
|
| 81 | * @throws \Exception |
|
| 82 | */ |
|
| 83 | public function createTask($data) |
|
| 84 | { |
|
| 85 | $statement = $this->database->prepare(TaskQuery::CREATE_TASK_QUERY); |
|
| 86 | $statement->bindParam('task', $data['task']); |
|
| 87 | $statement->bindParam('status', $data['status']); |
|
| 88 | $statement->execute(); |
|
| 89 | $task = $this->checkTask($this->database->lastInsertId()); |
|
| 90 | ||
| 91 | return $task; |
|
| 92 | } |
|
| 93 | ||
| 94 | /** |
|
| 95 | * Update a task. |
|
| 96 | * |
|
| 97 | * @param array $data |
|
| 98 | * @param int $taskId |
|
| 99 | * @return array |
|
| 100 | */ |
|
| 101 | public function updateTask($data, $taskId) |
|
| 102 | { |
|
| 103 | $statement = $this->database->prepare(TaskQuery::UPDATE_TASK_QUERY); |
|
| 104 | $statement->bindParam('id', $taskId); |
|
| 105 | $statement->bindParam('task', $data['task']); |
|
| 106 | $statement->bindParam('status', $data['status']); |
|
| 107 | $statement->execute(); |
|
| 108 | $task = $this->checkTask($taskId); |
|
| 109 | ||
| 110 | return $task; |
|
| 111 | } |
|
| 112 | ||
| 113 | /** |
|
| 114 | * Delete a task. |
|
| 115 | * |
|
| 116 | * @param int $taskId |
|
| 117 | * @return string |
|
| 118 | */ |
|
| 119 | public function deleteTask($taskId) |
|
| 120 | { |
|
| 121 | $statement = $this->database->prepare(TaskQuery::DELETE_TASK_QUERY); |
|
| 122 | $statement->bindParam('id', $taskId); |
|
| 123 | $statement->execute(); |
|
| 124 | ||
| 125 | return TaskMessage::TASK_DELETED; |
|
| 126 | } |
|
| 127 | } |
|
| 128 | ||
| @@ 12-127 (lines=116) @@ | ||
| 9 | /** |
|
| 10 | * Users Repository. |
|
| 11 | */ |
|
| 12 | class UserRepository extends BaseRepository |
|
| 13 | { |
|
| 14 | /** |
|
| 15 | * @param object $database |
|
| 16 | */ |
|
| 17 | public function __construct(\PDO $database = null) |
|
| 18 | { |
|
| 19 | $this->database = $database; |
|
| 20 | } |
|
| 21 | ||
| 22 | /** |
|
| 23 | * Check if the user exists. |
|
| 24 | * |
|
| 25 | * @param int $userId |
|
| 26 | * @return array $user |
|
| 27 | * @throws \Exception |
|
| 28 | */ |
|
| 29 | public function checkUser($userId) |
|
| 30 | { |
|
| 31 | $statement = $this->database->prepare(UserQuery::GET_USER_QUERY); |
|
| 32 | $statement->bindParam('id', $userId); |
|
| 33 | $statement->execute(); |
|
| 34 | $user = $statement->fetchObject(); |
|
| 35 | if (empty($user)) { |
|
| 36 | throw new UserException(UserException::USER_NOT_FOUND, 404); |
|
| 37 | } |
|
| 38 | ||
| 39 | return $user; |
|
| 40 | } |
|
| 41 | ||
| 42 | /** |
|
| 43 | * Get all users. |
|
| 44 | * |
|
| 45 | * @return array |
|
| 46 | */ |
|
| 47 | public function getUsers() |
|
| 48 | { |
|
| 49 | $statement = $this->database->prepare(UserQuery::GET_USERS_QUERY); |
|
| 50 | $statement->execute(); |
|
| 51 | ||
| 52 | return $statement->fetchAll(); |
|
| 53 | } |
|
| 54 | ||
| 55 | /** |
|
| 56 | * Search users by name. |
|
| 57 | * |
|
| 58 | * @param string $usersName |
|
| 59 | * @return array |
|
| 60 | * @throws \Exception |
|
| 61 | */ |
|
| 62 | public function searchUsers($usersName) |
|
| 63 | { |
|
| 64 | $statement = $this->database->prepare(UserQuery::SEARCH_USERS_QUERY); |
|
| 65 | $query = '%' . $usersName . '%'; |
|
| 66 | $statement->bindParam('query', $query); |
|
| 67 | $statement->execute(); |
|
| 68 | $users = $statement->fetchAll(); |
|
| 69 | if (!$users) { |
|
| 70 | throw new UserException(UserException::USER_NOT_FOUND, 404); |
|
| 71 | } |
|
| 72 | ||
| 73 | return $users; |
|
| 74 | } |
|
| 75 | ||
| 76 | /** |
|
| 77 | * Create a user. |
|
| 78 | * |
|
| 79 | * @param array $data |
|
| 80 | * @return array |
|
| 81 | * @throws \Exception |
|
| 82 | */ |
|
| 83 | public function createUser($data) |
|
| 84 | { |
|
| 85 | $statement = $this->database->prepare(UserQuery::CREATE_USER_QUERY); |
|
| 86 | $statement->bindParam('name', $data['name']); |
|
| 87 | $statement->bindParam('email', $data['email']); |
|
| 88 | $statement->execute(); |
|
| 89 | $user = $this->checkUser($this->database->lastInsertId()); |
|
| 90 | ||
| 91 | return $user; |
|
| 92 | } |
|
| 93 | ||
| 94 | /** |
|
| 95 | * Update a user. |
|
| 96 | * |
|
| 97 | * @param array $data |
|
| 98 | * @param int $userId |
|
| 99 | * @return array |
|
| 100 | */ |
|
| 101 | public function updateUser($data, $userId) |
|
| 102 | { |
|
| 103 | $statement = $this->database->prepare(UserQuery::UPDATE_USER_QUERY); |
|
| 104 | $statement->bindParam('id', $userId); |
|
| 105 | $statement->bindParam('name', $data['name']); |
|
| 106 | $statement->bindParam('email', $data['email']); |
|
| 107 | $statement->execute(); |
|
| 108 | $user = $this->checkUser($userId); |
|
| 109 | ||
| 110 | return $user; |
|
| 111 | } |
|
| 112 | ||
| 113 | /** |
|
| 114 | * Delete a user. |
|
| 115 | * |
|
| 116 | * @param int $userId |
|
| 117 | * @return string |
|
| 118 | */ |
|
| 119 | public function deleteUser($userId) |
|
| 120 | { |
|
| 121 | $statement = $this->database->prepare(UserQuery::DELETE_USER_QUERY); |
|
| 122 | $statement->bindParam('id', $userId); |
|
| 123 | $statement->execute(); |
|
| 124 | ||
| 125 | return UserMessage::USER_DELETED; |
|
| 126 | } |
|
| 127 | } |
|
| 128 | ||