From 1ebb1cd9085522d2ae183c97cdc8d585baf35f6e Mon Sep 17 00:00:00 2001 From: Sebastian Marcet Date: Tue, 27 Feb 2018 19:44:59 -0300 Subject: [PATCH] Added new endpoints GET /api/v1/summits/{id}/locations/metadata GET /api/v1/summits/{id}/locations Change-Id: If17e03e61c142e196dca0cd3358ea62e6def3809 --- .../OAuth2SummitLocationsApiController.php | 473 +++++++++++------- app/Http/Utils/PagingConstants.php | 23 + app/Http/routes.php | 2 + .../Locations/SummitAbstractLocation.php | 58 ++- .../Summit/Locations/SummitAirport.php | 18 +- .../Locations/SummitExternalLocation.php | 18 +- .../Locations/SummitGeoLocatedLocation.php | 22 + .../Summit/Locations/SummitHotel.php | 18 +- .../Locations/SummitLocationConstants.php | 30 ++ .../Summit/Locations/SummitVenue.php | 22 +- .../ISummitLocationRepository.php | 46 ++ ...ISummitRegistrationPromoCodeRepository.php | 2 +- app/Repositories/RepositoriesProvider.php | 9 + .../DoctrineSummitLocationRepository.php | 162 ++++++ database/seeds/ApiEndpointsSeeder.php | 41 +- tests/OAuth2SummitApiTest.php | 298 ----------- tests/OAuth2SummitLocationsApiTest.php | 381 ++++++++++++++ 17 files changed, 1095 insertions(+), 528 deletions(-) create mode 100644 app/Http/Utils/PagingConstants.php create mode 100644 app/Models/Foundation/Summit/Locations/SummitLocationConstants.php create mode 100644 app/Models/Foundation/Summit/Repositories/ISummitLocationRepository.php create mode 100644 app/Repositories/Summit/DoctrineSummitLocationRepository.php create mode 100644 tests/OAuth2SummitLocationsApiTest.php diff --git a/app/Http/Controllers/Apis/Protected/Summit/OAuth2SummitLocationsApiController.php b/app/Http/Controllers/Apis/Protected/Summit/OAuth2SummitLocationsApiController.php index f716af68..7f2347af 100644 --- a/app/Http/Controllers/Apis/Protected/Summit/OAuth2SummitLocationsApiController.php +++ b/app/Http/Controllers/Apis/Protected/Summit/OAuth2SummitLocationsApiController.php @@ -12,6 +12,9 @@ * limitations under the License. **/ +use App\Http\Utils\PagingConstants; +use App\Models\Foundation\Summit\Locations\SummitLocationConstants; +use App\Models\Foundation\Summit\Repositories\ISummitLocationRepository; use Exception; use Illuminate\Support\Facades\Input; use Illuminate\Support\Facades\Log; @@ -27,6 +30,7 @@ use models\summit\ISummitRepository; use ModelSerializers\SerializerRegistry; use services\model\ISummitService; use utils\Filter; +use utils\FilterElement; use utils\FilterParser; use utils\FilterParserException; use utils\OrderParser; @@ -59,6 +63,11 @@ final class OAuth2SummitLocationsApiController extends OAuth2ProtectedController */ private $event_feedback_repository; + /** + * @var ISummitLocationRepository + */ + private $location_repository; + public function __construct ( @@ -66,6 +75,7 @@ final class OAuth2SummitLocationsApiController extends OAuth2ProtectedController ISummitEventRepository $event_repository, ISpeakerRepository $speaker_repository, IEventFeedbackRepository $event_feedback_repository, + ISummitLocationRepository $location_repository, ISummitService $service, IResourceServerContext $resource_server_context ) { @@ -74,176 +84,127 @@ final class OAuth2SummitLocationsApiController extends OAuth2ProtectedController $this->speaker_repository = $speaker_repository; $this->event_repository = $event_repository; $this->event_feedback_repository = $event_feedback_repository; + $this->location_repository = $location_repository; $this->service = $service; } + /** + * @param $filter_element + * @return bool + */ + private function validateClassName($filter_element){ + if($filter_element instanceof FilterElement){ + return in_array($filter_element->getValue(), SummitLocationConstants::$valid_class_names); + } + $valid = true; + foreach($filter_element[0] as $elem){ + $valid = $valid && in_array($elem->getValue(), SummitLocationConstants::$valid_class_names); + } + return $valid; + } + + /** + * @param $summit_id + * @return mixed + */ public function getLocations($summit_id) { - try { - $summit = SummitFinderStrategyFactory::build($this->repository, $this->resource_server_context)->find($summit_id); - if (is_null($summit)) return $this->error404(); - - //locations - $locations = array(); - foreach ($summit->getLocations() as $location) - { - $locations[] = SerializerRegistry::getInstance()->getSerializer($location)->serialize(); - } - - $response = new PagingResponse - ( - count($locations), - count($locations), - 1, - 1, - $locations - ); - - return $this->ok($response->toArray($expand = Input::get('expand',''))); - - } catch (Exception $ex) { - Log::error($ex); - return $this->error500($ex); - } - } - - /** - * @param $summit_id - * @param $location_id - * @return mixed - */ - public function getLocation($summit_id, $location_id) - { - try { - - $expand = Request::input('expand', ''); - $relations = Request::input('relations', ''); - $relations = !empty($relations) ? explode(',', $relations) : []; - $summit = SummitFinderStrategyFactory::build($this->repository, $this->resource_server_context)->find($summit_id); - if (is_null($summit)) return $this->error404(); - - $location = $summit->getLocation($location_id); - if (is_null($location)) { - return $this->error404(); - } - return $this->ok(SerializerRegistry::getInstance()->getSerializer($location)->serialize($expand,[], $relations)); - } catch (Exception $ex) { - Log::error($ex); - return $this->error500($ex); - } - } - - /** - * @param string $summit_id - * @param string $location_id - * @param bool $published - * @return PagingResponse - * @throws EntityNotFoundException - * @throws ValidationException - */ - private function _getLocationEvents($summit_id, $location_id, $published = true) - { - $summit = SummitFinderStrategyFactory::build($this->repository, $this->resource_server_context)->find($summit_id); - if (is_null($summit)) - throw new EntityNotFoundException; - - if(strtolower($location_id) != "tbd") { - $location = $summit->getLocation(intval($location_id)); - if (is_null($location)) - throw new EntityNotFoundException; - } - $values = Input::all(); + $rules = [ - $rules = - [ 'page' => 'integer|min:1', - 'per_page' => 'required_with:page|integer|min:5|max:100', + 'per_page' => sprintf('required_with:page|integer|min:%s|max:%s', PagingConstants::MinPageSize, PagingConstants::MaxPageSize), ]; - $validation = Validator::make($values, $rules); - - if ($validation->fails()) { - $ex = new ValidationException(); - throw $ex->setMessages($validation->messages()->toArray()); - } - - // default values - $page = 1; - $per_page = 5; - - if (Input::has('page')) { - $page = intval(Input::get('page')); - $per_page = intval(Input::get('per_page')); - } - - $filter = null; - - if (Input::has('filter')) { - $filter = FilterParser::parse(Input::get('filter'), - [ - 'title' => ['=@', '=='], - 'start_date' => ['>', '<', '<=', '>=', '=='], - 'end_date' => ['>', '<', '<=', '>=', '=='], - 'speaker' => ['=@', '=='], - 'tags' => ['=@', '=='], - 'event_type_id' => ['=='], - 'track_id' => ['=='] - ]); - } - - $order = null; - - if (Input::has('order')) - { - $order = OrderParser::parse(Input::get('order'), - [ - 'title', - 'start_date', - 'end_date', - 'id', - 'created', - ]); - } - - if(is_null($filter)) $filter = new Filter(); - - $filter->addFilterCondition(FilterParser::buildFilter('summit_id','==', $summit_id)); - - if(intval($location_id) > 0) - $filter->addFilterCondition(FilterParser::buildFilter('location_id','==', $location_id)); - - if($published) - { - $filter->addFilterCondition(FilterParser::buildFilter('published','==', 1)); - } - - return strtolower($location_id) == "tbd" ? - $this->event_repository->getAllByPageLocationTBD(new PagingInfo($page, $per_page), $filter, $order): - $this->event_repository->getAllByPage(new PagingInfo($page, $per_page), $filter, $order); - } - - /** - * @param $summit_id - * @param $location_id - * @return mixed - */ - public function getLocationEvents($summit_id, $location_id) - { try { - return $this->ok($this->_getLocationEvents($summit_id, $location_id, false)->toArray(Request::input('expand', ''))); + + $summit = SummitFinderStrategyFactory::build($this->repository, $this->resource_server_context)->find($summit_id); + if (is_null($summit)) return $this->error404(); + + $validation = Validator::make($values, $rules); + + if ($validation->fails()) { + $ex = new ValidationException(); + throw $ex->setMessages($validation->messages()->toArray()); + } + + // default values + $page = 1; + $per_page = PagingConstants::DefaultPageSize; + + if (Input::has('page')) { + $page = intval(Input::get('page')); + $per_page = intval(Input::get('per_page')); + } + + $filter = null; + + if (Input::has('filter')) { + $filter = FilterParser::parse(Input::get('filter'), [ + 'class_name' => ['=='], + 'name' => ['==', '=@'], + 'description' => ['=@'], + 'address1' => ['=@'], + 'address2' => ['=@'], + 'zip_code' => ['==','=@'], + 'city' => ['==','=@'], + 'state' => ['==','=@'], + 'country' => ['==','=@'], + 'sold_out' => ['=='], + 'is_main' => ['=='], + ]); + } + + $order = null; + + if (Input::has('order')) + { + $order = OrderParser::parse(Input::get('order'), [ + 'id', + 'name', + 'order' + ]); + } + + if(is_null($filter)) $filter = new Filter(); + + if($filter->hasFilter("class_name") && !$this->validateClassName($filter->getFilter("class_name"))){ + throw new ValidationException( + sprintf + ( + "class_name filter has an invalid value ( valid values are %s", + implode(", ", SummitLocationConstants::$valid_class_names) + ) + ); + } + + $data = $this->location_repository->getBySummit($summit, new PagingInfo($page, $per_page), $filter, $order); + + return $this->ok + ( + $data->toArray + ( + Request::input('expand', ''), + [], + [], + [] + ) + ); } - catch (EntityNotFoundException $ex1) { + catch (ValidationException $ex1) + { Log::warning($ex1); - return $this->error404(); + return $this->error412(array( $ex1->getMessage())); } - catch (ValidationException $ex2) { + catch (EntityNotFoundException $ex2) + { Log::warning($ex2); - return $this->error412($ex2->getMessages()); + return $this->error404(array('message' => $ex2->getMessage())); } - catch(FilterParserException $ex3){ + catch(\HTTP401UnauthorizedException $ex3) + { Log::warning($ex3); - return $this->error412($ex3->getMessages()); + return $this->error401(); } catch (Exception $ex) { Log::error($ex); @@ -251,31 +212,6 @@ final class OAuth2SummitLocationsApiController extends OAuth2ProtectedController } } - /** - * @param $summit_id - * @param $location_id - * @return mixed - */ - public function getLocationPublishedEvents($summit_id, $location_id) - { - try { - return $this->ok($this->_getLocationEvents($summit_id, $location_id, true)->toArray(Request::input('expand', ''))); - } - catch (EntityNotFoundException $ex1) { - Log::warning($ex1); - return $this->error404(); - } - catch (ValidationException $ex2) { - Log::warning($ex2); - return $this->error412($ex2->getMessages()); - } - catch (Exception $ex) { - Log::error($ex); - return $this->error500($ex); - } - } - - /** * @param $summit_id * @return mixed @@ -380,7 +316,6 @@ final class OAuth2SummitLocationsApiController extends OAuth2ProtectedController } } - /** * @param $summit_id * @return mixed @@ -414,5 +349,185 @@ final class OAuth2SummitLocationsApiController extends OAuth2ProtectedController return $this->error500($ex); } } + /** + * @param $summit_id + * @param $location_id + * @return mixed + */ + public function getLocation($summit_id, $location_id) + { + try { + $expand = Request::input('expand', ''); + $relations = Request::input('relations', ''); + $relations = !empty($relations) ? explode(',', $relations) : []; + $summit = SummitFinderStrategyFactory::build($this->repository, $this->resource_server_context)->find($summit_id); + if (is_null($summit)) return $this->error404(); + + $location = $summit->getLocation($location_id); + if (is_null($location)) { + return $this->error404(); + } + return $this->ok(SerializerRegistry::getInstance()->getSerializer($location)->serialize($expand,[], $relations)); + } catch (Exception $ex) { + Log::error($ex); + return $this->error500($ex); + } + } + + /** + * @param string $summit_id + * @param string $location_id + * @param bool $published + * @return PagingResponse + * @throws EntityNotFoundException + * @throws ValidationException + */ + private function _getLocationEvents($summit_id, $location_id, $published = true) + { + $summit = SummitFinderStrategyFactory::build($this->repository, $this->resource_server_context)->find($summit_id); + if (is_null($summit)) + throw new EntityNotFoundException; + + if(strtolower($location_id) != "tbd") { + $location = $summit->getLocation(intval($location_id)); + if (is_null($location)) + throw new EntityNotFoundException; + } + + $values = Input::all(); + + $rules = + [ + 'page' => 'integer|min:1', + 'per_page' => sprintf('required_with:page|integer|min:%s|max:%s', PagingConstants::MinPageSize, PagingConstants::MaxPageSize), + ]; + + $validation = Validator::make($values, $rules); + + if ($validation->fails()) { + $ex = new ValidationException(); + throw $ex->setMessages($validation->messages()->toArray()); + } + + // default values + $page = 1; + $per_page = PagingConstants::DefaultPageSize; + + if (Input::has('page')) { + $page = intval(Input::get('page')); + $per_page = intval(Input::get('per_page')); + } + + $filter = null; + + if (Input::has('filter')) { + $filter = FilterParser::parse(Input::get('filter'), + [ + 'title' => ['=@', '=='], + 'start_date' => ['>', '<', '<=', '>=', '=='], + 'end_date' => ['>', '<', '<=', '>=', '=='], + 'speaker' => ['=@', '=='], + 'tags' => ['=@', '=='], + 'event_type_id' => ['=='], + 'track_id' => ['=='] + ]); + } + + $order = null; + + if (Input::has('order')) + { + $order = OrderParser::parse(Input::get('order'), + [ + 'title', + 'start_date', + 'end_date', + 'id', + 'created', + ]); + } + + if(is_null($filter)) $filter = new Filter(); + + $filter->addFilterCondition(FilterParser::buildFilter('summit_id','==', $summit_id)); + + if(intval($location_id) > 0) + $filter->addFilterCondition(FilterParser::buildFilter('location_id','==', $location_id)); + + if($published) + { + $filter->addFilterCondition(FilterParser::buildFilter('published','==', 1)); + } + + return strtolower($location_id) == "tbd" ? + $this->event_repository->getAllByPageLocationTBD(new PagingInfo($page, $per_page), $filter, $order): + $this->event_repository->getAllByPage(new PagingInfo($page, $per_page), $filter, $order); + } + + /** + * @param $summit_id + * @param $location_id + * @return mixed + */ + public function getLocationEvents($summit_id, $location_id) + { + try { + return $this->ok($this->_getLocationEvents($summit_id, $location_id, false)->toArray(Request::input('expand', ''))); + } + catch (EntityNotFoundException $ex1) { + Log::warning($ex1); + return $this->error404(); + } + catch (ValidationException $ex2) { + Log::warning($ex2); + return $this->error412($ex2->getMessages()); + } + catch(FilterParserException $ex3){ + Log::warning($ex3); + return $this->error412($ex3->getMessages()); + } + catch (Exception $ex) { + Log::error($ex); + return $this->error500($ex); + } + } + + /** + * @param $summit_id + * @param $location_id + * @return mixed + */ + public function getLocationPublishedEvents($summit_id, $location_id) + { + try { + return $this->ok($this->_getLocationEvents($summit_id, $location_id, true)->toArray(Request::input('expand', ''))); + } + catch (EntityNotFoundException $ex1) { + Log::warning($ex1); + return $this->error404(); + } + catch (ValidationException $ex2) { + Log::warning($ex2); + return $this->error412($ex2->getMessages()); + } + catch (Exception $ex) { + Log::error($ex); + return $this->error500($ex); + } + } + + /** + * @param $summit_id + * @return mixed + */ + public function getMetadata($summit_id){ + $summit = SummitFinderStrategyFactory::build($this->repository, $this->resource_server_context)->find($summit_id); + if (is_null($summit)) return $this->error404(); + + return $this->ok + ( + $this->location_repository->getMetadata($summit) + ); + } } \ No newline at end of file diff --git a/app/Http/Utils/PagingConstants.php b/app/Http/Utils/PagingConstants.php new file mode 100644 index 00000000..46f48b0e --- /dev/null +++ b/app/Http/Utils/PagingConstants.php @@ -0,0 +1,23 @@ += MinPageSize and <= MaxPageSize + const MinPageSize = 5; + const MaxPageSize = 100; +} \ No newline at end of file diff --git a/app/Http/routes.php b/app/Http/routes.php index 2de69a49..4cb40386 100644 --- a/app/Http/routes.php +++ b/app/Http/routes.php @@ -286,6 +286,8 @@ Route::group([ Route::get('/external-locations', 'OAuth2SummitLocationsApiController@getExternalLocations'); Route::get('/hotels', 'OAuth2SummitLocationsApiController@getHotels'); Route::get('/airports', 'OAuth2SummitLocationsApiController@getAirports'); + Route::get('metadata', [ 'middleware' => 'auth.user:administrators|summit-front-end-administrators', 'uses' => 'OAuth2SummitLocationsApiController@getMetadata']); + Route::group(array('prefix' => '{location_id}'), function () { Route::get('', 'OAuth2SummitLocationsApiController@getLocation'); Route::get('/events/published','OAuth2SummitLocationsApiController@getLocationPublishedEvents')->where('location_id', 'tbd|[0-9]+'); diff --git a/app/Models/Foundation/Summit/Locations/SummitAbstractLocation.php b/app/Models/Foundation/Summit/Locations/SummitAbstractLocation.php index b593df66..aa9dee23 100644 --- a/app/Models/Foundation/Summit/Locations/SummitAbstractLocation.php +++ b/app/Models/Foundation/Summit/Locations/SummitAbstractLocation.php @@ -27,7 +27,7 @@ use Doctrine\ORM\Mapping AS ORM; */ /** - * @ORM\Entity + * @ORM\Entity(repositoryClass="App\Repositories\Summit\DoctrineSummitLocationRepository") * @ORM\Table(name="SummitAbstractLocation") * @ORM\InheritanceType("JOINED") * @ORM\DiscriminatorColumn(name="ClassName", type="string") @@ -41,6 +41,40 @@ class SummitAbstractLocation extends SilverstripeBaseModel const TypeInternal = 'Internal'; const TypeNone = 'None'; + /** + * @ORM\Column(name="Name", type="string") + */ + protected $name; + + /** + * @ORM\Column(name="Description", type="string") + */ + protected $description; + + /** + * @ORM\Column(name="LocationType", type="string") + */ + protected $type; + + /** + * @ORM\Column(name="Order", type="integer") + */ + protected $order; + + public static $metadata = [ + 'name' => 'string', + 'description' => 'string', + 'type' => 'string', + 'order' => 'integer', + ]; + + /** + * @return array + */ + public static function getMetadata(){ + return self::$metadata; + } + public function __construct() { parent::__construct(); @@ -120,28 +154,6 @@ class SummitAbstractLocation extends SilverstripeBaseModel use SummitOwned; - - /** - * @ORM\Column(name="Name", type="string") - */ - protected $name; - - /** - * @ORM\Column(name="Description", type="string") - */ - protected $description; - - - /** - * @ORM\Column(name="LocationType", type="string") - */ - protected $type; - - /** - * @ORM\Column(name="Order", type="integer") - */ - protected $order; - /** * @return boolean */ diff --git a/app/Models/Foundation/Summit/Locations/SummitAirport.php b/app/Models/Foundation/Summit/Locations/SummitAirport.php index 5508d267..677b4f39 100644 --- a/app/Models/Foundation/Summit/Locations/SummitAirport.php +++ b/app/Models/Foundation/Summit/Locations/SummitAirport.php @@ -11,9 +11,7 @@ * See the License for the specific language governing permissions and * limitations under the License. **/ - use Doctrine\ORM\Mapping AS ORM; - /** * @ORM\Entity * @ORM\Table(name="SummitAirport") @@ -26,9 +24,11 @@ class SummitAirport extends SummitExternalLocation * @return string */ public function getClassName(){ - return 'SummitAirport'; + return self::ClassName; } + const ClassName = 'SummitAirport'; + /** * @return string */ @@ -49,4 +49,16 @@ class SummitAirport extends SummitExternalLocation * @ORM\Column(name="Type", type="string") */ private $airport_type; + + public static $metadata = [ + 'class_name' => self::ClassName, + 'airport_type' => 'string', + ]; + + /** + * @return array + */ + public static function getMetadata(){ + return array_merge(SummitExternalLocation::getMetadata(), self::$metadata); + } } \ No newline at end of file diff --git a/app/Models/Foundation/Summit/Locations/SummitExternalLocation.php b/app/Models/Foundation/Summit/Locations/SummitExternalLocation.php index 7f33007c..f579491f 100644 --- a/app/Models/Foundation/Summit/Locations/SummitExternalLocation.php +++ b/app/Models/Foundation/Summit/Locations/SummitExternalLocation.php @@ -11,9 +11,7 @@ * See the License for the specific language governing permissions and * limitations under the License. **/ - use Doctrine\ORM\Mapping AS ORM; - /** * @ORM\Entity * @ORM\Table(name="SummitExternalLocation") @@ -26,11 +24,13 @@ class SummitExternalLocation extends SummitGeoLocatedLocation const Lounge = 'Lounge'; const Other = 'Other'; + const ClassName = 'SummitExternalLocation'; + /** * @return string */ public function getClassName(){ - return 'SummitExternalLocation'; + return self::ClassName; } /** @@ -54,4 +54,16 @@ class SummitExternalLocation extends SummitGeoLocatedLocation */ protected $capacity; + public static $metadata = [ + 'class_name' => self::ClassName, + 'capacity' => 'integer', + ]; + + /** + * @return array + */ + public static function getMetadata(){ + return array_merge(SummitGeoLocatedLocation::getMetadata(), self::$metadata); + } + } \ No newline at end of file diff --git a/app/Models/Foundation/Summit/Locations/SummitGeoLocatedLocation.php b/app/Models/Foundation/Summit/Locations/SummitGeoLocatedLocation.php index b4dcaf62..816d8ce5 100644 --- a/app/Models/Foundation/Summit/Locations/SummitGeoLocatedLocation.php +++ b/app/Models/Foundation/Summit/Locations/SummitGeoLocatedLocation.php @@ -98,6 +98,28 @@ class SummitGeoLocatedLocation extends SummitAbstractLocation */ protected $images; + public static $metadata = [ + 'address1' => 'string', + 'address2' => 'string', + 'zip_code' => 'string', + 'city' => 'string', + 'state' => 'string', + 'country' => 'string', + 'website_url' => 'string', + 'lng' => 'string', + 'lat' => 'string', + 'display_on_site' => 'boolean', + 'details_page' => 'boolean', + 'location_message' => 'string', + 'images' => 'array', + ]; + + /** + * @return array + */ + public static function getMetadata(){ + return array_merge(SummitAbstractLocation::getMetadata(), self::$metadata); + } /** * @param SummitVenueRoom|null $room diff --git a/app/Models/Foundation/Summit/Locations/SummitHotel.php b/app/Models/Foundation/Summit/Locations/SummitHotel.php index 22694221..3e7ee8fb 100644 --- a/app/Models/Foundation/Summit/Locations/SummitHotel.php +++ b/app/Models/Foundation/Summit/Locations/SummitHotel.php @@ -26,9 +26,11 @@ class SummitHotel extends SummitExternalLocation * @return string */ public function getClassName(){ - return 'SummitHotel'; + return self::ClassName; } + const ClassName = 'SummitHotel'; + /** * @return string */ @@ -91,4 +93,18 @@ class SummitHotel extends SummitExternalLocation */ private $hotel_type; + public static $metadata = [ + 'class_name' => self::ClassName, + 'hotel_type' => 'string', + 'sold_out' => 'boolean', + 'booking_link' => 'string', + ]; + + /** + * @return array + */ + public static function getMetadata(){ + return array_merge(SummitExternalLocation::getMetadata(), self::$metadata); + } + } \ No newline at end of file diff --git a/app/Models/Foundation/Summit/Locations/SummitLocationConstants.php b/app/Models/Foundation/Summit/Locations/SummitLocationConstants.php new file mode 100644 index 00000000..e13f38fa --- /dev/null +++ b/app/Models/Foundation/Summit/Locations/SummitLocationConstants.php @@ -0,0 +1,30 @@ + self::ClassName, + 'is_main' => 'boolean', + 'floors' => 'array', + 'rooms' => 'array', + ]; + + /** + * @return array + */ + public static function getMetadata(){ + return array_merge(SummitGeoLocatedLocation::getMetadata(), self::$metadata); + } + } \ No newline at end of file diff --git a/app/Models/Foundation/Summit/Repositories/ISummitLocationRepository.php b/app/Models/Foundation/Summit/Repositories/ISummitLocationRepository.php new file mode 100644 index 00000000..5738c8bd --- /dev/null +++ b/app/Models/Foundation/Summit/Repositories/ISummitLocationRepository.php @@ -0,0 +1,46 @@ + 'al.name:json_string', + 'description' => 'al.description:json_string', + 'address1' => 'gll.address1:json_string', + 'address2' => 'gll.address2:json_string', + 'zip_code' => 'gll.zip_code:json_string', + 'city' => 'gll.city:json_string', + 'state' => 'gll.state:json_string', + 'country' => 'gll.country:json_string', + 'sold_out' => 'h.sold_out:json_boolean', + 'is_main' => 'v.is_main:json_boolean', + 'class_name' => new DoctrineInstanceOfFilterMapping( + "al", + [ + SummitVenue::ClassName => SummitVenue::class, + SummitHotel::ClassName => SummitHotel::class, + SummitExternalLocation::ClassName => SummitExternalLocation::class, + SummitAirport::ClassName => SummitAirport::class, + ] + ) + ]; + } + + /** + * @return array + */ + protected function getOrderMappings() + { + return [ + 'id' => 'al.id', + 'name' => 'al.name', + 'order' => 'al.order', + ]; + } + + /** + * @param Summit $summit + * @param PagingInfo $paging_info + * @param Filter|null $filter + * @param Order|null $order + * @return PagingResponse + */ + public function getBySummit + ( + Summit $summit, + PagingInfo $paging_info, + Filter $filter = null, + Order $order = null + ) + { + $query = $this->getEntityManager() + ->createQueryBuilder() + ->select("al") + ->from(SummitAbstractLocation::class, "al") + ->leftJoin(SummitGeoLocatedLocation::class, 'gll', 'WITH', 'gll.id = al.id') + ->leftJoin(SummitVenue::class, 'v', 'WITH', 'v.id = gll.id') + ->leftJoin(SummitExternalLocation::class, 'el', 'WITH', 'el.id = gll.id') + ->leftJoin(SummitHotel::class, 'h', 'WITH', 'h.id = el.id') + ->leftJoin(SummitAirport::class, 'ap', 'WITH', 'ap.id = el.id') + ->leftJoin('al.summit', 's') + ->where("s.id = :summit_id") + ->andWhere("not al INSTANCE OF ('" . implode("','", self::$forbidded_classes) . "')"); + + $query->setParameter("summit_id", $summit->getId()); + + if(!is_null($filter)){ + $filter->apply2Query($query, $this->getFilterMappings()); + } + + if (!is_null($order)) { + $order->apply2Query($query, $this->getOrderMappings()); + } else { + //default order + $query = $query->addOrderBy("al.id",'ASC'); + } + + $query = $query + ->setFirstResult($paging_info->getOffset()) + ->setMaxResults($paging_info->getPerPage()); + + $paginator = new Paginator($query, $fetchJoinCollection = true); + $total = $paginator->count(); + $data = []; + + foreach($paginator as $entity) + $data[] = $entity; + + return new PagingResponse + ( + $total, + $paging_info->getPerPage(), + $paging_info->getCurrentPage(), + $paging_info->getLastPage($total), + $data + ); + } + + /** + * @param Summit $summit + * @return array + */ + public function getMetadata(Summit $summit) + { + return [ + SummitVenue::getMetadata(), + SummitAirport::getMetadata(), + SummitHotel::getMetadata(), + SummitExternalLocation::getMetadata() + ]; + } +} \ No newline at end of file diff --git a/database/seeds/ApiEndpointsSeeder.php b/database/seeds/ApiEndpointsSeeder.php index 328a4358..337578c8 100644 --- a/database/seeds/ApiEndpointsSeeder.php +++ b/database/seeds/ApiEndpointsSeeder.php @@ -464,7 +464,7 @@ class ApiEndpointsSeeder extends Seeder 'scopes' => [sprintf(SummitScopes::WriteSummitData, $current_realm)], ), // locations - array( + [ 'name' => 'get-locations', 'route' => '/api/v1/summits/{id}/locations', 'http_method' => 'GET', @@ -472,8 +472,17 @@ class ApiEndpointsSeeder extends Seeder sprintf(SummitScopes::ReadSummitData, $current_realm), sprintf(SummitScopes::ReadAllSummitData, $current_realm) ], - ), - array( + ], + [ + 'name' => 'get-locations-metadata', + 'route' => '/api/v1/summits/{id}/locations/metadata', + 'http_method' => 'GET', + 'scopes' => [ + sprintf(SummitScopes::ReadSummitData, $current_realm), + sprintf(SummitScopes::ReadAllSummitData, $current_realm) + ], + ], + [ 'name' => 'get-venues', 'route' => '/api/v1/summits/{id}/locations/venues', 'http_method' => 'GET', @@ -481,8 +490,8 @@ class ApiEndpointsSeeder extends Seeder sprintf(SummitScopes::ReadSummitData, $current_realm), sprintf(SummitScopes::ReadAllSummitData, $current_realm) ], - ), - array( + ], + [ 'name' => 'get-external-locations', 'route' => '/api/v1/summits/{id}/locations/external-locations', 'http_method' => 'GET', @@ -490,8 +499,8 @@ class ApiEndpointsSeeder extends Seeder sprintf(SummitScopes::ReadSummitData, $current_realm), sprintf(SummitScopes::ReadAllSummitData, $current_realm) ], - ), - array( + ], + [ 'name' => 'get-hotels', 'route' => '/api/v1/summits/{id}/locations/hotels', 'http_method' => 'GET', @@ -499,8 +508,8 @@ class ApiEndpointsSeeder extends Seeder sprintf(SummitScopes::ReadSummitData, $current_realm), sprintf(SummitScopes::ReadAllSummitData, $current_realm) ], - ), - array( + ], + [ 'name' => 'get-airports', 'route' => '/api/v1/summits/{id}/locations/airports', 'http_method' => 'GET', @@ -508,8 +517,8 @@ class ApiEndpointsSeeder extends Seeder sprintf(SummitScopes::ReadSummitData, $current_realm), sprintf(SummitScopes::ReadAllSummitData, $current_realm) ], - ), - array( + ], + [ 'name' => 'get-location', 'route' => '/api/v1/summits/{id}/locations/{location_id}', 'http_method' => 'GET', @@ -517,8 +526,8 @@ class ApiEndpointsSeeder extends Seeder sprintf(SummitScopes::ReadSummitData, $current_realm), sprintf(SummitScopes::ReadAllSummitData, $current_realm) ], - ), - array( + ], + [ 'name' => 'get-location-events', 'route' => '/api/v1/summits/{id}/locations/{location_id}/events', 'http_method' => 'GET', @@ -526,8 +535,8 @@ class ApiEndpointsSeeder extends Seeder sprintf(SummitScopes::ReadSummitData, $current_realm), sprintf(SummitScopes::ReadAllSummitData, $current_realm) ], - ), - array( + ], + [ 'name' => 'get-location-published-events', 'route' => '/api/v1/summits/{id}/locations/{location_id}/events/published', 'http_method' => 'GET', @@ -535,7 +544,7 @@ class ApiEndpointsSeeder extends Seeder sprintf(SummitScopes::ReadSummitData, $current_realm), sprintf(SummitScopes::ReadAllSummitData, $current_realm) ], - ), + ], // event types [ 'name' => 'get-event-types', diff --git a/tests/OAuth2SummitApiTest.php b/tests/OAuth2SummitApiTest.php index fc99a04b..1463eb49 100644 --- a/tests/OAuth2SummitApiTest.php +++ b/tests/OAuth2SummitApiTest.php @@ -671,196 +671,6 @@ final class OAuth2SummitApiTest extends ProtectedApiTest $this->assertTrue(!is_null($events)); } - public function testGetCurrentSummitLocations() - { - $params = array - ( - 'id' => 'current', - ); - - $headers = array - ( - "HTTP_Authorization" => " Bearer " . $this->access_token, - "CONTENT_TYPE" => "application/json" - ); - - $response = $this->action - ( - "GET", - "OAuth2SummitLocationsApiController@getLocations", - $params, - array(), - array(), - array(), - $headers - ); - - $content = $response->getContent(); - $this->assertResponseStatus(200); - - $locations = json_decode($content); - $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 - ( - 'id' => 'current', - 'location_id' => 25 - ); - - $headers = array - ( - "HTTP_Authorization" => " Bearer " . $this->access_token, - "CONTENT_TYPE" => "application/json" - ); - - $response = $this->action - ( - "GET", - "OAuth2SummitLocationsApiController@getLocation", - $params, - array(), - array(), - array(), - $headers - ); - - $content = $response->getContent(); - $this->assertResponseStatus(200); - - $locations = json_decode($content); - $this->assertTrue(!is_null($locations)); - } - public function testGetCurrentSummitExternalOrder() { $params = array @@ -958,114 +768,6 @@ final class OAuth2SummitApiTest extends ProtectedApiTest $this->assertTrue(!is_null($attendee)); } - public function testCurrentSummitLocationEventsWithFilter($summit_id = 7) - { - $params = array - ( - 'id' => $summit_id, - 'page' => 1, - 'per_page' => 50, - 'location_id' => 52, - 'filter' => array - ( - 'tags=@Nova', - 'speaker=@Todd' - ) - ); - - $headers = array - ( - "HTTP_Authorization" => " Bearer " . $this->access_token, - "CONTENT_TYPE" => "application/json" - ); - - - $response = $this->action - ( - "GET", - "OAuth2SummitLocationsApiController@getLocationEvents", - $params, - array(), - array(), - array(), - $headers - ); - - $content = $response->getContent(); - $this->assertResponseStatus(200); - - $events = json_decode($content); - $this->assertTrue(!is_null($events)); - } - - public function testCurrentSummitPublishedLocationEventsWithFilter() - { - $params = array - ( - 'id' => 23, - 'location_id' => 311, - 'filter' => [ - - 'start_date>=1451479955' - ] - ); - - $headers = array - ( - "HTTP_Authorization" => " Bearer " . $this->access_token, - "CONTENT_TYPE" => "application/json" - ); - - $response = $this->action - ( - "GET", - "OAuth2SummitLocationsApiController@getLocationPublishedEvents", - $params, - array(), - array(), - array(), - $headers - ); - - $content = $response->getContent(); - $this->assertResponseStatus(200); - - $events = json_decode($content); - $this->assertTrue(!is_null($events)); - } - - public function testCurrentSummitPublishedLocationTBAEvents() - { - $params = array - ( - 'id' => 23, - 'location_id' => "tba", - ); - - $headers = array - ( - "HTTP_Authorization" => " Bearer " . $this->access_token, - "CONTENT_TYPE" => "application/json" - ); - - $response = $this->action - ( - "GET", - "OAuth2SummitLocationsApiController@getLocationPublishedEvents", - $params, - array(), - array(), - array(), - $headers - ); - - $content = $response->getContent(); - $this->assertResponseStatus(200); - - $events = json_decode($content); - $this->assertTrue(!is_null($events)); - } - public function testAddPresentationVideo($summit_id = 7, $presentation_id = 15404) { $params = array diff --git a/tests/OAuth2SummitLocationsApiTest.php b/tests/OAuth2SummitLocationsApiTest.php new file mode 100644 index 00000000..1cdbc536 --- /dev/null +++ b/tests/OAuth2SummitLocationsApiTest.php @@ -0,0 +1,381 @@ + $summit_id, + 'page' => 1, + 'per_page' => 5, + 'order' => '-order' + ]; + + $headers = + [ + "HTTP_Authorization" => " Bearer " . $this->access_token, + "CONTENT_TYPE" => "application/json" + ]; + + $response = $this->action + ( + "GET", + "OAuth2SummitLocationsApiController@getLocations", + $params, + [], + [], + [], + $headers + ); + + $content = $response->getContent(); + $this->assertResponseStatus(200); + + $locations = json_decode($content); + $this->assertTrue(!is_null($locations)); + } + + public function testGetCurrentSummitLocationsMetadata($summit_id = 23) + { + $params = [ + 'id' => $summit_id, + ]; + + $headers = + [ + "HTTP_Authorization" => " Bearer " . $this->access_token, + "CONTENT_TYPE" => "application/json" + ]; + + $response = $this->action + ( + "GET", + "OAuth2SummitLocationsApiController@getMetadata", + $params, + [], + [], + [], + $headers + ); + + $content = $response->getContent(); + $this->assertResponseStatus(200); + + $metadata = json_decode($content); + $this->assertTrue(!is_null($metadata)); + } + + public function testGetCurrentSummitLocationsByClassNameVenueORAirport($summit_id = 24) + { + $params = [ + 'id' => $summit_id, + 'page' => 1, + 'per_page' => 5, + 'filter' => [ + 'class_name=='.\models\summit\SummitVenue::ClassName.',class_name=='.\models\summit\SummitAirport::ClassName, + ] + ]; + + $headers = + [ + "HTTP_Authorization" => " Bearer " . $this->access_token, + "CONTENT_TYPE" => "application/json" + ]; + + $response = $this->action + ( + "GET", + "OAuth2SummitLocationsApiController@getLocations", + $params, + [], + [], + [], + $headers + ); + + $content = $response->getContent(); + $this->assertResponseStatus(200); + + $locations = json_decode($content); + $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 + ( + 'id' => 'current', + 'location_id' => 25 + ); + + $headers = array + ( + "HTTP_Authorization" => " Bearer " . $this->access_token, + "CONTENT_TYPE" => "application/json" + ); + + $response = $this->action + ( + "GET", + "OAuth2SummitLocationsApiController@getLocation", + $params, + array(), + array(), + array(), + $headers + ); + + $content = $response->getContent(); + $this->assertResponseStatus(200); + + $locations = json_decode($content); + $this->assertTrue(!is_null($locations)); + } + + public function testCurrentSummitLocationEventsWithFilter($summit_id = 7) + { + $params = array + ( + 'id' => $summit_id, + 'page' => 1, + 'per_page' => 50, + 'location_id' => 52, + 'filter' => array + ( + 'tags=@Nova', + 'speaker=@Todd' + ) + ); + + $headers = array + ( + "HTTP_Authorization" => " Bearer " . $this->access_token, + "CONTENT_TYPE" => "application/json" + ); + + + $response = $this->action + ( + "GET", + "OAuth2SummitLocationsApiController@getLocationEvents", + $params, + array(), + array(), + array(), + $headers + ); + + $content = $response->getContent(); + $this->assertResponseStatus(200); + + $events = json_decode($content); + $this->assertTrue(!is_null($events)); + } + + public function testCurrentSummitPublishedLocationEventsWithFilter() + { + $params = array + ( + 'id' => 23, + 'location_id' => 311, + 'filter' => [ + + 'start_date>=1451479955' + ] + ); + + $headers = array + ( + "HTTP_Authorization" => " Bearer " . $this->access_token, + "CONTENT_TYPE" => "application/json" + ); + + $response = $this->action + ( + "GET", + "OAuth2SummitLocationsApiController@getLocationPublishedEvents", + $params, + array(), + array(), + array(), + $headers + ); + + $content = $response->getContent(); + $this->assertResponseStatus(200); + + $events = json_decode($content); + $this->assertTrue(!is_null($events)); + } + + public function testCurrentSummitPublishedLocationTBAEvents() + { + $params = array + ( + 'id' => 23, + 'location_id' => "tba", + ); + + $headers = array + ( + "HTTP_Authorization" => " Bearer " . $this->access_token, + "CONTENT_TYPE" => "application/json" + ); + + $response = $this->action + ( + "GET", + "OAuth2SummitLocationsApiController@getLocationPublishedEvents", + $params, + array(), + array(), + array(), + $headers + ); + + $content = $response->getContent(); + $this->assertResponseStatus(200); + + $events = json_decode($content); + $this->assertTrue(!is_null($events)); + } +} \ No newline at end of file