Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FAU-422] feat: add fau/v1/degree-program/index endpoint #18

Merged
merged 2 commits into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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