Added endpoint get events CSV

GET /api/v1/summits/{id}/events/csv

Change-Id: I42aa61adbc7d9fe6a0fbee0bb16880a66519af03
This commit is contained in:
Sebastian Marcet 2018-02-15 14:09:14 -03:00
parent 85e4529a4b
commit 47c3b9de7e
7 changed files with 136 additions and 11 deletions

View File

@ -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

View File

@ -0,0 +1,24 @@
<?php namespace App\Http\Controllers;
/**
* Copyright 2018 OpenStack Foundation
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
/**
* Class RetrieveAllSummitEventsBySummitCSVStrategy
* @package App\Http\Controllers
*/
final class RetrieveAllSummitEventsBySummitCSVStrategy
extends RetrieveAllSummitEventsBySummitStrategy
{
protected function getPageParams(){
return [1, PHP_INT_MAX];
}
}

View File

@ -75,7 +75,7 @@ class RetrieveAllSummitEventsBySummitStrategy extends RetrieveSummitEventsStrate
* @throws EntityNotFoundException
* @throws ValidationException
*/
public function getEvents(array $params = array())
public function getEvents(array $params = [])
{
$summit_id = isset($params['summit_id'])? $params['summit_id']:0;
$this->summit = SummitFinderStrategyFactory::build($this->summit_repository, $this->resource_server_context)->find($summit_id);

View File

@ -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
(

View File

@ -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']);

View File

@ -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',

View File

@ -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