|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace App\Repository; |
|
6
|
|
|
|
|
7
|
|
|
use App\Exception\TaskException; |
|
8
|
|
|
use App\Entity\Task; |
|
9
|
|
|
|
|
10
|
|
|
final class TaskRepository extends BaseRepository |
|
11
|
|
|
{ |
|
12
|
8 |
|
public function checkAndGetTask(int $taskId, int $userId): Task |
|
13
|
|
|
{ |
|
14
|
8 |
|
$query = ' |
|
15
|
|
|
SELECT * FROM `tasks` |
|
16
|
|
|
WHERE `id` = :id AND `userId` = :userId |
|
17
|
|
|
'; |
|
18
|
8 |
|
$statement = $this->getDb()->prepare($query); |
|
19
|
8 |
|
$statement->bindParam('id', $taskId); |
|
20
|
8 |
|
$statement->bindParam('userId', $userId); |
|
21
|
8 |
|
$statement->execute(); |
|
22
|
8 |
|
$task = $statement->fetchObject(Task::class); |
|
23
|
8 |
|
if (!$task) { |
|
24
|
4 |
|
throw new TaskException('Task not found.', 404); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
4 |
|
return $task; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
1 |
|
public function getAllTasks(): array |
|
31
|
|
|
{ |
|
32
|
1 |
|
$query = 'SELECT * FROM `tasks` ORDER BY `id`'; |
|
33
|
1 |
|
$statement = $this->getDb()->prepare($query); |
|
34
|
1 |
|
$statement->execute(); |
|
35
|
|
|
|
|
36
|
1 |
|
return (array) $statement->fetchAll(); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
1 |
|
public function getTasksByUserId(int $userId): array |
|
40
|
|
|
{ |
|
41
|
1 |
|
$query = 'SELECT * FROM `tasks` WHERE `userId` = :userId ORDER BY `id`'; |
|
42
|
1 |
|
$statement = $this->getDb()->prepare($query); |
|
43
|
1 |
|
$statement->bindParam('userId', $userId); |
|
44
|
1 |
|
$statement->execute(); |
|
45
|
|
|
|
|
46
|
1 |
|
return (array) $statement->fetchAll(); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function getQueryTasksByPage(): string |
|
50
|
|
|
{ |
|
51
|
|
|
return " |
|
52
|
|
|
SELECT * |
|
53
|
|
|
FROM `tasks` |
|
54
|
|
|
WHERE `userId` = :userId |
|
55
|
|
|
AND `name` LIKE CONCAT('%', :name, '%') |
|
56
|
|
|
AND `description` LIKE CONCAT('%', :description, '%') |
|
57
|
|
|
AND `status` LIKE CONCAT('%', :status, '%') |
|
58
|
|
|
ORDER BY `id` |
|
59
|
|
|
"; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function getTasksByPage( |
|
63
|
|
|
int $userId, |
|
64
|
|
|
int $page, |
|
65
|
|
|
int $perPage, |
|
66
|
|
|
?string $name, |
|
67
|
|
|
?string $description, |
|
68
|
|
|
?string $status |
|
69
|
|
|
): array { |
|
70
|
|
|
$params = [ |
|
71
|
|
|
'userId' => $userId, |
|
72
|
|
|
'name' => is_null($name) ? '' : $name, |
|
73
|
|
|
'description' => is_null($description) ? '' : $description, |
|
74
|
|
|
'status' => is_null($status) ? '' : $status, |
|
75
|
|
|
]; |
|
76
|
|
|
$query = $this->getQueryTasksByPage(); |
|
77
|
|
|
$statement = $this->getDb()->prepare($query); |
|
78
|
|
|
$statement->bindParam('userId', $params['userId']); |
|
79
|
|
|
$statement->bindParam('name', $params['name']); |
|
80
|
|
|
$statement->bindParam('description', $params['description']); |
|
81
|
|
|
$statement->bindParam('status', $params['status']); |
|
82
|
|
|
$statement->execute(); |
|
83
|
|
|
$total = $statement->rowCount(); |
|
84
|
|
|
|
|
85
|
|
|
return $this->getResultsWithPagination( |
|
86
|
|
|
$query, |
|
87
|
|
|
$page, |
|
88
|
|
|
$perPage, |
|
89
|
|
|
$params, |
|
90
|
|
|
$total |
|
91
|
|
|
); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
4 |
|
private function getSearchTasksQuery(?int $status): string |
|
95
|
|
|
{ |
|
96
|
4 |
|
$statusQuery = ''; |
|
97
|
4 |
|
if ($status === 0 || $status === 1) { |
|
98
|
2 |
|
$statusQuery = 'AND `status` = :status'; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
4 |
|
return " |
|
102
|
|
|
SELECT * FROM `tasks` |
|
103
|
4 |
|
WHERE `name` LIKE :name AND `userId` = :userId ${statusQuery} |
|
104
|
|
|
ORDER BY `id` |
|
105
|
|
|
"; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
4 |
|
public function search(string $tasksName, int $userId, ?int $status): array |
|
109
|
|
|
{ |
|
110
|
4 |
|
$query = $this->getSearchTasksQuery($status); |
|
111
|
4 |
|
$name = '%' . $tasksName . '%'; |
|
112
|
4 |
|
$statement = $this->getDb()->prepare($query); |
|
113
|
4 |
|
$statement->bindParam('name', $name); |
|
114
|
4 |
|
$statement->bindParam('userId', $userId); |
|
115
|
4 |
|
if ($status === 0 || $status === 1) { |
|
116
|
2 |
|
$statement->bindParam('status', $status); |
|
117
|
|
|
} |
|
118
|
4 |
|
$statement->execute(); |
|
119
|
4 |
|
$tasks = (array) $statement->fetchAll(); |
|
120
|
4 |
|
if (!$tasks) { |
|
|
|
|
|
|
121
|
|
|
$message = 'No Tasks were found with that name.'; |
|
122
|
|
|
throw new TaskException($message, 404); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
4 |
|
return $tasks; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
1 |
|
public function create(Task $task): Task |
|
129
|
|
|
{ |
|
130
|
1 |
|
$query = ' |
|
131
|
|
|
INSERT INTO `tasks` |
|
132
|
|
|
(`name`, `description`, `status`, `userId`, `createdAt`) |
|
133
|
|
|
VALUES |
|
134
|
|
|
(:name, :description, :status, :userId, :createdAt) |
|
135
|
|
|
'; |
|
136
|
1 |
|
$statement = $this->getDb()->prepare($query); |
|
137
|
1 |
|
$name = $task->getName(); |
|
138
|
1 |
|
$desc = $task->getDescription(); |
|
139
|
1 |
|
$status = $task->getStatus(); |
|
140
|
1 |
|
$userId = $task->getUserId(); |
|
141
|
1 |
|
$created = $task->getCreatedAt(); |
|
142
|
1 |
|
$statement->bindParam('name', $name); |
|
143
|
1 |
|
$statement->bindParam('description', $desc); |
|
144
|
1 |
|
$statement->bindParam('status', $status); |
|
145
|
1 |
|
$statement->bindParam('userId', $userId); |
|
146
|
1 |
|
$statement->bindParam('createdAt', $created); |
|
147
|
1 |
|
$statement->execute(); |
|
148
|
|
|
|
|
149
|
1 |
|
$taskId = (int) $this->getDb()->lastInsertId(); |
|
150
|
|
|
|
|
151
|
1 |
|
return $this->checkAndGetTask((int) $taskId, (int) $userId); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
1 |
|
public function update(Task $task): Task |
|
155
|
|
|
{ |
|
156
|
1 |
|
$query = ' |
|
157
|
|
|
UPDATE `tasks` |
|
158
|
|
|
SET |
|
159
|
|
|
`name` = :name, |
|
160
|
|
|
`description` = :description, |
|
161
|
|
|
`status` = :status, |
|
162
|
|
|
`updatedAt` = :updatedAt |
|
163
|
|
|
WHERE `id` = :id AND `userId` = :userId |
|
164
|
|
|
'; |
|
165
|
1 |
|
$statement = $this->getDb()->prepare($query); |
|
166
|
1 |
|
$id = $task->getId(); |
|
167
|
1 |
|
$name = $task->getName(); |
|
168
|
1 |
|
$desc = $task->getDescription(); |
|
169
|
1 |
|
$status = $task->getStatus(); |
|
170
|
1 |
|
$userId = $task->getUserId(); |
|
171
|
1 |
|
$updated = $task->getUpdatedAt(); |
|
172
|
1 |
|
$statement->bindParam('id', $id); |
|
173
|
1 |
|
$statement->bindParam('name', $name); |
|
174
|
1 |
|
$statement->bindParam('description', $desc); |
|
175
|
1 |
|
$statement->bindParam('status', $status); |
|
176
|
1 |
|
$statement->bindParam('userId', $userId); |
|
177
|
1 |
|
$statement->bindParam('updatedAt', $updated); |
|
178
|
1 |
|
$statement->execute(); |
|
179
|
|
|
|
|
180
|
1 |
|
return $this->checkAndGetTask((int) $id, (int) $userId); |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
1 |
|
public function delete(int $taskId, int $userId): void |
|
184
|
|
|
{ |
|
185
|
1 |
|
$query = 'DELETE FROM `tasks` WHERE `id` = :id AND `userId` = :userId'; |
|
186
|
1 |
|
$statement = $this->getDb()->prepare($query); |
|
187
|
1 |
|
$statement->bindParam('id', $taskId); |
|
188
|
1 |
|
$statement->bindParam('userId', $userId); |
|
189
|
1 |
|
$statement->execute(); |
|
190
|
1 |
|
} |
|
191
|
|
|
} |
|
192
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.