From 7c489786a158c391188a2db3f20bd763cae2478c Mon Sep 17 00:00:00 2001
From: Volodymyr Shelmuk <v.shelmuk@inpsyde.com>
Date: Wed, 4 Sep 2024 13:48:21 +0300
Subject: [PATCH 1/2] feat: add `fau/v1/degree-program/index` endpoint

---
 .../DegreeProgramViewTranslated.php           | 12 ++++++
 .../TranslatedDegreeProgramController.php     | 39 +++++++++++++++++++
 2 files changed, 51 insertions(+)

diff --git a/src/Application/DegreeProgramViewTranslated.php b/src/Application/DegreeProgramViewTranslated.php
index f119eb3..5137e17 100644
--- a/src/Application/DegreeProgramViewTranslated.php
+++ b/src/Application/DegreeProgramViewTranslated.php
@@ -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();
diff --git a/src/Infrastructure/RestApi/TranslatedDegreeProgramController.php b/src/Infrastructure/RestApi/TranslatedDegreeProgramController.php
index 848a2a9..bdd6d5c 100644
--- a/src/Infrastructure/RestApi/TranslatedDegreeProgramController.php
+++ b/src/Infrastructure/RestApi/TranslatedDegreeProgramController.php
@@ -70,6 +70,13 @@ 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'],
+            ],
+        ]);
         register_rest_route($this->namespace, '/' . $this->rest_base . '/(?P<id>[\d]+)', [
             [
                 'methods' => WP_REST_Server::READABLE,
@@ -176,6 +183,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,
+            MultilingualString::DE
+        );
+
+        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.
      */

From fb48bb0cb8990d22f769efdf929dfc2722430c1b Mon Sep 17 00:00:00 2001
From: Volodymyr Shelmuk <v.shelmuk@inpsyde.com>
Date: Wed, 4 Sep 2024 14:35:41 +0300
Subject: [PATCH 2/2] feat: support `lang` param for
 `fau/v1/degree-program/index` endpoint

---
 .../RestApi/TranslatedDegreeProgramController.php            | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/src/Infrastructure/RestApi/TranslatedDegreeProgramController.php b/src/Infrastructure/RestApi/TranslatedDegreeProgramController.php
index bdd6d5c..333f593 100644
--- a/src/Infrastructure/RestApi/TranslatedDegreeProgramController.php
+++ b/src/Infrastructure/RestApi/TranslatedDegreeProgramController.php
@@ -75,6 +75,9 @@ public function register_routes(): void
                 '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]+)', [
@@ -190,7 +193,7 @@ public function getIndex(WP_REST_Request $request): WP_Error|WP_REST_Response
 
         $views = $this->degreeProgramCollectionRepository->findTranslatedCollection(
             $criteria,
-            MultilingualString::DE
+            $this->requestedLanguage($request)
         );
 
         if (!$views instanceof PaginationAwareCollection) {