Skip to content

Commit

Permalink
update question
Browse files Browse the repository at this point in the history
Signed-off-by: Christian Hartmann <[email protected]>
  • Loading branch information
Chartman123 committed Jul 11, 2024
1 parent 618c0ee commit 99b010f
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 20 deletions.
23 changes: 8 additions & 15 deletions appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@
// Questions
['name' => 'api#getQuestions', 'url' => '/api/{apiVersion}/forms/{id}/questions', 'verb' => 'GET', 'requirements' => $requirements_v3],
['name' => 'api#newQuestion', 'url' => '/api/{apiVersion}/forms/{id}/questions', 'verb' => 'POST', 'requirements' => $requirements_v3],
# ['name' => 'api#getQuestion', 'url' => '/api/{apiVersion}/questions', 'verb' => 'GET', 'requirements' => $requirements_v3],
# ['name' => 'api#getQuestion', 'url' => '/api/{apiVersion}/forms/{id}/questions/{questionId}', 'verb' => 'GET', 'requirements' => $requirements_v3],
['name' => 'api#updateQuestion', 'url' => '/api/{apiVersion}/forms/{id}/questions/{questionId}', 'verb' => 'PATCH', 'requirements' => $requirements_v3],

// Legacy v2 routes (TODO: remove with Forms v5)
// Forms
Expand Down Expand Up @@ -120,21 +121,13 @@
]
],
// TODO: Remove POST in next API release
[
'name' => 'api#updateQuestion',
'url' => '/api/{apiVersion}/question/update',
'verb' => 'POST',
'requirements' => [
'apiVersion' => 'v2(\.[1-4])?'
]
['name' => 'api#updateQuestionLegacy', 'url' => '/api/{apiVersion}/question/update', 'verb' => 'POST', 'requirements' => [
'apiVersion' => 'v2(\.[1-4])?'
]
],
[
'name' => 'api#updateQuestion',
'url' => '/api/{apiVersion}/question/update',
'verb' => 'PATCH',
'requirements' => [
'apiVersion' => 'v2\.[2-4]'
]
['name' => 'api#updateQuestionLegacy', 'url' => '/api/{apiVersion}/question/update', 'verb' => 'PATCH', 'requirements' => [
'apiVersion' => 'v2\.[2-4]'
]
],
// TODO: Remove POST in next API release
[
Expand Down
74 changes: 73 additions & 1 deletion lib/Controller/ApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,78 @@ public function newQuestion(int $id, ?string $type = null, string $text = '', ?i
return new DataResponse($response);

Check warning on line 494 in lib/Controller/ApiController.php

View check run for this annotation

Codecov / codecov/patch

lib/Controller/ApiController.php#L494

Added line #L494 was not covered by tests
}

/**
* @CORS
* @NoAdminRequired
*
* Writes the given key-value pairs into Database.
* Key 'order' should only be changed by reorderQuestions() and is not allowed here.
*
* @param int $id the form id
* @param int $questionId id of question to update
* @param array $keyValuePairs Array of key=>value pairs to update.
* @return DataResponse
* @throws OCSBadRequestException
* @throws OCSForbiddenException
*/
public function updateQuestion(int $id, int $questionId, array $keyValuePairs): DataResponse {
$this->logger->debug('Updating question: formId: {id}, questionId: {id}, values: {keyValuePairs}', [
'id' => $id,
'questionId' => $questionId,
'keyValuePairs' => $keyValuePairs
]);

Check warning on line 516 in lib/Controller/ApiController.php

View check run for this annotation

Codecov / codecov/patch

lib/Controller/ApiController.php#L511-L516

Added lines #L511 - L516 were not covered by tests

try {
$question = $this->questionMapper->findById($questionId);
} catch (IMapperException $e) {
$this->logger->debug('Could not find question');
throw new OCSBadRequestException('Could not find question');

Check warning on line 522 in lib/Controller/ApiController.php

View check run for this annotation

Codecov / codecov/patch

lib/Controller/ApiController.php#L519-L522

Added lines #L519 - L522 were not covered by tests
}

if ($question->getFormId() !== $id) {
throw new OCSBadRequestException('Question doesn\'t belong to given Form');

Check warning on line 526 in lib/Controller/ApiController.php

View check run for this annotation

Codecov / codecov/patch

lib/Controller/ApiController.php#L525-L526

Added lines #L525 - L526 were not covered by tests
}

$form = $this->getFormIfAllowed($id);
if ($this->formsService->isFormArchived($form)) {
$this->logger->debug('This form is archived and can not be modified');
throw new OCSForbiddenException();

Check warning on line 532 in lib/Controller/ApiController.php

View check run for this annotation

Codecov / codecov/patch

lib/Controller/ApiController.php#L529-L532

Added lines #L529 - L532 were not covered by tests
}

// Don't allow empty array
if (sizeof($keyValuePairs) === 0) {
$this->logger->info('Empty keyValuePairs, will not update.');
throw new OCSForbiddenException();

Check warning on line 538 in lib/Controller/ApiController.php

View check run for this annotation

Codecov / codecov/patch

lib/Controller/ApiController.php#L536-L538

Added lines #L536 - L538 were not covered by tests
}

//Don't allow to change id or formId
if (key_exists('id', $keyValuePairs) || key_exists('formId', $keyValuePairs)) {
$this->logger->debug('Not allowed to update id or formId');
throw new OCSForbiddenException();

Check warning on line 544 in lib/Controller/ApiController.php

View check run for this annotation

Codecov / codecov/patch

lib/Controller/ApiController.php#L542-L544

Added lines #L542 - L544 were not covered by tests
}

// Don't allow to reorder here
if (key_exists('order', $keyValuePairs)) {
$this->logger->debug('Key \'order\' is not allowed on updateQuestion. Please use reorderQuestions() to change order.');
throw new OCSForbiddenException('Please use reorderQuestions() to change order');

Check warning on line 550 in lib/Controller/ApiController.php

View check run for this annotation

Codecov / codecov/patch

lib/Controller/ApiController.php#L548-L550

Added lines #L548 - L550 were not covered by tests
}

if (key_exists('extraSettings', $keyValuePairs) && !$this->formsService->areExtraSettingsValid($keyValuePairs['extraSettings'], $question->getType())) {
throw new OCSBadRequestException('Invalid extraSettings, will not update.');

Check warning on line 554 in lib/Controller/ApiController.php

View check run for this annotation

Codecov / codecov/patch

lib/Controller/ApiController.php#L553-L554

Added lines #L553 - L554 were not covered by tests
}

// Create QuestionEntity with given Params & Id.
$question = Question::fromParams($keyValuePairs);
$question->setId($questionId);

Check warning on line 559 in lib/Controller/ApiController.php

View check run for this annotation

Codecov / codecov/patch

lib/Controller/ApiController.php#L558-L559

Added lines #L558 - L559 were not covered by tests

// Update changed Columns in Db.
$this->questionMapper->update($question);

Check warning on line 562 in lib/Controller/ApiController.php

View check run for this annotation

Codecov / codecov/patch

lib/Controller/ApiController.php#L562

Added line #L562 was not covered by tests

$this->formsService->setLastUpdatedTimestamp($id);

Check warning on line 564 in lib/Controller/ApiController.php

View check run for this annotation

Codecov / codecov/patch

lib/Controller/ApiController.php#L564

Added line #L564 was not covered by tests

return new DataResponse($question->getId());

Check warning on line 566 in lib/Controller/ApiController.php

View check run for this annotation

Codecov / codecov/patch

lib/Controller/ApiController.php#L566

Added line #L566 was not covered by tests
}

/*
/* Legacy API v2 methods (TODO: remove with Forms v5)
*/
Expand Down Expand Up @@ -959,7 +1031,7 @@ public function reorderQuestions(int $formId, array $newOrder): DataResponse {
* @throws OCSBadRequestException
* @throws OCSForbiddenException
*/
public function updateQuestion(int $id, array $keyValuePairs): DataResponse {
public function updateQuestionLegacy(int $id, array $keyValuePairs): DataResponse {

Check warning on line 1034 in lib/Controller/ApiController.php

View check run for this annotation

Codecov / codecov/patch

lib/Controller/ApiController.php#L1034

Added line #L1034 was not covered by tests
$this->logger->debug('Updating question: questionId: {id}, values: {keyValuePairs}', [
'id' => $id,
'keyValuePairs' => $keyValuePairs
Expand Down
9 changes: 7 additions & 2 deletions src/mixins/QuestionMixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -363,9 +363,14 @@ export default {
try {
// TODO: add loading status feedback ?
await axios.patch(
generateOcsUrl('apps/forms/api/v2.4/question/update'),
generateOcsUrl(
'apps/forms/api/v3/forms/{id}/questions/{questionId}',
{
id: this.formId,
questionId: this.id,
},
),
{
id: this.id,
keyValuePairs: {
[key]: value,
},
Expand Down
3 changes: 1 addition & 2 deletions tests/Integration/Api/ApiV3Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -787,9 +787,8 @@ public function dataUpdateQuestionProperties() {
* @param array $fullFormExpected
*/
public function testUpdateQuestionProperties(array $fullFormExpected): void {
$resp = $this->http->request('PATCH', 'api/v2.4/question/update', [
$resp = $this->http->request('PATCH', "api/v3/forms/{$this->testForms[0]['id']}/questions/{$this->testForms[0]['questions'][0]['id']}", [
'json' => [
'id' => $this->testForms[0]['questions'][0]['id'],
'keyValuePairs' => [
'isRequired' => false,
'text' => 'Still first Question!'
Expand Down

0 comments on commit 99b010f

Please sign in to comment.