Passed
Pull Request — master (#1284)
by René
04:02
created

VoteMapper::deleteByPoll()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 1
dl 0
loc 9
ccs 0
cts 6
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2017 Vinzenz Rosenkranz <[email protected]>
4
 *
5
 * @author Vinzenz Rosenkranz <[email protected]>
6
 * @author René Gieling <[email protected]>
7
 *
8
 * @license GNU AGPL version 3 or any later version
9
 *
10
 *  This program is free software: you can redistribute it and/or modify
11
 *  it under the terms of the GNU Affero General Public License as
12
 *  published by the Free Software Foundation, either version 3 of the
13
 *  License, or (at your option) any later version.
14
 *
15
 *  This program is distributed in the hope that it will be useful,
16
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 *  GNU Affero General Public License for more details.
19
 *
20
 *  You should have received a copy of the GNU Affero General Public License
21
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
 *
23
 */
24
25
namespace OCA\Polls\Db;
26
27
use OCP\DB\QueryBuilder\IQueryBuilder;
28
use OCP\IDBConnection;
29
use OCP\AppFramework\Db\QBMapper;
30
use Doctrine\DBAL\Exception\TableNotFoundException;
31
32
/**
33
 * @template-extends QBMapper<Vote>
34
 */
35
class VoteMapper extends QBMapper {
36
	public function __construct(IDBConnection $db) {
37 1
		parent::__construct($db, 'polls_votes', '\OCA\Polls\Db\Vote');
38 1
	}
39 1
40
	/**
41
	 * @throws \OCP\AppFramework\Db\DoesNotExistException if not found
42
	 * @return Vote[]
43
	 */
44
	public function findByPoll(int $pollId) {
45
		$qb = $this->db->getQueryBuilder();
46
47
		$qb->select('*')
48
		   ->from($this->getTableName())
49
		   ->where(
50
			   $qb->expr()->eq('poll_id', $qb->createNamedParameter($pollId, IQueryBuilder::PARAM_INT))
51
		   );
52
53
		return $this->findEntities($qb);
54
	}
55
56
	/**
57
	 * @throws \OCP\AppFramework\Db\DoesNotExistException if not found
58
	 * @return Vote[]
59
	 */
60
	public function findByPollAndUser(int $pollId, string $userId) {
61
		$qb = $this->db->getQueryBuilder();
62
63
		$qb->select('*')
64
		   ->from($this->getTableName())
65
		   ->where(
66
			   $qb->expr()->eq('poll_id', $qb->createNamedParameter($pollId, IQueryBuilder::PARAM_INT))
67
		   )
68
		   ->andWhere(
69
			   $qb->expr()->eq('user_id', $qb->createNamedParameter($userId, IQueryBuilder::PARAM_STR))
70
		   );
71
72
		return $this->findEntities($qb);
73
	}
74
75
	/**
76
	 * @throws \OCP\AppFramework\Db\DoesNotExistException if not found
77
	 * @return Vote
78
	 */
79
	public function findSingleVote(int $pollId, string $optionText, string $userId) {
80
		$qb = $this->db->getQueryBuilder();
81
82
		$qb->select('*')
83
		   ->from($this->getTableName())
84
		   ->where(
85
			   $qb->expr()->eq('poll_id', $qb->createNamedParameter($pollId, IQueryBuilder::PARAM_INT))
86
		   )
87
		   ->andWhere(
88
			   $qb->expr()->eq('vote_option_text', $qb->createNamedParameter($optionText, IQueryBuilder::PARAM_STR))
89
		   )
90
		   ->andWhere(
91
			   $qb->expr()->eq('user_id', $qb->createNamedParameter($userId, IQueryBuilder::PARAM_STR))
92
		   );
93
		return $this->findEntity($qb);
94
	}
95
96
	/**
97
	 * @throws \OCP\AppFramework\Db\DoesNotExistException if not found
98
	 * @return Vote[]
99
	 */
100
	public function findParticipantsByPoll(int $pollId) {
101
		$qb = $this->db->getQueryBuilder();
102
103
		$qb->selectDistinct('user_id')
104
		   ->from($this->getTableName())
105
		   ->where(
106
			   $qb->expr()->eq('poll_id', $qb->createNamedParameter($pollId, IQueryBuilder::PARAM_INT))
107
		   );
108
109
		return $this->findEntities($qb);
110
	}
111
112
	/**
113
	 * @throws \OCP\AppFramework\Db\DoesNotExistException if not found
114
	 *
115
	 * @return Vote[]
116
	 *
117
	 * @psalm-return array<array-key, Vote>
118
	 */
119
	public function findParticipantsVotes(int $pollId, $userId): array {
120
		$qb = $this->db->getQueryBuilder();
121
122
		$qb->select('*')
123
		   ->from($this->getTableName())
124
		   ->where(
125
			   $qb->expr()->eq('poll_id', $qb->createNamedParameter($pollId, IQueryBuilder::PARAM_INT))
126
		   )
127
		   ->andWhere(
128
			   $qb->expr()->eq('user_id', $qb->createNamedParameter($userId, IQueryBuilder::PARAM_STR))
129
		   );
130
131
		return $this->findEntities($qb);
132
	}
133
134
	public function deleteByPollAndUser(int $pollId, string $userId): void {
135
		$qb = $this->db->getQueryBuilder();
136
137
		$qb->delete($this->getTableName())
138
		   ->where(
139
			   $qb->expr()->eq('poll_id', $qb->createNamedParameter($pollId, IQueryBuilder::PARAM_INT))
140
		   )
141
		   ->andWhere(
142
			   $qb->expr()->eq('user_id', $qb->createNamedParameter($userId, IQueryBuilder::PARAM_STR))
143
		   );
144
145
		$qb->execute();
146
	}
147
148
	public function deleteByPoll($pollId): void {
149
		$qb = $this->db->getQueryBuilder();
150
151
		$qb->delete($this->getTableName())
152
		->where(
153
			$qb->expr()->eq('poll_id', $qb->createNamedParameter($pollId, IQueryBuilder::PARAM_INT))
154
		);
155
156
		$qb->execute();
157
	}
158
159
	/**
160
	 * @return void
161
	 */
162
	public function removeDuplicates() {
163
		try {
164
			$query = $this->db->getQueryBuilder();
165
			$query->select('id', 'poll_id', 'user_id', 'vote_option_text')
166
				->from($this->getTableName());
167
			$foundEntries = $query->execute();
168
169
			$delete = $this->db->getQueryBuilder();
170
			$delete->delete($this->getTableName())
171
				->where('id = :id');
172
173
			$entries2Keep = [];
174
175
			while ($row = $foundEntries->fetch()) {
176
				$currentRecord = [
177
					$row['poll_id'],
178
					$row['user_id'],
179
					$row['vote_option_text']
180
				];
181
				if (in_array($currentRecord, $entries2Keep)) {
182
					$delete->setParameter('id', $row['id']);
183
					$delete->execute();
184
				} else {
185
					$entries2Keep[] = $currentRecord;
186
				}
187
			}
188
		} catch (TableNotFoundException $e) {
189
			// ignore
190
		}
191
	}
192
}
193