From 76bd6998fc547c6dfafb144fff3d7dfe2058ef95 Mon Sep 17 00:00:00 2001
From: Jan-Hendrik Willms <tleilax+github@gmail.com>
Date: Thu, 19 May 2022 11:22:32 +0200
Subject: [PATCH] initial route setup

---
 lib/classes/JsonApi/RouteMap.php              |  7 ++
 .../JsonApi/Routes/MVV/AbschlussShow.php      | 26 ++++++
 .../JsonApi/Routes/MVV/StudiengangShow.php    | 29 ++++++
 lib/classes/JsonApi/SchemaMap.php             |  3 +
 lib/classes/JsonApi/Schemas/MVV/Abschluss.php | 72 +++++++++++++++
 lib/classes/JsonApi/Schemas/MVV/Fach.php      | 72 +++++++++++++++
 .../JsonApi/Schemas/MVV/Studiengang.php       | 88 +++++++++++++++++++
 7 files changed, 297 insertions(+)
 create mode 100644 lib/classes/JsonApi/Routes/MVV/AbschlussShow.php
 create mode 100644 lib/classes/JsonApi/Routes/MVV/StudiengangShow.php
 create mode 100644 lib/classes/JsonApi/Schemas/MVV/Abschluss.php
 create mode 100644 lib/classes/JsonApi/Schemas/MVV/Fach.php
 create mode 100644 lib/classes/JsonApi/Schemas/MVV/Studiengang.php

diff --git a/lib/classes/JsonApi/RouteMap.php b/lib/classes/JsonApi/RouteMap.php
index baccc262569..cc94d16f298 100644
--- a/lib/classes/JsonApi/RouteMap.php
+++ b/lib/classes/JsonApi/RouteMap.php
@@ -125,6 +125,7 @@ class RouteMap
         $this->addAuthenticatedForumRoutes($group);
         $this->addAuthenticatedInstitutesRoutes($group);
         $this->addAuthenticatedMessagesRoutes($group);
+        $this->addAuthenticatedMVVRoutes($group);
         $this->addAuthenticatedNewsRoutes($group);
         $this->addAuthenticatedStudyAreasRoutes($group);
         $this->addAuthenticatedWikiRoutes($group);
@@ -513,6 +514,12 @@ class RouteMap
         $group->delete('/messages/{id}', Routes\Messages\MessageDelete::class);
     }
 
+    private function addAuthenticatedMVVRoutes(RouteCollectorProxy $group): void
+    {
+        $group->get('/degree/{id}', Routes\MVV\AbschlussShow::class);
+        $group->get('/studycourse/{id}', Routes\MVV\StudiengangShow::class);
+    }
+
     private function addAuthenticatedForumRoutes(RouteCollectorProxy $group): void
     {
         $group->get('/courses/{id}/forum-categories', Routes\Forum\ForumCategoriesIndex::class);
diff --git a/lib/classes/JsonApi/Routes/MVV/AbschlussShow.php b/lib/classes/JsonApi/Routes/MVV/AbschlussShow.php
new file mode 100644
index 00000000000..efe76e94b50
--- /dev/null
+++ b/lib/classes/JsonApi/Routes/MVV/AbschlussShow.php
@@ -0,0 +1,26 @@
+<?php
+
+namespace JsonApi\Routes\MVV;
+
+use JsonApi\Errors\RecordNotFoundException;
+use JsonApi\JsonApiController;
+use JsonApi\Schemas\MVV\Studiengang;
+use Psr\Http\Message\ResponseInterface as Response;
+use Psr\Http\Message\ServerRequestInterface as Request;
+
+class AbschlussShow extends JsonApiController
+{
+    protected $allowedIncludePaths = [
+        Studiengang::TYPE,
+    ];
+
+    public function __invoke(Request $request, Response $response, $args): Response
+    {
+        $degree = \Abschluss::find($args['id']);
+        if (!$degree) {
+            throw new RecordNotFoundException("No degree with id {$args['id']}");
+        }
+
+        return $this->getContentResponse($degree);
+    }
+}
diff --git a/lib/classes/JsonApi/Routes/MVV/StudiengangShow.php b/lib/classes/JsonApi/Routes/MVV/StudiengangShow.php
new file mode 100644
index 00000000000..07caa589f57
--- /dev/null
+++ b/lib/classes/JsonApi/Routes/MVV/StudiengangShow.php
@@ -0,0 +1,29 @@
+<?php
+
+namespace JsonApi\Routes\MVV;
+
+use JsonApi\Errors\AuthorizationFailedException;
+use JsonApi\Errors\BadRequestException;
+use JsonApi\Errors\RecordNotFoundException;
+use JsonApi\JsonApiController;
+use JsonApi\Schemas\MVV\Abschluss;
+use JsonApi\Schemas\User as UserSchema;
+use Psr\Http\Message\ResponseInterface as Response;
+use Psr\Http\Message\ServerRequestInterface as Request;
+
+class StudiengangShow extends JsonApiController
+{
+    protected $allowedIncludePaths = [
+        Abschluss::TYPE,
+    ];
+
+    public function __invoke(Request $request, Response $response, $args): Response
+    {
+        $studycourse = \Studiengang::find($args['id']);
+        if (!$studycourse) {
+            throw new RecordNotFoundException("No study course with id {$args['id']}");
+        }
+
+        return $this->getContentResponse($studycourse);
+    }
+}
diff --git a/lib/classes/JsonApi/SchemaMap.php b/lib/classes/JsonApi/SchemaMap.php
index e7168cd9989..34eb3c9078d 100644
--- a/lib/classes/JsonApi/SchemaMap.php
+++ b/lib/classes/JsonApi/SchemaMap.php
@@ -51,6 +51,9 @@ class SchemaMap
             \FileRef::class => Schemas\FileRef::class,
             \FolderType::class => Schemas\Folder::class,
 
+            \Abschluss::class => Schemas\MVV\Abschluss::class,
+            \Studiengang::class => Schemas\MVV\Studiengang::class,
+
             \Courseware\Block::class => Schemas\Courseware\Block::class,
             \Courseware\BlockComment::class => Schemas\Courseware\BlockComment::class,
             \Courseware\BlockFeedback::class => Schemas\Courseware\BlockFeedback::class,
diff --git a/lib/classes/JsonApi/Schemas/MVV/Abschluss.php b/lib/classes/JsonApi/Schemas/MVV/Abschluss.php
new file mode 100644
index 00000000000..e857116ce04
--- /dev/null
+++ b/lib/classes/JsonApi/Schemas/MVV/Abschluss.php
@@ -0,0 +1,72 @@
+<?php
+namespace JsonApi\Schemas\MVV;
+
+use JsonApi\Schemas\SchemaProvider;
+use Neomerx\JsonApi\Contracts\Schema\ContextInterface;
+use Neomerx\JsonApi\Schema\Link;
+
+class Abschluss extends SchemaProvider
+{
+    const TYPE = 'degree';
+
+    const REL_STUDYCOURSES = 'studycourses';
+
+    /**
+     * @param \Abschluss $resource
+     */
+    public function getId($resource): ?string
+    {
+        return $resource->id;
+    }
+
+    /**
+     * @param \Abschluss $resource
+     */
+    public function getAttributes($resource, ContextInterface $context): iterable
+    {
+        return [
+            'name' => (string) $resource->name,
+            'name-short' => (string) $resource->name_kurz,
+            'description' => (string) $resource->beschreibung,
+            'description-formatted' => formatReady($resource->beschreibung),
+            'mkdate' => date('c', $resource->mkdate),
+            'chdate' => date('c', $resource->chdate),
+        ];
+    }
+
+    /**
+     * @param \Abschluss $resource
+     */
+    public function getRelationships($resource, ContextInterface $context): iterable
+    {
+        $relationships = [];
+
+        if ($context->getPosition()->getLevel() === 0) {
+            $relationships = $this->getStudycourseRelationship($relationships, $resource, $this->shouldInclude($context, self::REL_STUDYCOURSES));
+//            $relationships = $this->getBookingsRelationship($relationships, $resource, $this->shouldInclude($context, self::REL_BOOKINGS));
+        }
+
+        return $relationships;
+    }
+
+    private function getStudycourseRelationship(array $relationships, \Abschluss $resource, bool $shouldInclude)
+    {
+        if ($shouldInclude) {
+            $relatedStudycourses = $resource->studiengaenge;
+        } else {
+            $relatedStudycourses = $resource->studiengaenge->map(function (\Studiengang $studycourse) {
+                return \Studiengang::build(['id' => $studycourse->id], false);
+            });
+        }
+
+        $relationships[self::REL_STUDYCOURSES] = [
+            self::RELATIONSHIP_LINKS => [
+                Link::RELATED => $this->getRelationshipRelatedLink($resource, self::REL_STUDYCOURSES),
+            ],
+            self::RELATIONSHIP_DATA => $relatedStudycourses,
+        ];
+
+        return $relationships;
+        return $relationships;
+    }
+}
diff --git a/lib/classes/JsonApi/Schemas/MVV/Fach.php b/lib/classes/JsonApi/Schemas/MVV/Fach.php
new file mode 100644
index 00000000000..01ba0fa9f18
--- /dev/null
+++ b/lib/classes/JsonApi/Schemas/MVV/Fach.php
@@ -0,0 +1,72 @@
+<?php
+namespace JsonApi\Schemas\MVV;
+
+use JsonApi\Schemas\SchemaProvider;
+use Neomerx\JsonApi\Contracts\Schema\ContextInterface;
+use Neomerx\JsonApi\Schema\Link;
+
+class Fach extends SchemaProvider
+{
+    const TYPE = 'subject';
+
+    const REL_STUDYCOURSES = 'studycourses';
+
+    /**
+     * @param \Fach $resource
+     */
+    public function getId($resource): ?string
+    {
+        return $resource->id;
+    }
+
+    /**
+     * @param \Fach $resource
+     */
+    public function getAttributes($resource, ContextInterface $context): iterable
+    {
+        return [
+            'name' => (string) $resource->name,
+            'name-short' => (string) $resource->name_kurz,
+            'description' => (string) $resource->beschreibung,
+            'description-formatted' => formatReady($resource->beschreibung),
+            'mkdate' => date('c', $resource->mkdate),
+            'chdate' => date('c', $resource->chdate),
+        ];
+    }
+
+    /**
+     * @param \Fach $resource
+     */
+    public function getRelationships($resource, ContextInterface $context): iterable
+    {
+        $relationships = [];
+
+        if ($context->getPosition()->getLevel() === 0) {
+            $relationships = $this->getStudycourseRelationship($relationships, $resource, $this->shouldInclude($context, self::REL_STUDYCOURSES));
+//            $relationships = $this->getBookingsRelationship($relationships, $resource, $this->shouldInclude($context, self::REL_BOOKINGS));
+        }
+
+        return $relationships;
+    }
+
+    private function getStudycourseRelationship(array $relationships, \Fach $resource, bool $shouldInclude)
+    {
+        if ($shouldInclude) {
+            $relatedStudycourses = $resource->studiengaenge;
+        } else {
+            $relatedStudycourses = $resource->studiengaenge->map(function (\Studiengang $studycourse) {
+                return \Studiengang::build(['id' => $studycourse->id], false);
+            });
+        }
+
+        $relationships[self::REL_STUDYCOURSES] = [
+            self::RELATIONSHIP_LINKS => [
+                Link::RELATED => $this->getRelationshipRelatedLink($resource, self::REL_STUDYCOURSES),
+            ],
+            self::RELATIONSHIP_DATA => $relatedStudycourses,
+        ];
+
+        return $relationships;
+        return $relationships;
+    }
+}
diff --git a/lib/classes/JsonApi/Schemas/MVV/Studiengang.php b/lib/classes/JsonApi/Schemas/MVV/Studiengang.php
new file mode 100644
index 00000000000..d21ee5f5513
--- /dev/null
+++ b/lib/classes/JsonApi/Schemas/MVV/Studiengang.php
@@ -0,0 +1,88 @@
+<?php
+namespace JsonApi\Schemas\MVV;
+
+use JsonApi\Schemas\SchemaProvider;
+use Neomerx\JsonApi\Contracts\Schema\ContextInterface;
+use Neomerx\JsonApi\Schema\Link;
+
+class Studiengang extends SchemaProvider
+{
+    const TYPE = 'studycourses';
+
+    const REL_DEGREE = 'degree';
+
+    /**
+     * @param \Studiengang $resource
+     */
+    public function getId($resource): ?string
+    {
+        return $resource->id;
+    }
+
+    /**
+     * @param \Studiengang $resource
+     */
+    public function getAttributes($resource, ContextInterface $context): iterable
+    {
+        return [
+            'name' => (string) $resource->getDisplayName(),
+            'name-short' => (string) $resource->name_kurz,
+            'description' => (string) $resource->beschreibung,
+            'description-formatted' => formatReady($resource->beschreibung),
+            'mkdate' => date('c', $resource->mkdate),
+            'chdate' => date('c', $resource->chdate),
+        ];
+    }
+
+//   `studiengang_id` char(32) CHARACTER SET latin1 COLLATE latin1_bin NOT NULL,
+//  `abschluss_id` char(32) CHARACTER SET latin1 COLLATE latin1_bin DEFAULT NULL,
+//  `typ` enum('einfach','mehrfach') CHARACTER SET latin1 COLLATE latin1_bin DEFAULT NULL,
+//  `institut_id` char(32) CHARACTER SET latin1 COLLATE latin1_bin DEFAULT NULL,
+//  `start` char(32) CHARACTER SET latin1 COLLATE latin1_bin DEFAULT NULL,
+//  `end` char(32) CHARACTER SET latin1 COLLATE latin1_bin DEFAULT NULL,
+//  `beschlussdatum` int(11) unsigned DEFAULT NULL,
+//  `fassung_nr` int(2) DEFAULT NULL,
+//  `fassung_typ` varchar(32) CHARACTER SET latin1 COLLATE latin1_bin DEFAULT NULL,
+//  `stat` varchar(32) CHARACTER SET latin1 COLLATE latin1_bin DEFAULT NULL,
+//  `kommentar_status` text COLLATE utf8mb4_unicode_ci DEFAULT NULL,
+//  `schlagworte` text COLLATE utf8mb4_unicode_ci DEFAULT NULL,
+//  `studienzeit` tinyint(3) unsigned DEFAULT NULL,
+//  `studienplaetze` int(10) unsigned DEFAULT NULL,
+//  `abschlussgrad` varchar(32) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
+//  `enroll` varchar(50) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
+//  `author_id` char(32) CHARACTER SET latin1 COLLATE latin1_bin NOT NULL,
+//  `editor_id` char(32) CHARACTER SET latin1 COLLATE latin1_bin NOT NULL,
+
+    /**
+     * @param \Studiengang $resource
+     */
+    public function getRelationships($resource, ContextInterface $context): iterable
+    {
+        $relationships = [];
+
+        if ($context->getPosition()->getLevel() === 0) {
+            $relationships = $this->getDegreeRelationship($relationships, $resource, $this->shouldInclude($context, self::REL_DEGREE));
+        }
+
+        return $relationships;
+    }
+
+    private function getDegreeRelationship(array $relationships, \Studiengang $studycourse, bool $shouldInclude): array
+    {
+        $degree = $studycourse->abschluss;
+
+        if ($degree) {
+            $data = $shouldInclude ? $degree : \Abschluss::build(['id' => $degree->id], false);
+
+            $relationships[self::REL_DEGREE] = [
+                self::RELATIONSHIP_LINKS => [
+                    Link::RELATED => $this->createLinkToResource($data),
+                ],
+                self::RELATIONSHIP_DATA => $data,
+            ];
+        }
+
+        return $relationships;
+    }
+
+}
-- 
GitLab