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 <[email protected]>
  • Loading branch information
susnux committed Dec 21, 2023
1 parent 81e7ba9 commit 7b7b92b
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 46 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 {

Check warning on line 118 in lib/Db/SubmissionMapper.php

View check run for this annotation

Codecov / codecov/patch

lib/Db/SubmissionMapper.php#L118

Added line #L118 was not covered by tests
$qb = $this->db->getQueryBuilder();

$qb->select($qb->func()->count('*', 'num_submissions'))
$query = $qb->select($qb->func()->count('*', 'num_submissions'))

Check warning on line 121 in lib/Db/SubmissionMapper.php

View check run for this annotation

Codecov / codecov/patch

lib/Db/SubmissionMapper.php#L121

Added line #L121 was not covered by tests
->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)));

Check warning on line 125 in lib/Db/SubmissionMapper.php

View check run for this annotation

Codecov / codecov/patch

lib/Db/SubmissionMapper.php#L124-L125

Added lines #L124 - L125 were not covered by tests
}

$result = $qb->executeQuery();
$result = $query->executeQuery();

Check warning on line 128 in lib/Db/SubmissionMapper.php

View check run for this annotation

Codecov / codecov/patch

lib/Db/SubmissionMapper.php#L128

Added line #L128 was not covered by tests
$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
42 changes: 6 additions & 36 deletions tests/Unit/Service/SubmissionServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,43 +131,24 @@ public function setUp(): void {
/**
* @dataProvider dataIsUniqueSubmission
*/
public function testIsUniqueSubmission(array $submissionData, array $otherSubmissionsData, bool $expected) {
$this->submissionMapper->method('findByForm')
->willReturn(array_map(fn ($data) => Submission::fromParams($data), $otherSubmissionsData));
public function testIsUniqueSubmission(array $submissionData, int $numberOfSubmissions, bool $expected) {
$this->submissionMapper->method('countSubmissions')
->with($submissionData['formId'], $submissionData['userId'])
->willReturn($numberOfSubmissions);

$submission = Submission::fromParams($submissionData);
$this->assertEquals($expected, $this->submissionService->isUniqueSubmission($submission));
}

public function dataIsUniqueSubmission() {
return [
[
'submissionData' => [
'id' => 3,
'userId' => 'user',
'formId' => 1,
],
'otherSubmissionData' => [
[
'id' => 1,
'userId' => 'other',
'formId' => 1,
],
[
'id' => 2,
'userId' => 'yetAnother',
'formId' => 1,
],
],
'expected' => true,
],
[
'submissionData' => [
'id' => 1,
'userId' => 'user',
'formId' => 1,
],
'otherSubmissionData' => [],
'numberOfSubmissions' => 1,
'expected' => true,
],
[
Expand All @@ -176,18 +157,7 @@ public function dataIsUniqueSubmission() {
'userId' => 'user',
'formId' => 1,
],
'otherSubmissionData' => [
[
'id' => 1,
'userId' => 'other',
'formId' => 1,
],
[
'id' => 2,
'formId' => 1,
'userId' => 'user', // conflict
],
],
'numberOfSubmissions' => 2,
'expected' => false,
],
];
Expand Down

0 comments on commit 7b7b92b

Please sign in to comment.