Skip to content

Commit

Permalink
feat: add API v3 routes for questions
Browse files Browse the repository at this point in the history
Signed-off-by: Christian Hartmann <chris-hartmann@gmx.de>
  • Loading branch information
Chartman123 committed Jun 25, 2024
1 parent f810413 commit b52972b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
4 changes: 4 additions & 0 deletions appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,16 @@
],

// API routes v3
// Forms
['name' => 'api#getForms', 'url' => '/api/{apiVersion}/forms', 'verb' => 'GET', 'requirements' => $requirements_v3],
['name' => 'api#newForm', 'url' => '/api/{apiversion}/forms', 'verb' => 'POST', 'requirements' => $requirements_v3],
['name' => 'api#getForm', 'url' => '/api/{apiVersion}/forms/{id}', 'verb' => 'GET', 'requirements' => $requirements_v3],
['name' => 'api#updateForm', 'url' => '/api/{apiVersion}/forms/{id}', 'verb' => 'PATCH', 'requirements' => $requirements_v3],
['name' => 'api#deleteForm', 'url' => '/api/{apiVersion}/forms/{id}', 'verb' => 'DELETE', 'requirements' => $requirements_v3],

// Questions
['name' => 'api#getQuestions', 'url' => '/api/{apiVersion}/forms/{id}/questions', 'verb' => 'GET', 'requirements' => $requirements_v3],

// Legacy v2 routes (TODO: remove with Forms v5)
// Forms
['name' => 'api#getFormsLegacy', 'url' => '/api/{apiVersion}/forms', 'verb' => 'GET', 'requirements' => [
Expand Down
31 changes: 31 additions & 0 deletions lib/Controller/ApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ public function __construct(
}

// API v3 methods
// Forms
/**
* @CORS
* @NoAdminRequired
Expand Down Expand Up @@ -357,6 +358,36 @@ public function deleteForm(int $id): DataResponse {
return new DataResponse($id);

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

View check run for this annotation

Codecov / codecov/patch

lib/Controller/ApiController.php#L358

Added line #L358 was not covered by tests
}

// Questions
/**
* @CORS
* @NoAdminRequired
*
* Read all questions (including options)
*
* @param int $id FormId
* @return DataResponse
* @throws OCSBadRequestException
* @throws OCSForbiddenException
*/
public function getQuestions(int $id): DataResponse {

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

View check run for this annotation

Codecov / codecov/patch

lib/Controller/ApiController.php#L373

Added line #L373 was not covered by tests
try {
$form = $this->formMapper->findById($id);
} catch (IMapperException $e) {
$this->logger->debug('Could not find form');
throw new OCSBadRequestException();

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

View check run for this annotation

Codecov / codecov/patch

lib/Controller/ApiController.php#L375-L378

Added lines #L375 - L378 were not covered by tests
}

if (!$this->formsService->hasUserAccess($form)) {
$this->logger->debug('User has no permissions to get this form');
throw new OCSForbiddenException();

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

View check run for this annotation

Codecov / codecov/patch

lib/Controller/ApiController.php#L381-L383

Added lines #L381 - L383 were not covered by tests
}

$questionData = $this->formsService->getQuestions($id);;

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

View check run for this annotation

Codecov / codecov/patch

lib/Controller/ApiController.php#L386

Added line #L386 was not covered by tests

return new DataResponse($questionData);

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

View check run for this annotation

Codecov / codecov/patch

lib/Controller/ApiController.php#L388

Added line #L388 was not covered by tests
}

/*
/* Legacy API v2 methods (TODO: remove with Forms v5)
*/
Expand Down

0 comments on commit b52972b

Please sign in to comment.