diff --git a/src/Controller/GetCoursesByOrganization.php b/src/Controller/GetCoursesByOrganization.php deleted file mode 100644 index 6dbd3b0104a295228e494e0a6a0e79dc0560f1c0..0000000000000000000000000000000000000000 --- a/src/Controller/GetCoursesByOrganization.php +++ /dev/null @@ -1,44 +0,0 @@ -<?php - -declare(strict_types=1); - -namespace Dbp\Relay\BaseCourseBundle\Controller; - -use ApiPlatform\Core\DataProvider\PaginatorInterface; -use Dbp\Relay\BaseCourseBundle\API\CourseProviderInterface; -use Dbp\Relay\CoreBundle\Helpers\ArrayFullPaginator; -use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; -use Symfony\Component\HttpFoundation\Request; - -class GetCoursesByOrganization extends AbstractController -{ - public const ITEMS_PER_PAGE = 250; - - protected $coursesProvider; - - public function __construct(CourseProviderInterface $coursesProvider) - { - $this->coursesProvider = $coursesProvider; - } - - public function __invoke(string $identifier, Request $request): PaginatorInterface - { - $this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY'); - - $page = (int) $request->query->get('page', 1); - $perPage = (int) $request->query->get('perPage', self::ITEMS_PER_PAGE); - - $options = []; - $lang = $request->query->get('lang', 'de'); - $options['lang'] = $lang; - - $term = $request->query->get('term'); - if ($term !== null) { - $options['term'] = $term; - } - - $courses = $this->coursesProvider->getCoursesByOrganization($identifier, $options); - - return new ArrayFullPaginator($courses, $page, $perPage); - } -} diff --git a/src/Controller/GetCoursesByPerson.php b/src/Controller/GetCoursesByPerson.php deleted file mode 100644 index 8837097b1b09fb49ca445512f89e18c3d37257db..0000000000000000000000000000000000000000 --- a/src/Controller/GetCoursesByPerson.php +++ /dev/null @@ -1,44 +0,0 @@ -<?php - -declare(strict_types=1); - -namespace Dbp\Relay\BaseCourseBundle\Controller; - -use ApiPlatform\Core\DataProvider\PaginatorInterface; -use Dbp\Relay\BaseCourseBundle\API\CourseProviderInterface; -use Dbp\Relay\CoreBundle\Helpers\ArrayFullPaginator; -use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; -use Symfony\Component\HttpFoundation\Request; - -class GetCoursesByPerson extends AbstractController -{ - public const ITEMS_PER_PAGE = 250; - - protected $coursesProvider; - - public function __construct(CourseProviderInterface $coursesProvider) - { - $this->coursesProvider = $coursesProvider; - } - - public function __invoke(string $identifier, Request $request): PaginatorInterface - { - $this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY'); - - $page = (int) $request->query->get('page', 1); - $perPage = (int) $request->query->get('perPage', self::ITEMS_PER_PAGE); - - $options = []; - $lang = $request->query->get('lang', 'de'); - $options['lang'] = $lang; - - $term = $request->query->get('term'); - if ($term !== null) { - $options['term'] = $term; - } - - $courses = $this->coursesProvider->getCoursesByPerson($identifier, $options); - - return new ArrayFullPaginator($courses, $page, $perPage); - } -} diff --git a/src/DataProvider/CourseCollectionDataProvider.php b/src/DataProvider/CourseCollectionDataProvider.php index 89103f1bd004dbaf20cbce6977dc1750b05237d5..5b9de4986df0685e22547db69fcef0883e29c4cc 100644 --- a/src/DataProvider/CourseCollectionDataProvider.php +++ b/src/DataProvider/CourseCollectionDataProvider.php @@ -13,11 +13,11 @@ use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; final class CourseCollectionDataProvider extends AbstractController implements CollectionDataProviderInterface, RestrictedDataProviderInterface { - private $api; + private $courseProvider; - public function __construct(CourseProviderInterface $api) + public function __construct(CourseProviderInterface $courseProvider) { - $this->api = $api; + $this->courseProvider = $courseProvider; } public function supports(string $resourceClass, string $operationName = null, array $context = []): bool @@ -29,14 +29,34 @@ final class CourseCollectionDataProvider extends AbstractController implements C { $this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY'); + $options = []; $filters = $context['filters'] ?? []; - $options = ['lang' => $filters['lang'] ?? 'de']; - + $options['lang'] = $filters['lang'] ?? 'de'; $term = $filters['term'] ?? null; if ($term !== null) { $options['term'] = $term; } + $organizationId = $filters['organizationId'] ?? null; + $personId = $filters['personId'] ?? null; + + $courses = null; + if (!empty($organizationId) || !empty($personId)) { + if (!empty($organizationId)) { + $courses = $this->courseProvider->getCoursesByOrganization($organizationId, $options); + } + if (!empty($personId)) { + $coursesByPerson = $this->courseProvider->getCoursesByPerson($personId, $options); + if (!empty($organizationId)) { + $courses = array_uintersect($courses, $coursesByPerson, compareCourses); + } else { + $courses = $coursesByPerson; + } + } + } else { + $courses = $this->courseProvider->getCourses($options); + } + $page = 1; if (isset($filters['page'])) { $page = (int) $filters['page']; @@ -47,6 +67,17 @@ final class CourseCollectionDataProvider extends AbstractController implements C $perPage = (int) $filters['perPage']; } - return new ArrayFullPaginator($this->api->getCourses($options), $page, $perPage); + return new ArrayFullPaginator($courses, $page, $perPage); + } + + private static function compareCourses(Course $a, Course $b): int + { + if ($a->getIdentifier() > $b->getIdentifier()) { + return 1; + } else if ($a->getIdentifier() === $b->getIdentifier()) { + return 0; + } else { + return -1; + } } } diff --git a/src/Entity/Course.php b/src/Entity/Course.php index 9e51ace0cb6114399e7904ef7a4f2415afb4ea18..c40fac7452d6a87170890b2e6b2d45e3ef0a7fbd 100644 --- a/src/Entity/Course.php +++ b/src/Entity/Course.php @@ -19,39 +19,11 @@ use Symfony\Component\Serializer\Annotation\Groups; * "parameters" = { * {"name" = "lang", "in" = "query", "description" = "Language of result", "type" = "string", "enum" = {"de", "en"}, "example" = "de"}, * {"name" = "term", "in" = "query", "description" = "Teaching term", "type" = "string", "enum" = {"W", "S"}, "example" = "W"}, + * {"name" = "organizationId", "in" = "query", "description" = "ID of organization", "required" = false, "type" = "string", "example" = "1190"}, + * {"name" = "personId", "in" = "query", "description" = "ID of lecturer", "required" = false, "type" = "string", "example" = "woody007"}, * } * } - * }, - * "get_by_organization" = { - * "method" = "GET", - * "path" = "/base/organizations/{identifier}/courses", - * "controller" = GetCoursesByOrganization::class, - * "read" = false, - * "openapi_context" = { - * "tags" = {"BaseCourse"}, - * "summary" = "Get the Courses related to an organization.", - * "parameters" = { - * {"name" = "lang", "in" = "query", "description" = "Language of result", "type" = "string", "enum" = {"de", "en"}, "example" = "de"}, - * {"name" = "identifier", "in" = "path", "description" = "Id of Organization", "required" = true, "type" = "string", "example" = "123456"}, - * {"name" = "term", "in" = "query", "description" = "Teaching term", "type" = "string", "enum" = {"W", "S"}, "example" = "W"}, - * } - * }, - * }, - * "get_by_person" = { - * "method" = "GET", - * "path" = "/base/people/{identifier}/courses", - * "controller" = GetCoursesByPerson::class, - * "read" = false, - * "openapi_context" = { - * "tags" = {"BaseCourse"}, - * "summary" = "Get the Courses related to a person.", - * "parameters" = { - * {"name" = "lang", "in" = "query", "description" = "Language of result", "type" = "string", "enum" = {"de", "en"}, "example" = "de"}, - * {"name" = "identifier", "in" = "path", "description" = "Id of person", "required" = true, "type" = "string", "example" = "woody007"}, - * {"name" = "term", "in" = "query", "description" = "Teaching term", "type" = "string", "enum" = {"W", "S"}, "example" = "W"}, - * } - * }, - * }, + * } * }, * itemOperations={ * "get" = { diff --git a/tests/ApiTest.php b/tests/ApiTest.php index 0a702255782e91cf402830f7fbbce4ff96ca0768..08009db28d4411a60ec8c32745ad6cd43a5726af 100644 --- a/tests/ApiTest.php +++ b/tests/ApiTest.php @@ -29,18 +29,4 @@ class ApiTest extends ApiTestCase $response = $client->request('GET', '/base/courses/123/attendees'); $this->assertSame(Response::HTTP_UNAUTHORIZED, $response->getStatusCode()); } - - public function testCoursesByOrganizationNoAuth() - { - $client = self::createClient(); - $response = $client->request('GET', '/base/organizations/123/courses'); - $this->assertSame(Response::HTTP_UNAUTHORIZED, $response->getStatusCode()); - } - - public function testCoursesByPersonNoAuth() - { - $client = self::createClient(); - $response = $client->request('GET', '/base/people/123/courses'); - $this->assertSame(Response::HTTP_UNAUTHORIZED, $response->getStatusCode()); - } }