Skip to content

Commit

Permalink
refactor lastUpdated for Options
Browse files Browse the repository at this point in the history
Signed-off-by: Christian Hartmann <[email protected]>
  • Loading branch information
Chartman123 committed Sep 10, 2024
1 parent 5a649ef commit 454cb6b
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 8 deletions.
6 changes: 0 additions & 6 deletions lib/Controller/ApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -780,8 +780,6 @@ public function newOption(int $formId, int $questionId, array $optionTexts): Dat
}
}

$this->formMapper->update($form);

return new DataResponse($addedOptions);
}

Expand Down Expand Up @@ -850,8 +848,6 @@ public function updateOption(int $formId, int $questionId, int $optionId, array
// Update changed Columns in Db.
$this->optionMapper->update($option);

$this->formMapper->update($form);

return new DataResponse($option->getId());
}

Expand Down Expand Up @@ -899,8 +895,6 @@ public function deleteOption(int $formId, int $questionId, int $optionId): DataR

$this->optionMapper->delete($option);

$this->formMapper->update($form);

return new DataResponse($optionId);
}

Expand Down
44 changes: 42 additions & 2 deletions lib/Db/OptionMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
namespace OCA\Forms\Db;

use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Db\Entity;
use OCP\AppFramework\Db\QBMapper;
use OCP\IDBConnection;

Expand All @@ -40,16 +41,55 @@ class OptionMapper extends QBMapper {
* OptionMapper constructor.
* @param IDBConnection $db
*/
public function __construct(IDBConnection $db) {
public function __construct(
IDBConnection $db,
private FormMapper $formMapper,
private QuestionMapper $questionMapper
) {
parent::__construct($db, 'forms_v2_options', Option::class);
}

/**
* @param Entity $entity
* @psalm-param Option $entity
* @return Option
* @throws \OCP\DB\Exception
*/
public function insert(Entity $entity): Option {
$formId = $this->questionMapper->findById($entity->getQuestionId())->getFormId();
$this->formMapper->update($this->formMapper->findById($formId));
return parent::insert($entity);
}

/**
* @param Entity $entity
* @psalm-param Option $entity
* @return Option
* @throws \OCP\DB\Exception
*/
public function update(Entity $entity): Option {
$formId = $this->questionMapper->findById($entity->getQuestionId())->getFormId();
$this->formMapper->update($this->formMapper->findById($formId));
return parent::update($entity);
}

/**
* @param Entity $entity
* @psalm-param Option $entity
* @return Option
* @throws \OCP\DB\Exception
*/
public function delete(Entity $entity): Option {
$formId = $this->questionMapper->findById($entity->getQuestionId())->getFormId();
$this->formMapper->update($this->formMapper->findById($formId));
return parent::delete($entity);
}

/**
* @param int $questionId
* @throws DoesNotExistException if not found
* @return Option[]
*/

public function findByQuestion(int $questionId): array {
$qb = $this->db->getQueryBuilder();

Expand Down

0 comments on commit 454cb6b

Please sign in to comment.