Skip to content
Snippets Groups Projects
Commit 8c24b002 authored by Tobias Gross-Vogt's avatar Tobias Gross-Vogt
Browse files

#28178 filter paramters instead of subressource

parent 0fe5fe7f
No related branches found
No related tags found
No related merge requests found
Pipeline #94331 failed
<?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);
}
}
<?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);
}
}
......@@ -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;
}
}
}
......@@ -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" = {
......
......@@ -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());
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment