Updated endpoint

/api/v1/summits/{id}/event-types

- paging params
** page
** per_page

- filters
** name (=@, ==)
** class_name (==)
** black_out_times (==)
** use_sponsors (==)
** are_sponsors_mandatory (==)
** allows_attachment (==)
** use_speakers (==)
** are_speakers_mandatory (==)
** use_moderator (==)
** is_moderator_mandatory (==)
** should_be_available_on_cfp (==)
- order
** id
** name

Change-Id: Ia516718f6811e9dc1006d78883b675ca97276074
This commit is contained in:
Sebastian Marcet 2018-02-19 11:05:14 -03:00
parent 1c6acf5e0f
commit a20d18d965
10 changed files with 474 additions and 35 deletions

View File

@ -218,39 +218,6 @@ final class OAuth2SummitApiController extends OAuth2ProtectedController
}
}
/**
* @param $summit_id
* @return mixed
*/
public function getEventTypes($summit_id)
{
try {
$summit = SummitFinderStrategyFactory::build($this->repository, $this->resource_server_context)->find($summit_id);
if (is_null($summit)) return $this->error404();
//event types
$event_types = array();
foreach ($summit->getEventTypes() as $event_type)
{
$event_types[] = SerializerRegistry::getInstance()->getSerializer($event_type)->serialize();
}
$response = new PagingResponse
(
count($event_types),
count($event_types),
1,
1,
$event_types
);
return $this->ok($response->toArray());
} catch (Exception $ex) {
Log::error($ex);
return $this->error500($ex);
}
}
/**
* @param $summit_id
* @return mixed

View File

@ -0,0 +1,176 @@
<?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.
**/
use Illuminate\Support\Facades\Request;
use App\Models\Foundation\Summit\Events\SummitEventTypeConstants;
use App\Models\Foundation\Summit\Repositories\ISummitEventTypeRepository;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Validator;
use models\exceptions\ValidationException;
use models\oauth2\IResourceServerContext;
use models\summit\ISummitRepository;
use models\exceptions\EntityNotFoundException;
use utils\Filter;
use utils\FilterElement;
use utils\FilterParser;
use utils\OrderParser;
use utils\PagingInfo;
use Exception;
/**
* Class OAuth2SummitsEventTypesApiController
* @package App\Http\Controllers
*/
final class OAuth2SummitsEventTypesApiController extends OAuth2ProtectedController
{
/**
* @var ISummitRepository
*/
private $summit_repository;
public function __construct
(
ISummitEventTypeRepository $repository,
ISummitRepository $summit_repository,
IResourceServerContext $resource_server_context
)
{
parent::__construct($resource_server_context);
$this->repository = $repository;
$this->summit_repository = $summit_repository;
}
/**
* @param $filter_element
* @return bool
*/
private function validateClassName($filter_element){
if($filter_element instanceof FilterElement){
return in_array($filter_element->getValue(), SummitEventTypeConstants::$valid_class_names);
}
$valid = true;
foreach($filter_element[0] as $elem){
$valid = $valid && in_array($elem->getValue(), SummitEventTypeConstants::$valid_class_names);
}
return $valid;
}
/**
* @param $summit_id
* @return mixed
*/
public function getAllBySummit($summit_id){
$values = Input::all();
$rules = [
'page' => 'integer|min:1',
'per_page' => 'required_with:page|integer|min:5|max:100',
];
try {
$summit = SummitFinderStrategyFactory::build($this->summit_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 = 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'), [
'name' => ['=@', '=='],
'class_name' => ['=='],
'black_out_times' => ['=='],
'use_sponsors' => ['=='],
'are_sponsors_mandatory' => ['=='],
'allows_attachment' => ['=='],
'use_speakers' => ['=='],
'are_speakers_mandatory' => ['=='],
'use_moderator' => ['=='],
'is_moderator_mandatory' => ['=='],
'should_be_available_on_cfp' => ['=='],
]);
}
$order = null;
if (Input::has('order'))
{
$order = OrderParser::parse(Input::get('order'), [
'id',
'name',
]);
}
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(", ", SummitEventTypeConstants::$valid_class_names)
)
);
}
$data = $this->repository->getBySummit($summit, new PagingInfo($page, $per_page), $filter, $order);
return $this->ok
(
$data->toArray
(
Request::input('expand', ''),
[],
[],
[]
)
);
}
catch (ValidationException $ex1)
{
Log::warning($ex1);
return $this->error412(array( $ex1->getMessage()));
}
catch (EntityNotFoundException $ex2)
{
Log::warning($ex2);
return $this->error404(array('message' => $ex2->getMessage()));
}
catch(\HTTP401UnauthorizedException $ex3)
{
Log::warning($ex3);
return $this->error401();
}
catch (Exception $ex) {
Log::error($ex);
return $this->error500($ex);
}
}
}

View File

@ -294,7 +294,7 @@ Route::group([
// event types
Route::group(array('prefix' => 'event-types'), function () {
Route::get('', 'OAuth2SummitApiController@getEventTypes');
Route::get('', 'OAuth2SummitsEventTypesApiController@getAllBySummit');
});
// external orders

View File

@ -199,4 +199,6 @@ SQL;
public function getClassName(){
return 'PresentationType';
}
const ClassName = 'PRESENTATION_TYPE';
}

View File

@ -15,7 +15,7 @@ use models\utils\SilverstripeBaseModel;
use Doctrine\ORM\Mapping AS ORM;
/**
* @ORM\Entity
* @ORM\Entity(repositoryClass="App\Repositories\Summit\DoctrineSummitEventTypeRepository")
* @ORM\Table(name="SummitEventType")
* @ORM\InheritanceType("JOINED")
* @ORM\DiscriminatorColumn(name="ClassName", type="string")
@ -162,4 +162,6 @@ class SummitEventType extends SilverstripeBaseModel
return 'SummitEventType';
}
const ClassName = 'EVENT_TYPE';
}

View File

@ -0,0 +1,27 @@
<?php namespace App\Models\Foundation\Summit\Events;
/**
* 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.
**/
use models\summit\PresentationType;
use models\summit\SummitEventType;
/**
* Class SummitEventTypeConstants
* @package App\Models\Foundation\Summit\Events
*/
final class SummitEventTypeConstants
{
public static $valid_class_names = [
SummitEventType::ClassName,
PresentationType::ClassName,
];
}

View File

@ -0,0 +1,41 @@
<?php namespace App\Models\Foundation\Summit\Repositories;
/**
* 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.
**/
use models\summit\Summit;
use models\utils\IBaseRepository;
use utils\Filter;
use utils\Order;
use utils\PagingInfo;
use utils\PagingResponse;
/**
* Interface ISummitEventTypeRepository
* @package App\Models\Foundation\Summit\Repositories
*/
interface ISummitEventTypeRepository extends IBaseRepository
{
/**
* @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
);
}

View File

@ -12,6 +12,7 @@
* limitations under the License.
**/
use App\Models\Foundation\Summit\Repositories\IPresentationSpeakerSummitAssistanceConfirmationRequestRepository;
use App\Models\Foundation\Summit\Repositories\ISummitEventTypeRepository;
use Illuminate\Support\Facades\App;
use Illuminate\Support\ServiceProvider;
use LaravelDoctrine\ORM\Facades\EntityManager;
@ -26,6 +27,7 @@ use models\summit\ISummitTicketTypeRepository;
use models\summit\PresentationSpeakerSummitAssistanceConfirmationRequest;
use models\summit\SpeakerRegistrationRequest;
use models\summit\SpeakerSummitRegistrationPromoCode;
use models\summit\SummitEventType;
use models\summit\SummitRegistrationPromoCode;
use models\summit\SummitTicketType;
/**
@ -263,5 +265,12 @@ final class RepositoriesProvider extends ServiceProvider
return EntityManager::getRepository(PresentationSpeakerSummitAssistanceConfirmationRequest::class);
}
);
App::singleton(
ISummitEventTypeRepository::class,
function(){
return EntityManager::getRepository(SummitEventType::class);
}
);
}
}

View File

@ -0,0 +1,134 @@
<?php namespace App\Repositories\Summit;
/**
* 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.
**/
use App\Models\Foundation\Summit\Repositories\ISummitEventTypeRepository;
use App\Repositories\SilverStripeDoctrineRepository;
use Doctrine\ORM\Tools\Pagination\Paginator;
use models\summit\PresentationType;
use models\summit\Summit;
use models\summit\SummitEventType;
use utils\DoctrineInstanceOfFilterMapping;
use utils\Filter;
use utils\Order;
use utils\PagingInfo;
use utils\PagingResponse;
/**
* Class DoctrineSummitEventTypeRepository
* @package App\Repositories\Summit
*/
final class DoctrineSummitEventTypeRepository
extends SilverStripeDoctrineRepository
implements ISummitEventTypeRepository
{
/**
* @return string
*/
protected function getBaseEntity()
{
return SummitEventType::class;
}
protected function getFilterMappings()
{
return [
'name' => 'et.type:json_string',
'black_out_times' => 'et.black_out_times:json_boolean',
'use_sponsors' => 'et.use_sponsors:json_boolean',
'are_sponsors_mandatory' => 'et.are_sponsors_mandatory:json_boolean',
'allows_attachment' => 'et.allows_attachment:json_boolean',
'use_speakers' => 'pt.use_speakers:json_boolean',
'are_speakers_mandatory' => 'pt.are_speakers_mandatory:json_boolean',
'use_moderator' => 'pt.use_moderator:json_boolean',
'is_moderator_mandatory' => 'pt.is_moderator_mandatory:json_boolean',
'should_be_available_on_cfp' => 'pt.should_be_available_on_cfp:json_boolean',
'class_name' => new DoctrineInstanceOfFilterMapping(
"et",
[
SummitEventType::ClassName => SummitEventType::class,
PresentationType::ClassName => PresentationType::class,
]
)
];
}
/**
* @return array
*/
protected function getOrderMappings()
{
return [
'name' => 'et.type',
'id' => 'et.id',
];
}
/**
* @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("et")
->from(SummitEventType::class, "et")
->leftJoin(PresentationType::class, 'pt', 'WITH', 'pt.id = et.id')
->leftJoin('et.summit', 's')
->where("s.id = :summit_id");
$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("et.type",'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
);
}
}

View File

@ -0,0 +1,81 @@
<?php
/**
* 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 OAuth2EventTypesApiTest
*/
final class OAuth2EventTypesApiTest extends ProtectedApiTest
{
public function testGetEventTypesByClassName(){
$params = [
'id' => 23,
'page' => 1,
'per_page' => 10,
'filter' => 'class_name==EVENT_TYPE',
'order' => '+name'
];
$headers = [
"HTTP_Authorization" => " Bearer " . $this->access_token,
"CONTENT_TYPE" => "application/json"
];
$response = $this->action(
"GET",
"OAuth2SummitsEventTypesApiController@getAllBySummit",
$params,
[],
[],
[],
$headers
);
$content = $response->getContent();
$this->assertResponseStatus(200);
$event_types = json_decode($content);
$this->assertTrue(!is_null($event_types));
}
public function testGetEventTypesByClassNamePresentationType(){
$params = [
'id' => 23,
'page' => 1,
'per_page' => 10,
'filter' => 'class_name==PRESENTATION_TYPE',
'order' => '+name'
];
$headers = [
"HTTP_Authorization" => " Bearer " . $this->access_token,
"CONTENT_TYPE" => "application/json"
];
$response = $this->action(
"GET",
"OAuth2SummitsEventTypesApiController@getAllBySummit",
$params,
[],
[],
[],
$headers
);
$content = $response->getContent();
$this->assertResponseStatus(200);
$event_types = json_decode($content);
$this->assertTrue(!is_null($event_types));
}
}