1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace App\Repository; |
6
|
|
|
|
7
|
|
|
use App\Exception\TaskException; |
8
|
|
|
|
9
|
|
|
class TaskRepository extends BaseRepository |
10
|
|
|
{ |
11
|
|
|
public function __construct(\PDO $database) |
12
|
|
|
{ |
13
|
|
|
$this->database = $database; |
14
|
|
|
} |
15
|
|
|
|
16
|
|
|
public function checkAndGetTask(int $taskId, int $userId) |
17
|
|
|
{ |
18
|
|
|
$query = 'SELECT * FROM `tasks` WHERE `id` = :id AND `userId` = :userId'; |
19
|
|
|
$statement = $this->getDb()->prepare($query); |
20
|
|
|
$statement->bindParam('id', $taskId); |
21
|
|
|
$statement->bindParam('userId', $userId); |
22
|
|
|
$statement->execute(); |
23
|
|
|
$task = $statement->fetchObject(); |
24
|
|
|
if (empty($task)) { |
25
|
|
|
throw new TaskException('Task not found.', 404); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
return $task; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function getAllTasks(): array |
32
|
|
|
{ |
33
|
|
|
$query = 'SELECT * FROM `tasks` ORDER BY `id`'; |
34
|
|
|
$statement = $this->getDb()->prepare($query); |
35
|
|
|
$statement->execute(); |
36
|
|
|
|
37
|
|
|
return $statement->fetchAll(); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
View Code Duplication |
public function getAll(int $userId): array |
|
|
|
|
41
|
|
|
{ |
42
|
|
|
$query = 'SELECT * FROM `tasks` WHERE `userId` = :userId ORDER BY `id`'; |
43
|
|
|
$statement = $this->getDb()->prepare($query); |
44
|
|
|
$statement->bindParam('userId', $userId); |
45
|
|
|
$statement->execute(); |
46
|
|
|
|
47
|
|
|
return $statement->fetchAll(); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function search($tasksName, int $userId, $status): array |
51
|
|
|
{ |
52
|
|
|
$query = $this->getSearchTasksQuery($status); |
53
|
|
|
$name = '%' . $tasksName . '%'; |
54
|
|
|
$statement = $this->getDb()->prepare($query); |
55
|
|
|
$statement->bindParam('name', $name); |
56
|
|
|
$statement->bindParam('userId', $userId); |
57
|
|
|
if ($status === 0 || $status === 1) { |
58
|
|
|
$statement->bindParam('status', $status); |
59
|
|
|
} |
60
|
|
|
$statement->execute(); |
61
|
|
|
|
62
|
|
|
return $statement->fetchAll(); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
private function getSearchTasksQuery($status) |
66
|
|
|
{ |
67
|
|
|
$statusQuery = ''; |
68
|
|
|
if ($status === 0 || $status === 1) { |
69
|
|
|
$statusQuery = 'AND `status` = :status'; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return " |
73
|
|
|
SELECT * FROM `tasks` |
74
|
|
|
WHERE `name` LIKE :name AND `userId` = :userId $statusQuery |
75
|
|
|
ORDER BY `id` |
76
|
|
|
"; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function create($task) |
80
|
|
|
{ |
81
|
|
|
$query = ' |
82
|
|
|
INSERT INTO `tasks` (`name`, `description`, `status`, `userId`) |
83
|
|
|
VALUES (:name, :description, :status, :userId) |
84
|
|
|
'; |
85
|
|
|
$statement = $this->getDb()->prepare($query); |
86
|
|
|
$statement->bindParam('name', $task->name); |
87
|
|
|
$statement->bindParam('description', $task->description); |
88
|
|
|
$statement->bindParam('status', $task->status); |
89
|
|
|
$statement->bindParam('userId', $task->userId); |
90
|
|
|
$statement->execute(); |
91
|
|
|
|
92
|
|
|
return $this->checkAndGetTask((int) $this->database->lastInsertId(), (int) $task->userId); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function update($task) |
96
|
|
|
{ |
97
|
|
|
$query = ' |
98
|
|
|
UPDATE `tasks` |
99
|
|
|
SET `name` = :name, `description` = :description, `status` = :status |
100
|
|
|
WHERE `id` = :id AND `userId` = :userId |
101
|
|
|
'; |
102
|
|
|
$statement = $this->getDb()->prepare($query); |
103
|
|
|
$statement->bindParam('id', $task->id); |
104
|
|
|
$statement->bindParam('name', $task->name); |
105
|
|
|
$statement->bindParam('description', $task->description); |
106
|
|
|
$statement->bindParam('status', $task->status); |
107
|
|
|
$statement->bindParam('userId', $task->userId); |
108
|
|
|
$statement->execute(); |
109
|
|
|
|
110
|
|
|
return $this->checkAndGetTask((int) $task->id, (int) $task->userId); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
View Code Duplication |
public function delete(int $taskId, int $userId): string |
|
|
|
|
114
|
|
|
{ |
115
|
|
|
$query = 'DELETE FROM `tasks` WHERE `id` = :id AND `userId` = :userId'; |
116
|
|
|
$statement = $this->getDb()->prepare($query); |
117
|
|
|
$statement->bindParam('id', $taskId); |
118
|
|
|
$statement->bindParam('userId', $userId); |
119
|
|
|
$statement->execute(); |
120
|
|
|
|
121
|
|
|
return 'The task was deleted.'; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.