From 66b642004b6743bb766935733471cf4824f813e9 Mon Sep 17 00:00:00 2001 From: Johannes Wachter Date: Thu, 3 Oct 2019 08:20:54 +0200 Subject: [PATCH] 09 - Add a list representation for locations in the admin interface --- config/lists/locations.xml | 41 +++++++++++++++ config/packages/sulu_admin.yaml | 4 ++ src/Admin/LocationAdmin.php | 51 +++++++++++++++++++ src/Controller/Admin/LocationController.php | 38 ++++++++++++++ src/Entity/Location.php | 2 + .../Admin/LocationControllerTest.php | 49 ++++++++++++++++++ tests/Functional/Traits/LocationTrait.php | 35 +++++++++++++ translations/admin.de.json | 3 +- translations/admin.en.json | 3 +- 9 files changed, 224 insertions(+), 2 deletions(-) create mode 100644 config/lists/locations.xml create mode 100644 src/Admin/LocationAdmin.php create mode 100644 src/Controller/Admin/LocationController.php create mode 100644 tests/Functional/Controller/Admin/LocationControllerTest.php create mode 100644 tests/Functional/Traits/LocationTrait.php diff --git a/config/lists/locations.xml b/config/lists/locations.xml new file mode 100644 index 0000000..b72bc73 --- /dev/null +++ b/config/lists/locations.xml @@ -0,0 +1,41 @@ + + + locations + + + + id + App\Entity\Location + + + + name + App\Entity\Location + + + + street + App\Entity\Location + + + + number + App\Entity\Location + + + + postalCode + App\Entity\Location + + + + city + App\Entity\Location + + + + countryCode + App\Entity\Location + + + diff --git a/config/packages/sulu_admin.yaml b/config/packages/sulu_admin.yaml index e0afa07..6e1a371 100644 --- a/config/packages/sulu_admin.yaml +++ b/config/packages/sulu_admin.yaml @@ -18,6 +18,10 @@ sulu_admin: routes: list: app.get_event_registration_list + locations: + routes: + list: app.get_location_list + # Registering Selection Field Types in this section field_type_options: selection: diff --git a/src/Admin/LocationAdmin.php b/src/Admin/LocationAdmin.php new file mode 100644 index 0000000..f713a57 --- /dev/null +++ b/src/Admin/LocationAdmin.php @@ -0,0 +1,51 @@ +viewBuilderFactory = $viewBuilderFactory; + } + + public function configureNavigationItems(NavigationItemCollection $navigationItemCollection): void + { + $module = $navigationItemCollection->get('app.events'); + + $locations = new NavigationItem('app.locations'); + $locations->setPosition(10); + $locations->setView(static::LOCATION_LIST_VIEW); + + $module->addChild($locations); + } + + public function configureViews(ViewCollection $viewCollection): void + { + $listView = $this->viewBuilderFactory->createListViewBuilder(self::LOCATION_LIST_VIEW, '/locations') + ->setResourceKey(Location::RESOURCE_KEY) + ->setListKey(self::LOCATION_LIST_KEY) + ->setTitle('app.locations') + ->addListAdapters(['table']) + ->addToolbarActions([]); + $viewCollection->add($listView); + } +} diff --git a/src/Controller/Admin/LocationController.php b/src/Controller/Admin/LocationController.php new file mode 100644 index 0000000..b3e251a --- /dev/null +++ b/src/Controller/Admin/LocationController.php @@ -0,0 +1,38 @@ +doctrineListRepresentationFactory = $doctrineListRepresentationFactory; + } + + /** + * @Route("/admin/api/locations", methods={"GET"}, name="app.get_location_list") + */ + public function getListAction(Request $request): Response + { + $listRepresentation = $this->doctrineListRepresentationFactory->createDoctrineListRepresentation( + Location::RESOURCE_KEY + ); + + return $this->json($listRepresentation->toArray()); + } +} diff --git a/src/Entity/Location.php b/src/Entity/Location.php index 892f081..5c2f6d0 100644 --- a/src/Entity/Location.php +++ b/src/Entity/Location.php @@ -11,6 +11,8 @@ */ class Location { + const RESOURCE_KEY = 'locations'; + /** * @var int|null * diff --git a/tests/Functional/Controller/Admin/LocationControllerTest.php b/tests/Functional/Controller/Admin/LocationControllerTest.php new file mode 100644 index 0000000..08b7d6f --- /dev/null +++ b/tests/Functional/Controller/Admin/LocationControllerTest.php @@ -0,0 +1,49 @@ +client = $this->createAuthenticatedClient(); + $this->purgeDatabase(); + } + + public function testCGet(): void + { + $location1 = $this->createLocation('Sulu'); + $location2 = $this->createLocation('Symfony'); + + $this->client->request('GET', '/admin/api/locations'); + + $response = $this->client->getResponse(); + $this->assertInstanceOf(Response::class, $response); + $result = json_decode($response->getContent() ?: '', true); + $this->assertHttpStatusCode(200, $response); + + $this->assertSame(2, $result['total']); + $this->assertCount(2, $result['_embedded']['locations']); + $items = $result['_embedded']['locations']; + + $this->assertSame($location1->getId(), $items[0]['id']); + $this->assertSame($location2->getId(), $items[1]['id']); + + $this->assertSame($location1->getName(), $items[0]['name']); + $this->assertSame($location2->getName(), $items[1]['name']); + } +} diff --git a/tests/Functional/Traits/LocationTrait.php b/tests/Functional/Traits/LocationTrait.php new file mode 100644 index 0000000..d40c142 --- /dev/null +++ b/tests/Functional/Traits/LocationTrait.php @@ -0,0 +1,35 @@ +getLocationRepository()->create(); + $location->setName($name); + $location->setStreet(''); + $location->setNumber(''); + $location->setPostalCode(''); + $location->setCity(''); + $location->setCountryCode(''); + + static::getEntityManager()->persist($location); + static::getEntityManager()->flush(); + + return $location; + } + + protected function getLocationRepository(): LocationRepository + { + return static::getEntityManager()->getRepository(Location::class); + } + + abstract protected static function getEntityManager(): EntityManagerInterface; +} diff --git a/translations/admin.de.json b/translations/admin.de.json index 54fd06f..ac72ae7 100644 --- a/translations/admin.de.json +++ b/translations/admin.de.json @@ -7,5 +7,6 @@ "app.end_date": "Ende", "app.enable_event": "Veranstaltungen aktivieren", "app.enabled": "Aktiviert", - "app.location": "Standort" + "app.location": "Standort", + "app.locations": "Standorte" } diff --git a/translations/admin.en.json b/translations/admin.en.json index 293fa2d..b073ae1 100644 --- a/translations/admin.en.json +++ b/translations/admin.en.json @@ -7,5 +7,6 @@ "app.end_date": "Ende", "app.enable_event": "Enable event", "app.enabled": "Enabled", - "app.location": "Location" + "app.location": "Location", + "app.locations": "Locations" }