Added new endpoint

Events per location

Change-Id: I1d2057f72a016a7dbd768fc696d5a4c3ad09645e
This commit is contained in:
Sebastian Marcet 2016-04-27 15:30:58 -03:00
parent 800184deda
commit 9775dd5e31
6 changed files with 338 additions and 8 deletions

View File

@ -18,6 +18,7 @@ use Illuminate\Support\Facades\Request;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Log;
use utils\PagingResponse;
/**
* Copyright 2015 OpenStack Foundation
@ -1323,6 +1324,130 @@ class OAuth2SummitApiController extends OAuth2ProtectedController
}
}
/**
* @param string $summit_id
* @param int $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)->find($summit_id);
if (is_null($summit))
throw new EntityNotFoundException;
$location = $summit->getLocation($location_id);
if (is_null($location))
throw new EntityNotFoundException;
$values = Input::all();
$rules = array
(
'page' => 'integer|min:1',
'per_page' => 'required_with:page|integer|min:5|max:100',
);
$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'), array
(
'title' => array('=@', '=='),
'speaker' => array('=@', '=='),
'tags' => array('=@', '=='),
'start_date' => array('>', '<', '<=', '>=', '=='),
'end_date' => array('>', '<', '<=', '>=', '=='),
'summit_type_id' => array('=='),
'event_type_id' => array('=='),
'track_id' => array('=='),
));
}
list($total, $per_page, $current_page, $last_page, $events) = $location->events
(
$page, $per_page, $filter , $published
);
return new PagingResponse
(
$total,
$per_page,
$current_page,
$last_page,
$events
);
}
/**
* @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());
}
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
* @param $location_id
* @return mixed
*/
public function getLocationPublishedEvents($summit_id, $location_id)
{
try {
return $this->ok($this->_getLocationEvents($summit_id, $location_id, true)->toArray());
}
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

@ -109,8 +109,9 @@ Route::group(array(
Route::get('', 'OAuth2SummitApiController@getLocations');
Route::group(array('prefix' => '{location_id}'), function () {
Route::get('', 'OAuth2SummitApiController@getLocation');
Route::get('/events/published','OAuth2SummitApiController@getLocationEvents');
Route::get('/events','OAuth2SummitApiController@getLocationPublishedEvents');
});
});

View File

@ -265,7 +265,6 @@ class Summit extends SilverstripeBaseModel
return array($total,$per_page, $current_page, $last_page, $events);
}
/**
* @param int $member_id
* @return SummitAttendee

View File

@ -1,4 +1,5 @@
<?php
<?php namespace models\summit;
/**
* Copyright 2015 OpenStack Foundation
* Licensed under the Apache License, Version 2.0 (the "License");
@ -12,10 +13,12 @@
* limitations under the License.
**/
namespace models\summit;
use models\utils\IEntity;
use models\utils\SilverstripeBaseModel;
use utils\Filter;
use utils\ExistsFilterManyManyMapping;
use utils\ExistsFilterManyToOneMapping;
/**
* Class SummitAbstractLocation
@ -23,11 +26,11 @@ use models\utils\SilverstripeBaseModel;
*/
class SummitAbstractLocation extends SilverstripeBaseModel implements IEntity
{
protected $table = 'SummitAbstractLocation';
protected $table = 'SummitAbstractLocation';
protected $stiBaseClass = 'models\summit\SummitAbstractLocation';
protected $stiBaseClass = 'models\summit\SummitAbstractLocation';
protected $mtiClassType = 'concrete';
protected $mtiClassType = 'concrete';
protected $array_mappings = array
(
@ -46,4 +49,104 @@ class SummitAbstractLocation extends SilverstripeBaseModel implements IEntity
return (int)$this->ID;
}
/**
* @param int $page
* @param int $per_page
* @param Filter|null $filter
* @param bool|false $published
* @return array
*/
public function events($page = 1, $per_page = 100, Filter $filter = null, $published = false)
{
$rel = $this
->hasMany('models\summit\SummitEvent', 'LocationID', 'ID')
->select
(
array
(
'SummitEvent.*',
'Presentation.Priority',
'Presentation.Level',
'Presentation.Status',
'Presentation.OtherTopic',
'Presentation.Progress',
'Presentation.Slug',
'Presentation.CreatorID',
'Presentation.CategoryID',
'Presentation.Views',
'Presentation.ModeratorID',
'Presentation.ProblemAddressed',
'Presentation.AttendeesExpectedLearnt',
'Presentation.SelectionMotive',
)
);
$rel = $rel->leftJoin('Presentation', 'SummitEvent.ID', '=', 'Presentation.ID');
if($published)
{
$rel = $rel->where('Published','=','1');
}
if(!is_null($filter))
{
$filter->apply2Relation($rel, array
(
'title' => 'SummitEvent.Title',
'start_date' => 'SummitEvent.StartDate:datetime_epoch',
'end_date' => 'SummitEvent.EndDate:datetime_epoch',
'tags' => new ExistsFilterManyManyMapping
(
'Tag',
'SummitEvent_Tags',
'SummitEvent_Tags.TagID = Tag.ID',
"SummitEvent_Tags.SummitEventID = SummitEvent.ID AND Tag.Tag :operator ':value'"
),
'summit_type_id' => new ExistsFilterManyManyMapping
(
'SummitType',
'SummitEvent_AllowedSummitTypes',
'SummitType.ID = SummitEvent_AllowedSummitTypes.SummitTypeID',
'SummitEvent_AllowedSummitTypes.SummitEventID = SummitEvent.ID AND SummitType.ID :operator :value'
),
'event_type_id' => new ExistsFilterManyToOneMapping
(
'SummitEventType',
'SummitEventType.ID = SummitEvent.TypeID AND SummitEventType.ID :operator :value'
),
'track_id' => new ExistsFilterManyToOneMapping
(
'PresentationCategory',
'PresentationCategory.ID = Presentation.CategoryID AND PresentationCategory.ID :operator :value'
),
'speaker' => new ExistsFilterManyManyMapping
(
'PresentationSpeaker',
'Presentation_Speakers',
'Presentation_Speakers.PresentationSpeakerID = PresentationSpeaker.ID',
"Presentation_Speakers.PresentationID = SummitEvent.ID AND CONCAT(FirstName, ' ' , LastName) :operator ':value'"
),
));
}
$rel = $rel->orderBy('StartDate','asc')->orderBy('EndDate','asc');
$pagination_result = $rel->paginate($per_page);
$total = $pagination_result->total();
$items = $pagination_result->items();
$per_page = $pagination_result->perPage();
$current_page = $pagination_result->currentPage();
$last_page = $pagination_result->lastPage();
$events = array();
foreach($items as $e)
{
if($e->ClassName === 'Presentation')
$e = Presentation::toPresentation($e);
array_push($events, $e);
}
return array($total,$per_page, $current_page, $last_page, $events);
}
}

View File

@ -472,6 +472,26 @@ class ApiEndpointsSeeder extends Seeder
)
);
ApiEndpoint::create(
array(
'name' => 'get-location-events',
'active' => true,
'api_id' => $summit->id,
'route' => '/api/v1/summits/{id}/locations/{location_id}/events',
'http_method' => 'GET'
)
);
ApiEndpoint::create(
array(
'name' => 'get-location-published-events',
'active' => true,
'api_id' => $summit->id,
'route' => '/api/v1/summits/{id}/locations/{location_id}/events/published',
'http_method' => 'GET'
)
);
// event types
ApiEndpoint::create(
@ -567,6 +587,12 @@ class ApiEndpointsSeeder extends Seeder
$endpoint->scopes()->attach($summit_read_scope->id);
$endpoint = ApiEndpoint::where('name', '=', 'get-summit-types')->first();
$endpoint->scopes()->attach($summit_read_scope->id);
$endpoint = ApiEndpoint::where('name', '=', 'get-location-published-events')->first();
$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();
$endpoint->scopes()->attach($summit_external_order_read->id);

View File

@ -82,6 +82,7 @@ class OAuth2SummitApiTest extends ProtectedApiTest
$content = $response->getContent();
$summit = json_decode($content);
$this->assertTrue(!is_null($summit));
$this->assertTrue(count($summit->schedule) > 0);
$this->assertResponseStatus(200);
}
@ -1476,4 +1477,79 @@ class OAuth2SummitApiTest extends ProtectedApiTest
$attendee = json_decode($content);
$this->assertTrue(!is_null($attendee));
}
public function testCurrentSummitLocationEventsWithFilter()
{
$params = array
(
'id' => 'current',
'location_id' => 25,
'filter' => array
(
'tags=@design',
'start_date>1445895000'
)
);
$headers = array
(
"HTTP_Authorization" => " Bearer " .$this->access_token,
"CONTENT_TYPE" => "application/json"
);
$response = $this->action
(
"GET",
"OAuth2SummitApiController@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' => 'current',
'location_id' => 68,
/*'filter' => array
(
'speaker=@Alex',
)*/
);
$headers = array
(
"HTTP_Authorization" => " Bearer " .$this->access_token,
"CONTENT_TYPE" => "application/json"
);
$response = $this->action
(
"GET",
"OAuth2SummitApiController@getLocationPublishedEvents",
$params,
array(),
array(),
array(),
$headers
);
$content = $response->getContent();
$this->assertResponseStatus(200);
$events = json_decode($content);
$this->assertTrue(!is_null($events));
}
}