From df929f698b401f7237f09883a8314da53081f739 Mon Sep 17 00:00:00 2001 From: Johannes Wachter Date: Wed, 25 Sep 2019 07:37:16 +0200 Subject: [PATCH] 07 - Display the registrations for each event in the admin interface --- config/lists/event_registrations.xml | 31 +++++++++++ config/packages/sulu_admin.yaml | 4 ++ config/routes_admin.yaml | 8 +++ src/Admin/EventAdmin.php | 14 +++++ .../Admin/EventRegistrationController.php | 41 ++++++++++++++ src/Entity/EventRegistration.php | 2 + .../Admin/EventRegistrationControllerTest.php | 54 +++++++++++++++++++ .../Traits/EventRegistrationTrait.php | 14 +++++ translations/admin.de.json | 1 + translations/admin.en.json | 1 + 10 files changed, 170 insertions(+) create mode 100644 config/lists/event_registrations.xml create mode 100644 src/Controller/Admin/EventRegistrationController.php create mode 100644 tests/Functional/Controller/Admin/EventRegistrationControllerTest.php diff --git a/config/lists/event_registrations.xml b/config/lists/event_registrations.xml new file mode 100644 index 0000000..19e96b6 --- /dev/null +++ b/config/lists/event_registrations.xml @@ -0,0 +1,31 @@ + + + event_registrations + + + + id + App\Entity\EventRegistration + + + + firstName + App\Entity\EventRegistration + + + + lastName + App\Entity\EventRegistration + + + + email + App\Entity\EventRegistration + + + + event + App\Entity\EventRegistration + + + diff --git a/config/packages/sulu_admin.yaml b/config/packages/sulu_admin.yaml index fba6ab6..30c2925 100644 --- a/config/packages/sulu_admin.yaml +++ b/config/packages/sulu_admin.yaml @@ -14,6 +14,10 @@ sulu_admin: list: app.get_events detail: app.get_event + event_registrations: + routes: + list: app.get_event_registrations + # Registering Selection Field Types in this section field_type_options: selection: diff --git a/config/routes_admin.yaml b/config/routes_admin.yaml index b883181..5923813 100644 --- a/config/routes_admin.yaml +++ b/config/routes_admin.yaml @@ -7,3 +7,11 @@ app_events: name_prefix: app. options: expose: true + +app_event_registrations: + type: rest + resource: App\Controller\Admin\EventRegistrationController + prefix: /admin/api + name_prefix: app. + options: + expose: true diff --git a/src/Admin/EventAdmin.php b/src/Admin/EventAdmin.php index 55b6605..8592371 100644 --- a/src/Admin/EventAdmin.php +++ b/src/Admin/EventAdmin.php @@ -5,6 +5,7 @@ namespace App\Admin; use App\Entity\Event; +use App\Entity\EventRegistration; use Sulu\Bundle\AdminBundle\Admin\Admin; use Sulu\Bundle\AdminBundle\Admin\Navigation\NavigationItem; use Sulu\Bundle\AdminBundle\Admin\Navigation\NavigationItemCollection; @@ -22,6 +23,8 @@ class EventAdmin extends Admin const EVENT_LIST_VIEW = 'app.events_list'; + const EVENT_REGISTRATION_LIST_KEY = 'event_registrations'; + const EVENT_ADD_FORM_VIEW = 'app.event_add_form'; const EVENT_EDIT_FORM_VIEW = 'app.event_edit_form'; @@ -119,5 +122,16 @@ public function configureViews(ViewCollection $viewCollection): void ->addToolbarActions($formToolbarActions) ->setParent(static::EVENT_EDIT_FORM_VIEW); $viewCollection->add($editDetailsFormView); + + $editDetailsFormView = $this->viewBuilderFactory->createListViewBuilder(static::EVENT_EDIT_FORM_VIEW . '.registrations', '/registrations') + ->setResourceKey(EventRegistration::RESOURCE_KEY) + ->setListKey(self::EVENT_REGISTRATION_LIST_KEY) + ->setTabTitle('app.registrations') + ->addRouterAttributesToListRequest(['id' => 'eventId']) + ->addListAdapters(['table']) + ->addToolbarActions([]) + ->setUserSettingsKey(EventRegistration::RESOURCE_KEY) + ->setParent(static::EVENT_EDIT_FORM_VIEW); + $viewCollection->add($editDetailsFormView); } } diff --git a/src/Controller/Admin/EventRegistrationController.php b/src/Controller/Admin/EventRegistrationController.php new file mode 100644 index 0000000..2b4dfca --- /dev/null +++ b/src/Controller/Admin/EventRegistrationController.php @@ -0,0 +1,41 @@ +doctrineListRepresentationFactory = $doctrineListRepresentationFactory; + + parent::__construct($viewHandler, $tokenStorage); + } + + public function cgetAction(int $eventId): Response + { + $listRepresentation = $this->doctrineListRepresentationFactory->createDoctrineListRepresentation( + EventRegistration::RESOURCE_KEY, + ['eventId' => $eventId] + ); + + return $this->handleView($this->view($listRepresentation)); + } +} diff --git a/src/Entity/EventRegistration.php b/src/Entity/EventRegistration.php index db9338a..4c457e1 100644 --- a/src/Entity/EventRegistration.php +++ b/src/Entity/EventRegistration.php @@ -12,6 +12,8 @@ */ class EventRegistration { + const RESOURCE_KEY = 'event_registrations'; + /** * @var int|null * diff --git a/tests/Functional/Controller/Admin/EventRegistrationControllerTest.php b/tests/Functional/Controller/Admin/EventRegistrationControllerTest.php new file mode 100644 index 0000000..a2eb9ab --- /dev/null +++ b/tests/Functional/Controller/Admin/EventRegistrationControllerTest.php @@ -0,0 +1,54 @@ +client = $this->createAuthenticatedClient(); + $this->purgeDatabase(); + } + + public function testCGet(): void + { + $event1 = $this->createEvent('Sulu is awesome', 'de'); + $event2 = $this->createEvent('Symfony live is awesome', 'de'); + + $registration1 = $this->createEventRegistration($event1, 'Max', 'Mustermann'); + $registration2 = $this->createEventRegistration($event2, 'Mira', 'Musterfrau'); + + $this->client->request('GET', '/admin/api/events/' . $event1->getId() . '/registrations'); + + $response = $this->client->getResponse(); + $this->assertInstanceOf(Response::class, $response); + $result = json_decode($response->getContent() ?: '', true); + $this->assertHttpStatusCode(200, $response); + + $this->assertSame(1, $result['total']); + $this->assertCount(1, $result['_embedded']['event_registrations']); + $items = $result['_embedded']['event_registrations']; + + $this->assertSame($registration1->getId(), $items[0]['id']); + + $this->assertSame($registration1->getFirstName(), $items[0]['firstName']); + $this->assertSame($registration1->getLastName(), $items[0]['lastName']); + $this->assertArrayHasKey('email', $items[0]); + } +} diff --git a/tests/Functional/Traits/EventRegistrationTrait.php b/tests/Functional/Traits/EventRegistrationTrait.php index 9c0dd32..1223c16 100644 --- a/tests/Functional/Traits/EventRegistrationTrait.php +++ b/tests/Functional/Traits/EventRegistrationTrait.php @@ -11,6 +11,20 @@ trait EventRegistrationTrait { + public function createEventRegistration(Event $event, string $firstName, string $lastName): EventRegistration + { + $event = $this->getEventRegistrationRepository()->create($event); + $event->setFirstName($firstName); + $event->setLastName($lastName); + $event->setEmail($firstName . '@' . $lastName . '.at'); + $event->setMessage(''); + + $this->getEntityManager()->persist($event); + $this->getEntityManager()->flush(); + + return $event; + } + /** * @return EventRegistration[] */ diff --git a/translations/admin.de.json b/translations/admin.de.json index 00e3e92..f8524e6 100644 --- a/translations/admin.de.json +++ b/translations/admin.de.json @@ -1,5 +1,6 @@ { "app.events": "Veranstaltungen", + "app.registrations": "Registrierungen", "app.teaser": "Kurzbeschreibung", "app.start_date": "Start", "app.end_date": "Ende", diff --git a/translations/admin.en.json b/translations/admin.en.json index 4f9f398..08b1ad7 100644 --- a/translations/admin.en.json +++ b/translations/admin.en.json @@ -1,5 +1,6 @@ { "app.events": "Events", + "app.registrations": "Registrations", "app.teaser": "Teaser", "app.start_date": "Start", "app.end_date": "Ende",