From bf35c0c9277d1d637f5561a45aa798f5ceb89023 Mon Sep 17 00:00:00 2001 From: Sebastian Marcet Date: Mon, 26 Sep 2016 17:18:56 -0300 Subject: [PATCH] New Locations endpoints added endpoints to get locations by type: * Venue * External Location * Hotel * Airport Change-Id: I08816d1f3a1dd0020290775f8be663df6ee387c8 --- .../OAuth2SummitLocationsApiController.php | 100 ++++++++++++++ app/Http/routes.php | 5 +- .../Locations/SummitVenueSerializer.php | 6 +- app/Models/Foundation/Summit/Summit.php | 27 ++++ database/seeds/ApiEndpointsSeeder.php | 49 +++++++ tests/OAuth2SummitApiTest.php | 127 ++++++++++++++++++ 6 files changed, 311 insertions(+), 3 deletions(-) diff --git a/app/Http/Controllers/apis/protected/summit/OAuth2SummitLocationsApiController.php b/app/Http/Controllers/apis/protected/summit/OAuth2SummitLocationsApiController.php index 5053d0a1..2bfae140 100644 --- a/app/Http/Controllers/apis/protected/summit/OAuth2SummitLocationsApiController.php +++ b/app/Http/Controllers/apis/protected/summit/OAuth2SummitLocationsApiController.php @@ -262,4 +262,104 @@ final class OAuth2SummitLocationsApiController extends OAuth2ProtectedController } } + + /** + * @param $summit_id + * @return mixed + */ + public function getVenues($summit_id) + { + try { + $summit = SummitFinderStrategyFactory::build($this->repository)->find($summit_id); + if (is_null($summit)) return $this->error404(); + + //locations + $locations = array(); + + foreach ($summit->getVenues() as $location) + { + $locations[] = SerializerRegistry::getInstance()->getSerializer($location)->serialize(); + } + + return $this->ok($locations); + + } catch (Exception $ex) { + Log::error($ex); + return $this->error500($ex); + } + } + + /** + * @param $summit_id + * @return mixed + */ + public function getExternalLocations($summit_id) + { + try { + $summit = SummitFinderStrategyFactory::build($this->repository)->find($summit_id); + if (is_null($summit)) return $this->error404(); + + //locations + $locations = array(); + foreach ($summit->getExternalLocations() as $location) + { + $locations[] = SerializerRegistry::getInstance()->getSerializer($location)->serialize(); + } + + return $this->ok($locations); + } catch (Exception $ex) { + Log::error($ex); + return $this->error500($ex); + } + } + + /** + * @param $summit_id + * @return mixed + */ + public function getHotels($summit_id) + { + try { + $summit = SummitFinderStrategyFactory::build($this->repository)->find($summit_id); + if (is_null($summit)) return $this->error404(); + + //locations + $locations = array(); + foreach ($summit->getHotels() as $location) + { + $locations[] = SerializerRegistry::getInstance()->getSerializer($location)->serialize(); + } + + return $this->ok($locations); + } catch (Exception $ex) { + Log::error($ex); + return $this->error500($ex); + } + } + + + /** + * @param $summit_id + * @return mixed + */ + public function getAirports($summit_id) + { + try { + $summit = SummitFinderStrategyFactory::build($this->repository)->find($summit_id); + if (is_null($summit)) return $this->error404(); + + //locations + $locations = array(); + foreach ($summit->getAirports() as $location) + { + $locations[] = SerializerRegistry::getInstance()->getSerializer($location)->serialize(); + } + + return $this->ok($locations); + } catch (Exception $ex) { + Log::error($ex); + return $this->error500($ex); + } + } + } \ No newline at end of file diff --git a/app/Http/routes.php b/app/Http/routes.php index ae650bf6..fe974105 100644 --- a/app/Http/routes.php +++ b/app/Http/routes.php @@ -129,7 +129,10 @@ Route::group(array( Route::group(array('prefix' => 'locations'), function () { Route::get('', 'OAuth2SummitLocationsApiController@getLocations'); - + Route::get('/venues', 'OAuth2SummitLocationsApiController@getVenues'); + Route::get('/external-locations', 'OAuth2SummitLocationsApiController@getExternalLocations'); + Route::get('/hotels', 'OAuth2SummitLocationsApiController@getHotels'); + Route::get('/airports', 'OAuth2SummitLocationsApiController@getAirports'); Route::group(array('prefix' => '{location_id}'), function () { Route::get('', 'OAuth2SummitLocationsApiController@getLocation'); Route::get('/events/published','OAuth2SummitLocationsApiController@getLocationEvents'); diff --git a/app/ModelSerializers/Locations/SummitVenueSerializer.php b/app/ModelSerializers/Locations/SummitVenueSerializer.php index 635dd5ce..67d6f0d4 100644 --- a/app/ModelSerializers/Locations/SummitVenueSerializer.php +++ b/app/ModelSerializers/Locations/SummitVenueSerializer.php @@ -37,20 +37,22 @@ final class SummitVenueSerializer extends SummitGeoLocatedLocationSerializer $values = parent::serialize($expand, $fields, $relations, $params); $venue = $this->object; // rooms - $rooms = array(); + $rooms = []; foreach($venue->getRooms() as $room) { $rooms[] = SerializerRegistry::getInstance()->getSerializer($room)->serialize($expand, $fields, $relations, $params); } + if(count($rooms) > 0) $values['rooms'] = $rooms; // floors - $floors = array(); + $floors = []; foreach($venue->getFloors() as $floor) { $floors[] = SerializerRegistry::getInstance()->getSerializer($floor)->serialize($expand, $fields, $relations, $params); } + if(count($floors) > 0) $values['floors'] = $floors; diff --git a/app/Models/Foundation/Summit/Summit.php b/app/Models/Foundation/Summit/Summit.php index c1c2b892..853f1c95 100644 --- a/app/Models/Foundation/Summit/Summit.php +++ b/app/Models/Foundation/Summit/Summit.php @@ -330,6 +330,33 @@ class Summit extends SilverstripeBaseModel }); } + /** + * @return SummitHotel[] + */ + public function getHotels(){ + return $this->locations->filter(function($e){ + return $e instanceof SummitHotel; + }); + } + + /** + * @return SummitAirport[] + */ + public function getAirports(){ + return $this->locations->filter(function($e){ + return $e instanceof SummitAirport; + }); + } + + /** + * @return SummitExternalLocation[] + */ + public function getExternalLocations(){ + return $this->locations->filter(function($e){ + return $e->getClassName() == 'SummitExternalLocation'; + }); + } + /** * @return ArrayCollection */ diff --git a/database/seeds/ApiEndpointsSeeder.php b/database/seeds/ApiEndpointsSeeder.php index 034c20d1..439e8f93 100644 --- a/database/seeds/ApiEndpointsSeeder.php +++ b/database/seeds/ApiEndpointsSeeder.php @@ -472,6 +472,46 @@ class ApiEndpointsSeeder extends Seeder ) ); + ApiEndpoint::create( + array( + 'name' => 'get-venues', + 'active' => true, + 'api_id' => $summit->id, + 'route' => '/api/v1/summits/{id}/locations/venues', + 'http_method' => 'GET' + ) + ); + + ApiEndpoint::create( + array( + 'name' => 'get-external-locations', + 'active' => true, + 'api_id' => $summit->id, + 'route' => '/api/v1/summits/{id}/locations/external-locations', + 'http_method' => 'GET' + ) + ); + + ApiEndpoint::create( + array( + 'name' => 'get-hotels', + 'active' => true, + 'api_id' => $summit->id, + 'route' => '/api/v1/summits/{id}/locations/hotels', + 'http_method' => 'GET' + ) + ); + + ApiEndpoint::create( + array( + 'name' => 'get-airports', + 'active' => true, + 'api_id' => $summit->id, + 'route' => '/api/v1/summits/{id}/locations/airports', + 'http_method' => 'GET' + ) + ); + ApiEndpoint::create( array( 'name' => 'get-location', @@ -653,6 +693,14 @@ class ApiEndpointsSeeder extends Seeder $endpoint->scopes()->attach($summit_read_scope->id); $endpoint = ApiEndpoint::where('name', '=', 'get-locations')->first(); $endpoint->scopes()->attach($summit_read_scope->id); + $endpoint = ApiEndpoint::where('name', '=', 'get-venues')->first(); + $endpoint->scopes()->attach($summit_read_scope->id); + $endpoint = ApiEndpoint::where('name', '=', 'get-hotels')->first(); + $endpoint->scopes()->attach($summit_read_scope->id); + $endpoint = ApiEndpoint::where('name', '=', 'get-airports')->first(); + $endpoint->scopes()->attach($summit_read_scope->id); + $endpoint = ApiEndpoint::where('name', '=', 'get-external-locations')->first(); + $endpoint->scopes()->attach($summit_read_scope->id); $endpoint = ApiEndpoint::where('name', '=', 'get-location')->first(); $endpoint->scopes()->attach($summit_read_scope->id); $endpoint = ApiEndpoint::where('name', '=', 'get-event-types')->first(); @@ -663,6 +711,7 @@ class ApiEndpointsSeeder extends Seeder $endpoint->scopes()->attach($summit_read_scope->id); $endpoint = ApiEndpoint::where('name', '=', 'get-location-events')->first(); $endpoint->scopes()->attach($summit_read_scope->id); + // read external orders $endpoint = ApiEndpoint::where('name', '=', 'get-external-order')->first(); diff --git a/tests/OAuth2SummitApiTest.php b/tests/OAuth2SummitApiTest.php index 80ccafdf..f0ffb10e 100644 --- a/tests/OAuth2SummitApiTest.php +++ b/tests/OAuth2SummitApiTest.php @@ -1451,6 +1451,133 @@ final class OAuth2SummitApiTest extends ProtectedApiTest $this->assertTrue(!is_null($locations)); } + + public function testGetCurrentSummitVenues() + { + $params = array + ( + 'id' => 'current', + ); + + $headers = array + ( + "HTTP_Authorization" => " Bearer " .$this->access_token, + "CONTENT_TYPE" => "application/json" + ); + + $response = $this->action + ( + "GET", + "OAuth2SummitLocationsApiController@getVenues", + $params, + array(), + array(), + array(), + $headers + ); + + $content = $response->getContent(); + $this->assertResponseStatus(200); + + $locations = json_decode($content); + $this->assertTrue(!is_null($locations)); + } + + + public function testGetCurrentSummitHotels() + { + $params = array + ( + 'id' => 'current', + ); + + $headers = array + ( + "HTTP_Authorization" => " Bearer " .$this->access_token, + "CONTENT_TYPE" => "application/json" + ); + + $response = $this->action + ( + "GET", + "OAuth2SummitLocationsApiController@getHotels", + $params, + array(), + array(), + array(), + $headers + ); + + $content = $response->getContent(); + $this->assertResponseStatus(200); + + $locations = json_decode($content); + $this->assertTrue(!is_null($locations)); + } + + public function testGetCurrentSummitAirports() + { + $params = array + ( + 'id' => 'current', + ); + + $headers = array + ( + "HTTP_Authorization" => " Bearer " .$this->access_token, + "CONTENT_TYPE" => "application/json" + ); + + $response = $this->action + ( + "GET", + "OAuth2SummitLocationsApiController@getAirports", + $params, + array(), + array(), + array(), + $headers + ); + + $content = $response->getContent(); + $this->assertResponseStatus(200); + + $locations = json_decode($content); + $this->assertTrue(!is_null($locations)); + } + + + public function testGetCurrentSummitExternalLocations() + { + $params = array + ( + 'id' => 'current', + ); + + $headers = array + ( + "HTTP_Authorization" => " Bearer " .$this->access_token, + "CONTENT_TYPE" => "application/json" + ); + + $response = $this->action + ( + "GET", + "OAuth2SummitLocationsApiController@getExternalLocations", + $params, + array(), + array(), + array(), + $headers + ); + + $content = $response->getContent(); + $this->assertResponseStatus(200); + + $locations = json_decode($content); + $this->assertTrue(!is_null($locations)); + } + public function testGetCurrentSummitLocation() { $params = array