Skip to content

Commit

Permalink
Merge pull request #18 from RRZE-Webteam/FAU-442
Browse files Browse the repository at this point in the history
  • Loading branch information
shvlv authored Sep 4, 2024
2 parents 381097c + fb48bb0 commit 636f846
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/Application/DegreeProgramViewTranslated.php
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,18 @@ public function asArray(): array
];
}

public function asSimplifiedArray(): array
{
return [
DegreeProgram::ID => $this->id->asInt(),
DegreeProgram::SLUG => $this->slug,
self::LINK => $this->link,
DegreeProgram::CAMPO_KEYS => $this->campoKeys->asArray(),
self::DATE => $this->date,
self::MODIFIED => $this->modified,
];
}

public function jsonSerialize(): array
{
return $this->asArray();
Expand Down
42 changes: 42 additions & 0 deletions src/Infrastructure/RestApi/TranslatedDegreeProgramController.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,16 @@ public function register_routes(): void
'args' => $this->get_collection_params(),
],
]);
register_rest_route($this->namespace, '/' . $this->rest_base . '/index', [
[
'methods' => WP_REST_Server::READABLE,
'callback' => [$this, 'getIndex'],
'permission_callback' => [$this, 'get_items_permissions_check'],
'args' => [
'lang' => self::languageParam(),
],
],
]);
register_rest_route($this->namespace, '/' . $this->rest_base . '/(?P<id>[\d]+)', [
[
'methods' => WP_REST_Server::READABLE,
Expand Down Expand Up @@ -176,6 +186,38 @@ public function get_items($request): WP_Error|WP_REST_Response
return $response;
}

public function getIndex(WP_REST_Request $request): WP_Error|WP_REST_Response
{
$criteria = CollectionCriteria::new()
->withoutPagination();

$views = $this->degreeProgramCollectionRepository->findTranslatedCollection(
$criteria,
$this->requestedLanguage($request)
);

if (!$views instanceof PaginationAwareCollection) {
return new WP_Error(
'unexpected_error',
_x(
'Something went wrong. Please try again later.',
'rest_api: response status',
'fau-degree-program-common'
),
['status' => 500]
);
}

$data = [];
foreach ($views as $view) {
$data[] = $this->prepare_response_for_collection(
new WP_REST_Response($view->asSimplifiedArray())
);
}

return new WP_REST_Response($data);
}

/**
* @param WP_REST_Request $request Full data about the request.
*/
Expand Down

0 comments on commit 636f846

Please sign in to comment.