From 47c3b9de7ee73bbd850cebbf6dbfd8ed45b2d56b Mon Sep 17 00:00:00 2001 From: Sebastian Marcet Date: Thu, 15 Feb 2018 14:09:14 -0300 Subject: [PATCH] Added endpoint get events CSV GET /api/v1/summits/{id}/events/csv Change-Id: I42aa61adbc7d9fe6a0fbee0bb16880a66519af03 --- .../OAuth2SummitEventsApiController.php | 49 +++++++++++++++++++ ...ieveAllSummitEventsBySummitCSVStrategy.php | 24 +++++++++ ...etrieveAllSummitEventsBySummitStrategy.php | 2 +- .../events/RetrieveSummitEventsStrategy.php | 23 +++++---- app/Http/routes.php | 3 +- database/seeds/ApiEndpointsSeeder.php | 9 ++++ tests/OAuth2SummitEventsApiTest.php | 37 ++++++++++++++ 7 files changed, 136 insertions(+), 11 deletions(-) create mode 100644 app/Http/Controllers/Apis/Protected/Summit/Strategies/events/RetrieveAllSummitEventsBySummitCSVStrategy.php diff --git a/app/Http/Controllers/Apis/Protected/Summit/OAuth2SummitEventsApiController.php b/app/Http/Controllers/Apis/Protected/Summit/OAuth2SummitEventsApiController.php index 99694786..640de203 100644 --- a/app/Http/Controllers/Apis/Protected/Summit/OAuth2SummitEventsApiController.php +++ b/app/Http/Controllers/Apis/Protected/Summit/OAuth2SummitEventsApiController.php @@ -12,6 +12,8 @@ * limitations under the License. **/ +use App\Http\Utils\BooleanCellFormatter; +use App\Http\Utils\EpochCellFormatter; use Exception; use Illuminate\Http\Request as LaravelRequest; use Illuminate\Support\Facades\Input; @@ -111,6 +113,53 @@ final class OAuth2SummitEventsApiController extends OAuth2ProtectedController } } + /** + * @param $summit_id + * @return mixed + */ + public function getEventsCSV($summit_id) + { + try + { + $strategy = new RetrieveAllSummitEventsBySummitCSVStrategy($this->repository, $this->event_repository, $this->resource_server_context); + $response = $strategy->getEvents(['summit_id' => $summit_id]); + + $filename = "events-" . date('Ymd'); + $list = $response->toArray(null, [], ['none']); + + return $this->export + ( + 'csv', + $filename, + $list['data'], + [ + 'created' => new EpochCellFormatter(), + 'last_edited' => new EpochCellFormatter(), + 'start_date' => new EpochCellFormatter(), + 'end_date' => new EpochCellFormatter(), + 'allow_feedback' => new BooleanCellFormatter(), + 'is_published' => new BooleanCellFormatter(), + 'rsvp_external' => new BooleanCellFormatter(), + ] + ); + } + 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 diff --git a/app/Http/Controllers/Apis/Protected/Summit/Strategies/events/RetrieveAllSummitEventsBySummitCSVStrategy.php b/app/Http/Controllers/Apis/Protected/Summit/Strategies/events/RetrieveAllSummitEventsBySummitCSVStrategy.php new file mode 100644 index 00000000..163a8570 --- /dev/null +++ b/app/Http/Controllers/Apis/Protected/Summit/Strategies/events/RetrieveAllSummitEventsBySummitCSVStrategy.php @@ -0,0 +1,24 @@ +summit = SummitFinderStrategyFactory::build($this->summit_repository, $this->resource_server_context)->find($summit_id); diff --git a/app/Http/Controllers/Apis/Protected/Summit/Strategies/events/RetrieveSummitEventsStrategy.php b/app/Http/Controllers/Apis/Protected/Summit/Strategies/events/RetrieveSummitEventsStrategy.php index b9a6bff1..78c852c6 100644 --- a/app/Http/Controllers/Apis/Protected/Summit/Strategies/events/RetrieveSummitEventsStrategy.php +++ b/app/Http/Controllers/Apis/Protected/Summit/Strategies/events/RetrieveSummitEventsStrategy.php @@ -20,13 +20,25 @@ use Illuminate\Support\Facades\Validator; use Illuminate\Support\Facades\Input; use utils\FilterParser; use utils\PagingInfo; - /** * Class RetrieveSummitEventsStrategy * @package App\Http\Controllers */ abstract class RetrieveSummitEventsStrategy { + + protected function getPageParams(){ + // default values + $page = 1; + $per_page = 5; + + if (Input::has('page')) { + $page = intval(Input::get('page')); + $per_page = intval(Input::get('per_page')); + } + return [$page, $per_page]; + } + /** * @param array $params * @return PagingResponse @@ -48,15 +60,8 @@ abstract class RetrieveSummitEventsStrategy 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')); - } + list($page, $per_page) = $this->getPageParams(); return $this->retrieveEventsFromSource ( diff --git a/app/Http/routes.php b/app/Http/routes.php index 02260eed..01eee0c4 100644 --- a/app/Http/routes.php +++ b/app/Http/routes.php @@ -230,7 +230,8 @@ Route::group([ // events Route::group(array('prefix' => 'events'), function () { - Route::get('', 'OAuth2SummitEventsApiController@getEvents'); + Route::get('',[ 'middleware' => 'auth.user:administrators|summit-front-end-administrators', 'uses' => 'OAuth2SummitEventsApiController@getEvents']); + Route::get('csv',[ 'middleware' => 'auth.user:administrators|summit-front-end-administrators', 'uses' => 'OAuth2SummitEventsApiController@getEventsCSV']); // bulk actions Route::delete('/publish', [ 'middleware' => 'auth.user:administrators|summit-front-end-administrators', 'uses' => 'OAuth2SummitEventsApiController@unPublishEvents']); Route::put('/publish', [ 'middleware' => 'auth.user:administrators|summit-front-end-administrators', 'uses' => 'OAuth2SummitEventsApiController@updateAndPublishEvents']); diff --git a/database/seeds/ApiEndpointsSeeder.php b/database/seeds/ApiEndpointsSeeder.php index b44c743a..ab40f420 100644 --- a/database/seeds/ApiEndpointsSeeder.php +++ b/database/seeds/ApiEndpointsSeeder.php @@ -304,6 +304,15 @@ class ApiEndpointsSeeder extends Seeder sprintf(SummitScopes::ReadAllSummitData, $current_realm) ], ), + array( + 'name' => 'get-events-csv', + 'route' => '/api/v1/summits/{id}/events/csv', + 'http_method' => 'GET', + 'scopes' => [ + sprintf(SummitScopes::ReadSummitData, $current_realm), + sprintf(SummitScopes::ReadAllSummitData, $current_realm) + ], + ), array( 'name' => 'get-published-events', 'route' => '/api/v1/summits/{id}/events/published', diff --git a/tests/OAuth2SummitEventsApiTest.php b/tests/OAuth2SummitEventsApiTest.php index 3d40c0a4..1551d465 100644 --- a/tests/OAuth2SummitEventsApiTest.php +++ b/tests/OAuth2SummitEventsApiTest.php @@ -329,6 +329,43 @@ class OAuth2SummitEventsApiTest extends ProtectedApiTest $this->assertTrue(!is_null($events)); } + public function testCurrentSummitEventsWithFilterCSV() + { + $params = array + ( + 'id' => 6, + 'expand' => 'feedback', + 'filter' => array + ( + 'tags=@design', + 'start_date>1445895000' + ) + ); + + $headers = array + ( + "HTTP_Authorization" => " Bearer " . $this->access_token, + "CONTENT_TYPE" => "application/json" + ); + + $response = $this->action + ( + "GET", + "OAuth2SummitEventsApiController@getEventsCSV", + $params, + array(), + array(), + array(), + $headers + ); + + $csv = $response->getContent(); + $this->assertResponseStatus(200); + + + $this->assertTrue(!empty($csv)); + } + public function testCurrentSelectionMotiveSummitEvents() { $params = array