Browse Source

GraphQL: get course/session by api id #2644

Angel Fernando Quiroz Campos 6 years ago
parent
commit
b496a9f45a

+ 17 - 5
src/GraphQlBundle/Map/QueryMap.php

@@ -398,13 +398,18 @@ class QueryMap extends ResolverMap implements ContainerAwareInterface
     {
         $this->checkAuthorization();
 
-        $id = (int) $args['id'];
+        $itemIdInput = array_map('trim', $args['itemId']);
 
+        if (empty($itemIdInput['name']) || empty($itemIdInput['value'])) {
+            throw new UserError($this->translator->trans('Missing parameters'));
+        }
+
+        $courseId = \CourseManager::get_course_id_from_original_id($itemIdInput['value'], $itemIdInput['name']);
         $courseRepo = $this->em->getRepository('ChamiloCoreBundle:Course');
-        $course = $courseRepo->find($id);
+        $course = $courseRepo->find($courseId);
 
-        if (!$course) {
-            throw new UserError($this->translator->trans('Course not found.'));
+        if (empty($courseId)) {
+            throw new UserError($this->translator->trans("Course not found"));
         }
 
         if (false === $this->securityChecker->isGranted(CourseVoter::VIEW, $course)) {
@@ -423,9 +428,16 @@ class QueryMap extends ResolverMap implements ContainerAwareInterface
     {
         $this->checkAuthorization();
 
+        $itemIdInput = array_map('trim', $args['itemId']);
+
+        if (empty($itemIdInput['name']) || empty($itemIdInput['value'])) {
+            throw new UserError($this->translator->trans('Missing parameters'));
+        }
+
+        $sessionId = \SessionManager::getSessionIdFromOriginalId($itemIdInput['value'], $itemIdInput['name']);
         $sessionRepo = $this->em->getRepository('ChamiloCoreBundle:Session');
         /** @var Session $session */
-        $session = $sessionRepo->find($args['id']);
+        $session = $sessionRepo->find($sessionId);
 
         if (!$session) {
             throw new UserError($this->translator->trans('Session not found.'));

+ 2 - 2
src/GraphQlBundle/Resources/config/schema.types.graphql

@@ -2,9 +2,9 @@ type Query {
     "The current user logged in the API."
     viewer: User!
     "Get the data from a course on the platform."
-    course(id: Int!): Course
+    course(itemId: ItemIdInput!): Course
     "Get the data from a session on the platform."
-    session(id: Int!): Session
+    session(itemId: ItemIdInput!): Session
     "Get the data from a session category."
     sessionCategory(id: Int!): SessionCategory
 }