26
26
27
27
namespace OCA \Polls \Db ;
28
28
29
+ use Exception ;
29
30
use OCA \Polls \Exceptions \ShareNotFoundException ;
30
31
use OCA \Polls \Model \UserBase ;
31
32
use OCP \AppFramework \Db \DoesNotExistException ;
@@ -56,28 +57,19 @@ public function __construct(
56
57
* @psalm-return array<array-key, Share>
57
58
*/
58
59
public function findByPoll (int $ pollId , bool $ getDeleted = false ): array {
59
-
60
60
$ qb = $ this ->db ->getQueryBuilder ();
61
61
62
- $ qb ->select ('shares.* ' )
63
- ->from ($ this ->getTableName (), 'shares ' )
64
- ->groupBy ('shares.id ' )
65
- ->where ($ qb ->expr ()->eq ('shares.poll_id ' , $ qb ->createNamedParameter ($ pollId , IQueryBuilder::PARAM_INT )))
66
- ->leftJoin (
67
- 'shares ' ,
68
- Vote::TABLE ,
69
- 'votes ' ,
70
- $ qb ->expr ()->andX (
71
- $ qb ->expr ()->eq ('shares.poll_id ' , 'votes.poll_id ' ),
72
- $ qb ->expr ()->eq ('shares.user_id ' , 'votes.user_id ' ),
73
- )
74
- )
75
- ->addSelect ($ qb ->func ()->count ('votes.id ' , 'voted ' ));
62
+ $ qb ->select (self ::TABLE . '.* ' )
63
+ ->from ($ this ->getTableName (), self ::TABLE )
64
+ ->groupBy (self ::TABLE . '.id ' )
65
+ ->where ($ qb ->expr ()->eq (self ::TABLE . '.poll_id ' , $ qb ->createNamedParameter ($ pollId , IQueryBuilder::PARAM_INT )));
76
66
77
67
if (!$ getDeleted ) {
78
- $ qb ->andWhere ($ qb ->expr ()->eq (' shares ' . '.deleted ' , $ qb ->createNamedParameter (0 , IQueryBuilder::PARAM_INT )));
68
+ $ qb ->andWhere ($ qb ->expr ()->eq (self :: TABLE . '.deleted ' , $ qb ->createNamedParameter (0 , IQueryBuilder::PARAM_INT )));
79
69
}
80
70
71
+ $ this ->joinUserVoteCount ($ qb , self ::TABLE );
72
+
81
73
return $ this ->findEntities ($ qb );
82
74
}
83
75
@@ -127,18 +119,20 @@ public function findByPollUnreminded(int $pollId, bool $getDeleted = false): arr
127
119
public function findByPollAndUser (int $ pollId , string $ userId , bool $ findDeleted = false ): Share {
128
120
$ qb = $ this ->db ->getQueryBuilder ();
129
121
130
- $ qb ->select ('* ' )
131
- ->from ($ this ->getTableName ())
132
- ->where ($ qb ->expr ()->eq ('poll_id ' , $ qb ->createNamedParameter ($ pollId , IQueryBuilder::PARAM_INT )))
133
- ->andWhere ($ qb ->expr ()->eq ('user_id ' , $ qb ->createNamedParameter ($ userId , IQueryBuilder::PARAM_STR )));
122
+ $ qb ->select (self ::TABLE . '.* ' )
123
+ ->from ($ this ->getTableName (), self ::TABLE )
124
+ ->where ($ qb ->expr ()->eq (self ::TABLE . '.poll_id ' , $ qb ->createNamedParameter ($ pollId , IQueryBuilder::PARAM_INT )))
125
+ ->andWhere ($ qb ->expr ()->eq (self ::TABLE . '.user_id ' , $ qb ->createNamedParameter ($ userId , IQueryBuilder::PARAM_STR )))
126
+ ->andWhere ($ qb ->expr ()->isNotNull (self ::TABLE . '.id ' ));
134
127
135
128
if (!$ findDeleted ) {
136
- $ qb ->andWhere ($ qb ->expr ()->eq (' deleted ' , $ qb ->createNamedParameter (0 , IQueryBuilder::PARAM_INT )));
129
+ $ qb ->andWhere ($ qb ->expr ()->eq (self :: TABLE . ' . deleted ' , $ qb ->createNamedParameter (0 , IQueryBuilder::PARAM_INT )));
137
130
}
131
+ $ this ->joinUserVoteCount ($ qb , self ::TABLE );
138
132
139
133
try {
140
134
return $ this ->findEntity ($ qb );
141
- } catch (DoesNotExistException $ e ) {
135
+ } catch (Exception $ e ) {
142
136
throw new ShareNotFoundException ("Share not found by userId and pollId " );
143
137
}
144
138
}
@@ -165,13 +159,15 @@ public function getReplacement(int $pollId, string $userId): Share {
165
159
public function findByToken (string $ token , bool $ getDeleted = false ): Share {
166
160
$ qb = $ this ->db ->getQueryBuilder ();
167
161
168
- $ qb ->select (' * ' )
169
- ->from ($ this ->getTableName ())
170
- ->where ($ qb ->expr ()->eq (' token ' , $ qb ->createNamedParameter ($ token , IQueryBuilder::PARAM_STR )));
162
+ $ qb ->select (self :: TABLE . ' . * ' )
163
+ ->from ($ this ->getTableName (), self :: TABLE )
164
+ ->where ($ qb ->expr ()->eq (self :: TABLE . ' . token ' , $ qb ->createNamedParameter ($ token , IQueryBuilder::PARAM_STR )));
171
165
172
- // if (!$getDeleted) {
173
- // $qb->andWhere($qb->expr()->eq('deleted', $qb->createNamedParameter(0, IQueryBuilder::PARAM_INT)));
174
- // }
166
+ if (!$ getDeleted ) {
167
+ $ qb ->andWhere ($ qb ->expr ()->eq (self ::TABLE . '.deleted ' , $ qb ->createNamedParameter (0 , IQueryBuilder::PARAM_INT )));
168
+ }
169
+
170
+ $ this ->joinUserVoteCount ($ qb , self ::TABLE );
175
171
176
172
try {
177
173
return $ this ->findEntity ($ qb );
@@ -205,4 +201,25 @@ public function purgeDeletedShares(int $offset): void {
205
201
$ query ->executeStatement ();
206
202
}
207
203
204
+ /**
205
+ * Joins votes count of the share user in the given poll
206
+ */
207
+ protected function joinUserVoteCount (IQueryBuilder &$ qb , string $ fromAlias ): void {
208
+ $ joinAlias = 'votes ' ;
209
+
210
+ $ qb ->addSelect ($ qb ->func ()->count ($ joinAlias . '.id ' , 'voted ' ));
211
+
212
+ $ qb ->leftJoin (
213
+ $ fromAlias ,
214
+ Vote::TABLE ,
215
+ $ joinAlias ,
216
+ $ qb ->expr ()->andX (
217
+ $ qb ->expr ()->eq ($ fromAlias . '.poll_id ' , $ joinAlias . '.poll_id ' ),
218
+ $ qb ->expr ()->eq ($ fromAlias . '.user_id ' , $ joinAlias . '.user_id ' ),
219
+ )
220
+ );
221
+ // avoid result with nulled columns
222
+ $ qb ->groupBy ($ fromAlias . '.id ' );
223
+ }
224
+
208
225
}
0 commit comments