From 800184deda53d7e84437157249530b7c223ff92e Mon Sep 17 00:00:00 2001 From: Sebastian Marcet Date: Tue, 19 Apr 2016 11:24:42 -0300 Subject: [PATCH] Updaate get summit event endpoints added new query string params * fields this is a list of the desired fields that you want ( string comma delimited) * relaations this is a list of the desired relations that you want ( string comma delimited) if none is given, then its return everything al usual Change-Id: I5415e72379de794c695b2191c9e348574fef9c15 --- .../summit/OAuth2SummitApiController.php | 172 +++++++++--------- app/Models/summit/Presentation.php | 74 ++++++-- app/Models/summit/SummitEvent.php | 63 ++++++- app/Providers/AppServiceProvider.php | 2 +- tests/OAuth2SummitApiTest.php | 106 +++++++++++ 5 files changed, 302 insertions(+), 115 deletions(-) diff --git a/app/Http/Controllers/apis/protected/summit/OAuth2SummitApiController.php b/app/Http/Controllers/apis/protected/summit/OAuth2SummitApiController.php index 4193c317..dfe38b5d 100644 --- a/app/Http/Controllers/apis/protected/summit/OAuth2SummitApiController.php +++ b/app/Http/Controllers/apis/protected/summit/OAuth2SummitApiController.php @@ -14,10 +14,10 @@ use services\model\ISummitService; use utils\OrderParser; use utils\PagingInfo; use utils\FilterParser; -use Request; -use Validator; -use Input; -use Log; +use Illuminate\Support\Facades\Request; +use Illuminate\Support\Facades\Validator; +use Illuminate\Support\Facades\Input; +use Illuminate\Support\Facades\Log; /** * Copyright 2015 OpenStack Foundation @@ -647,6 +647,63 @@ class OAuth2SummitApiController extends OAuth2ProtectedController } } + /** + * @param $summit_id + * @param $event_id + * @param string $expand + * @param string $fields + * @param string $relations + * @param bool $published + * @return array + * @throws EntityNotFoundException + */ + private function _getSummitEvent($summit_id, $event_id, $expand = '', $fields = '', $relations = '', $published = true) + { + $summit = SummitFinderStrategyFactory::build($this->repository)->find($summit_id); + if (is_null($summit)) throw new EntityNotFoundException; + + $event = $published ? $summit->getScheduleEvent(intval($event_id)) : $summit->getEvent(intval($event_id)); + + if (is_null($event)) throw new EntityNotFoundException; + $relations = !empty($relations) ? explode(',', $relations) : array(); + $fields = !empty($fields) ? explode(',', $fields) : array(); + $data = $event->toArray($fields, $relations); + + if (!empty($expand)) { + foreach (explode(',', $expand) as $relation) { + switch (trim($relation)) { + case 'feedback': { + $feedback = array(); + list($total, $per_page, $current_page, $last_page, $items) = $event->feedback(1, PHP_INT_MAX); + foreach ($items as $f) { + array_push($feedback, $f->toArray()); + } + $data['feedback'] = $feedback; + } + break; + case 'speakers':{ + if($event instanceof Presentation){ + unset($data['speakers']); + $speakers = array(); + foreach($event->speakers() as $speaker) + { + array_push($speakers, $speaker->toArray()); + } + $data['speakers'] = $speakers; + } + } + break; + case 'location': { + $location = $event->getLocation(); + $data['location'] = $location->toArray(); + unset($data['location_id']); + } + break; + } + } + } + return $data; + } /** * @param $summit_id * @param $event_id @@ -656,54 +713,18 @@ class OAuth2SummitApiController extends OAuth2ProtectedController { try { - $summit = SummitFinderStrategyFactory::build($this->repository)->find($summit_id); - if (is_null($summit)) return $this->error404(); + $expand = Request::input('expand', ''); + $fields = Request::input('fields', ''); + $relations = Request::input('relations', ''); - $expand = Request::input('expand', ''); - - $event = $summit->getEvent(intval($event_id)); - - if (is_null($event)) { - return $this->error404(); - } - - $data = $event->toArray(); - - if (!empty($expand)) { - foreach (explode(',', $expand) as $relation) { - switch (trim($relation)) { - case 'feedback': { - $feedback = array(); - list($total, $per_page, $current_page, $last_page, $items) = $event->feedback(1, - PHP_INT_MAX); - foreach ($items as $f) { - array_push($feedback, $f->toArray()); - } - $data['feedback'] = $feedback; - } - break; - case 'speakers':{ - if($event instanceof Presentation){ - unset($data['speakers']); - $speakers = array(); - foreach($event->speakers() as $speaker) - { - array_push($speakers, $speaker->toArray()); - } - $data['speakers'] = $speakers; - } - } - break; - case 'location': { - $location = $event->getLocation(); - $data['location'] = $location->toArray(); - unset($data['location_id']); - } - } - } - } + $data = $this->_getSummitEvent($summit_id, $event_id, $expand, $fields, $relations, false); return $this->ok($data); - } catch (Exception $ex) { + } + catch (EntityNotFoundException $ex1) { + Log::warning($ex1); + return $this->error404(); + } + catch (Exception $ex) { Log::error($ex); return $this->error500($ex); } @@ -718,49 +739,18 @@ class OAuth2SummitApiController extends OAuth2ProtectedController { try { - $summit = SummitFinderStrategyFactory::build($this->repository)->find($summit_id); - if (is_null($summit)) return $this->error404(); + $expand = Request::input('expand', ''); + $fields = Request::input('fields', ''); + $relations = Request::input('relations', ''); - $expand = Request::input('expand', ''); - - $event = $summit->getScheduleEvent(intval($event_id)); - - if (is_null($event)) { - return $this->error404(); - } - - $data = $event->toArray(); - - if (!empty($expand)) { - foreach (explode(',', $expand) as $relation) { - switch (trim($relation)) { - case 'feedback': { - $feedback = array(); - list($total, $per_page, $current_page, $last_page, $items) = $event->feedback(1, - PHP_INT_MAX); - foreach ($items as $f) { - array_push($feedback, $f->toArray()); - } - $data['feedback'] = $feedback; - } - break; - case 'speakers':{ - if($event instanceof Presentation){ - unset($data['speakers']); - $speakers = array(); - foreach($event->speakers() as $speaker) - { - array_push($speakers, $speaker->toArray()); - } - $data['speakers'] = $speakers; - } - } - break; - } - } - } + $data = $this->_getSummitEvent($summit_id, $event_id, $expand, $fields, $relations, true); return $this->ok($data); - } catch (Exception $ex) { + } + catch (EntityNotFoundException $ex1) { + Log::warning($ex1); + return $this->error404(); + } + catch (Exception $ex) { Log::error($ex); return $this->error500($ex); } diff --git a/app/Models/summit/Presentation.php b/app/Models/summit/Presentation.php index 476cd136..179d32e0 100644 --- a/app/Models/summit/Presentation.php +++ b/app/Models/summit/Presentation.php @@ -53,6 +53,38 @@ class Presentation extends SummitEvent 'RSVPLink' => 'rsvp_link:json_string', ); + public static $allowed_fields = array + ( + 'id', + 'title', + 'description', + 'start_date', + 'end_date', + 'location_id', + 'summit_id', + 'type_id', + 'class_name', + 'track_id', + 'moderator_speaker_id', + 'level', + 'allow_feedback', + 'avg_feedback_rate', + 'is_published', + 'head_count', + 'rsvp_link', + ); + + public static $allowed_relations = array + ( + 'summit_types', + 'sponsors', + 'tags', + 'slides', + 'videos', + 'speakers', + ); + + /** * @param array $fields * @return PresentationSpeaker[] @@ -80,27 +112,39 @@ class Presentation extends SummitEvent } /** + * @param array $fields + * @param array $relations * @return array */ - public function toArray() + public function toArray(array $fields = array(), array $relations = array()) { - $values = parent::toArray(); - if(!$this->from_speaker) - $values['speakers'] = $this->getSpeakerIds(); + if(!count($fields)) $fields = self::$allowed_fields; + if(!count($relations)) $relations = self::$allowed_relations; - $slides = array(); - foreach($this->slides() as $s) - { - array_push($slides, $s->toArray()); - } - $values['slides'] = $slides; + $values = parent::toArray($fields, $relations); - $videos = array(); - foreach($this->videos() as $v) - { - array_push($videos, $v->toArray()); + if(in_array('speakers', $relations)) { + if (!$this->from_speaker) + $values['speakers'] = $this->getSpeakerIds(); + } + + if(in_array('slides', $relations)) + { + $slides = array(); + foreach ($this->slides() as $s) { + array_push($slides, $s->toArray()); + } + $values['slides'] = $slides; + } + + if(in_array('videos', $relations)) + { + $videos = array(); + foreach ($this->videos() as $v) { + array_push($videos, $v->toArray()); + } + $values['videos'] = $videos; } - $values['videos'] = $videos; return $values; } diff --git a/app/Models/summit/SummitEvent.php b/app/Models/summit/SummitEvent.php index d03486e9..49192209 100644 --- a/app/Models/summit/SummitEvent.php +++ b/app/Models/summit/SummitEvent.php @@ -55,6 +55,31 @@ class SummitEvent extends SilverstripeBaseModel 'RSVPLink' => 'rsvp_link:json_string', ); + public static $allowed_fields = array + ( + 'id', + 'title', + 'description', + 'start_date', + 'end_date', + 'location_id', + 'summit_id', + 'type_id', + 'class_name', + 'allow_feedback', + 'avg_feedback_rate', + 'is_published', + 'head_count', + 'rsvp_link', + ); + + public static $allowed_relations = array + ( + 'summit_types', + 'sponsors', + 'tags', + ); + /** * @param string $title * @return $this @@ -297,8 +322,16 @@ class SummitEvent extends SilverstripeBaseModel ->get(); } - public function toArray() + /** + * @param array $fields + * @param array $relations + * @return array + */ + public function toArray(array $fields = array(), array $relations = array()) { + if(!count($fields)) $fields = self::$allowed_fields; + if(!count($relations)) $relations = self::$allowed_relations; + $values = parent::toArray(); //check if description is empty, if so, set short description $description = $values['description']; @@ -307,14 +340,28 @@ class SummitEvent extends SilverstripeBaseModel $values['description'] = JsonUtils::toJsonString($this->ShortDescription); } - $values['summit_types'] = $this->getSummitTypesIds(); - $values['sponsors'] = $this->getSponsorsIds(); - $tags = array(); - foreach($this->tags() as $t) - { - array_push($tags, $t->toArray()); + //check requested fields + + foreach($values as $field => $value){ + if(in_array($field, $fields)) continue; + unset($values[$field]); } - $values['tags'] = $tags; + + if(in_array('summit_types', $relations)) + $values['summit_types'] = $this->getSummitTypesIds(); + + if(in_array('sponsors', $relations)) + $values['sponsors'] = $this->getSponsorsIds(); + + if(in_array('tags', $relations)) + { + $tags = array(); + foreach ($this->tags() as $t) { + array_push($tags, $t->toArray()); + } + $values['tags'] = $tags; + } + return $values; } diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index 939a2e03..177c38e6 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -74,7 +74,7 @@ class AppServiceProvider extends ServiceProvider Validator::extend('string_array', function($attribute, $value, $parameters, $validator) { $validator->addReplacer('string_array', function($message, $attribute, $rule, $parameters) use ($validator) { - return sprintf("%s should be an array of integers", $attribute); + return sprintf("%s should be an array of strings", $attribute); }); if(!is_array($value)) return false; foreach($value as $element) diff --git a/tests/OAuth2SummitApiTest.php b/tests/OAuth2SummitApiTest.php index 7f7a249b..052b55f9 100644 --- a/tests/OAuth2SummitApiTest.php +++ b/tests/OAuth2SummitApiTest.php @@ -730,6 +730,112 @@ class OAuth2SummitApiTest extends ProtectedApiTest $this->assertTrue(!is_null($events)); } + + public function testGetPublishedEventFields(){ + + + $params = array + ( + 'id' => 'current', + 'event_id' => 8900, + 'fields' => 'id,avg_feedback_rate,head_count', + 'relations' => 'none' + ); + + $headers = array + ( + "HTTP_Authorization" => " Bearer " .$this->access_token, + "CONTENT_TYPE" => "application/json" + ); + + + $response = $this->action + ( + "GET", + "OAuth2SummitApiController@getScheduledEvent", + $params, + array(), + array(), + array(), + $headers + ); + + $content = $response->getContent(); + $this->assertResponseStatus(200); + + $events = json_decode($content); + $this->assertTrue(!is_null($events)); + } + + public function testGetPublishedEventFieldsNotExists(){ + + + $params = array + ( + 'id' => 'current', + 'event_id' => 8900, + 'fields' => 'id_test', + 'relations' => 'none' + ); + + $headers = array + ( + "HTTP_Authorization" => " Bearer " .$this->access_token, + "CONTENT_TYPE" => "application/json" + ); + + + $response = $this->action + ( + "GET", + "OAuth2SummitApiController@getScheduledEvent", + $params, + array(), + array(), + array(), + $headers + ); + + $content = $response->getContent(); + $this->assertResponseStatus(200); + + $events = json_decode($content); + $this->assertTrue(!is_null($events)); + } + + public function testGetPublishedEvent(){ + + $params = array + ( + 'id' => 'current', + 'event_id' => 8900, + ); + + $headers = array + ( + "HTTP_Authorization" => " Bearer " .$this->access_token, + "CONTENT_TYPE" => "application/json" + ); + + + $response = $this->action + ( + "GET", + "OAuth2SummitApiController@getScheduledEvent", + $params, + array(), + array(), + array(), + $headers + ); + + $content = $response->getContent(); + $this->assertResponseStatus(200); + + $events = json_decode($content); + $this->assertTrue(!is_null($events)); + } + public function testPostEvent($start_date = 1461613958, $end_date = 1461613990 ) { $params = array