Skip to content

Commit

Permalink
fixup! use count for better performance
Browse files Browse the repository at this point in the history
Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>
  • Loading branch information
susnux committed Dec 21, 2023
1 parent 81e7ba9 commit 724e1b6
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
12 changes: 9 additions & 3 deletions lib/Db/SubmissionMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,16 +110,22 @@ public function findParticipantsByForm(int $formId): array {
}

/**
* Count submissions by form and optionally also by userId
* @param int $formId ID of the form to count submissions for
* @param string|null $userId optionally limit submissions to the one of that user
* @throws \Exception
*/
public function countSubmissions(int $formId): int {
public function countSubmissions(int $formId, ?string $userId = null): int {
$qb = $this->db->getQueryBuilder();

$qb->select($qb->func()->count('*', 'num_submissions'))
$query = $qb->select($qb->func()->count('*', 'num_submissions'))
->from($this->getTableName())
->where($qb->expr()->eq('form_id', $qb->createNamedParameter($formId, IQueryBuilder::PARAM_INT)));
if (!is_null($userId)) {
$query->andWhere($qb->expr()->eq('user_id', $qb->createNamedParameter($userId, IQueryBuilder::PARAM_STR)));
}

$result = $qb->executeQuery();
$result = $query->executeQuery();
$row = $result->fetch();
$result->closeCursor();

Expand Down
8 changes: 1 addition & 7 deletions lib/Service/SubmissionService.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,13 +158,7 @@ public function getSubmissions(int $formId): array {
* Validate the new submission is unique
*/
public function isUniqueSubmission(Submission $newSubmission): bool {
$submissions = $this->submissionMapper->findByForm($newSubmission->getFormId());
foreach ($submissions as $submission) {
if ($submission->getUserId() === $newSubmission->getUserId() && $submission->getId() !== $newSubmission->getId()) {
return false;
}
}
return true;
return $this->submissionMapper->countSubmissions($newSubmission->getFormId(), $newSubmission->getUserId()) === 1;
}

/**
Expand Down

0 comments on commit 724e1b6

Please sign in to comment.