From 55404323cff0d8f66467a4ff1535b92754924db7 Mon Sep 17 00:00:00 2001 From: Christian Hartmann Date: Tue, 10 Sep 2024 19:00:50 +0200 Subject: [PATCH] refactor lastUpdated for Options Signed-off-by: Christian Hartmann --- lib/Controller/ApiController.php | 12 --------- lib/Db/OptionMapper.php | 44 ++++++++++++++++++++++++++++++-- 2 files changed, 42 insertions(+), 14 deletions(-) diff --git a/lib/Controller/ApiController.php b/lib/Controller/ApiController.php index eeed3d673..ec8b80a52 100644 --- a/lib/Controller/ApiController.php +++ b/lib/Controller/ApiController.php @@ -780,8 +780,6 @@ public function newOption(int $formId, int $questionId, array $optionTexts): Dat } } - $this->formMapper->update($form); - return new DataResponse($addedOptions); } @@ -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()); } @@ -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); } @@ -1976,8 +1970,6 @@ public function newOptionLegacy(int $questionId, string $text): DataResponse { $option = $this->optionMapper->insert($option); - $this->formMapper->update($form); - return new DataResponse($option->read()); } @@ -2037,8 +2029,6 @@ public function updateOptionLegacy(int $id, array $keyValuePairs): DataResponse // Update changed Columns in Db. $this->optionMapper->update($option); - $this->formMapper->update($form); - return new DataResponse($option->getId()); } @@ -2079,8 +2069,6 @@ public function deleteOptionLegacy(int $id): DataResponse { $this->optionMapper->delete($option); - $this->formMapper->update($form); - return new DataResponse($id); } diff --git a/lib/Db/OptionMapper.php b/lib/Db/OptionMapper.php index c307e9639..47733ee4a 100644 --- a/lib/Db/OptionMapper.php +++ b/lib/Db/OptionMapper.php @@ -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; @@ -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();