Fix on get speakers

fixed parsing order param error
fixed admin serializer

Change-Id: I824d374bd43fe4c7d4ab54bdb15e3d6a24c2c913
This commit is contained in:
Sebastian Marcet 2017-12-17 16:02:13 -03:00
parent e1546336a3
commit 788a548c3e
9 changed files with 217 additions and 37 deletions

View File

@ -141,22 +141,24 @@ final class OAuth2SummitSpeakersApiController extends OAuth2ProtectedController
if (Input::has('filter'))
{
$filter = FilterParser::parse(Input::get('filter'), array
(
'first_name' => array('=@', '=='),
'last_name' => array('=@', '=='),
'email' => array('=@', '=='),
));
$filter = FilterParser::parse(Input::get('filter'), [
'first_name' => ['=@', '=='],
'last_name' => ['=@', '=='],
'email' => ['=@', '=='],
'id' => ['=='],
]);
}
$order = null;
if (Input::has('order'))
{
$order = OrderParser::parse(Input::get('order'), array
(
$order = OrderParser::parse(Input::get('order'), [
'first_name',
'last_name',
));
'id',
'email',
]);
}
$current_member_id = $this->resource_server_context->getCurrentUserId();
@ -172,7 +174,7 @@ final class OAuth2SummitSpeakersApiController extends OAuth2ProtectedController
$result->toArray(Request::input('expand', ''),[],[],['summit_id' => $summit_id, 'published' => true, 'summit' => $summit], $serializer_type)
);
}
catch(FilterParserException $ex1){
catch(ValidationException $ex1){
Log::warning($ex1);
return $this->error412($ex1->getMessages());
}
@ -223,22 +225,24 @@ final class OAuth2SummitSpeakersApiController extends OAuth2ProtectedController
if (Input::has('filter'))
{
$filter = FilterParser::parse(Input::get('filter'), array
(
'first_name' => array('=@', '=='),
'last_name' => array('=@', '=='),
'email' => array('=@', '=='),
));
$filter = FilterParser::parse(Input::get('filter'), [
'first_name' => ['=@', '=='],
'last_name' => ['=@', '=='],
'email' => ['=@', '=='],
'id' => ['=='],
]);
}
$order = null;
if (Input::has('order'))
{
$order = OrderParser::parse(Input::get('order'), array
(
$order = OrderParser::parse(Input::get('order'), [
'id',
'email',
'first_name',
'last_name',
));
]);
}
$result = $this->speaker_repository->getAllByPage(new PagingInfo($page, $per_page), $filter, $order);
@ -248,7 +252,7 @@ final class OAuth2SummitSpeakersApiController extends OAuth2ProtectedController
$result->toArray(Request::input('expand', ''),[],[])
);
}
catch(FilterParserException $ex1){
catch(ValidationException $ex1){
Log::warning($ex1);
return $this->error412($ex1->getMessages());
}
@ -299,6 +303,10 @@ final class OAuth2SummitSpeakersApiController extends OAuth2ProtectedController
);
}
catch(ValidationException $ex1){
Log::warning($ex1);
return $this->error412($ex1->getMessages());
}
catch(EntityNotFoundException $ex2)
{
Log::warning($ex2);

View File

@ -324,21 +324,25 @@ final class Filter
*/
public function toRawSQL(array $mappings)
{
$sql = '';
$sql = '';
$this->bindings = [];
$param_prefix = "param_%s";
$param_idx = 1;
foreach ($this->filters as $filter) {
if ($filter instanceof FilterElement) {
if (isset($mappings[$filter->getField()])) {
$mapping = $mappings[$filter->getField()];
$mapping = explode(':', $mapping);
$value = $filter->getValue();
$op = $filter->getOperator();
$value = $filter->getValue();
$op = $filter->getOperator();
if (count($mapping) > 1) {
$filter->setValue($this->convertValue($value, $mapping[1]));
}
$cond = sprintf(' %s %s :%s', $mapping[0], $op, $filter->getField());
$this->bindings[$filter->getField()] = $filter->getValue();
$cond = sprintf(' %s %s :%s', $mapping[0], $op, sprintf($param_prefix, $param_idx));
$this->bindings[sprintf($param_prefix, $param_idx)] = $filter->getValue();
++$param_idx;
if (!empty($sql)) $sql .= " AND ";
$sql .= $cond;
}
@ -350,13 +354,14 @@ final class Filter
if ($e instanceof FilterElement && isset($mappings[$e->getField()])) {
$mapping = $mappings[$e->getField()];
$mapping = explode(':', $mapping);
$value = $e->getValue();
$op = $e->getOperator();
$value = $e->getValue();
$op = $e->getOperator();
if (count($mapping) > 1) {
$e->setValue($this->convertValue($value, $mapping[1]));
}
$cond = sprintf(" %s %s :%s", $mapping[0], $op, $e->getField());
$this->bindings[$e->getField()] = $e->getValue();
$cond = sprintf(" %s %s :%s", $mapping[0], $op, sprintf($param_prefix, $param_idx));
$this->bindings[sprintf($param_prefix, $param_idx)] = $e->getValue();
++$param_idx;
if (!empty($sql_or)) $sql_or .= " OR ";
$sql_or .= $cond;
}

View File

@ -24,10 +24,11 @@ final class OrderParser
* @param string $orders
* @param array $allowed_fields
* @return Order
* @throws OrderParserException
*/
public static function parse($orders, $allowed_fields = array())
public static function parse($orders, $allowed_fields = [])
{
$res = array();
$res = [];
$orders = explode(',', $orders);
//default ordering is asc
foreach($orders as $field)
@ -36,18 +37,21 @@ final class OrderParser
if(strpos($field, '+') === 0)
{
$field = trim($field,'+');
if(!in_array($field, $allowed_fields)) continue;
if(!in_array($field, $allowed_fields))
throw new OrderParserException(sprintf("filter by field %s is not allowed", $field));
$element = OrderElement::buildAscFor($field);
}
else if(strpos($field, '-') === 0)
{
$field = trim($field,'-');
if(!in_array($field, $allowed_fields)) continue;
if(!in_array($field, $allowed_fields))
throw new OrderParserException(sprintf("filter by field %s is not allowed", $field));
$element = OrderElement::buildDescFor($field);
}
else
{
if(!in_array($field, $allowed_fields)) continue;
if(!in_array($field, $allowed_fields))
throw new OrderParserException(sprintf("filter by field %s is not allowed", $field));
$element = OrderElement::buildAscFor($field);
}
array_push($res, $element);

View File

@ -0,0 +1,25 @@
<?php namespace utils;
/**
* Copyright 2017 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\exceptions\ValidationException;
/**
* Class OrderParserException
* @package utils
*/
final class OrderParserException extends ValidationException
{
public function __construct($message = "") {
parent::__construct($message, 0, null);
}
}

View File

@ -35,6 +35,7 @@ final class AdminPresentationSpeakerSerializer extends PresentationSpeakerSerial
if(!$speaker instanceof PresentationSpeaker) return [];
$values = parent::serialize($expand, $fields, $relations, $params);
$values['email'] = $speaker->getEmail();
$summit = isset($params['summit'])? $params['summit']:null;
if(!is_null($summit)){

View File

@ -513,6 +513,42 @@ SQL;
return $summits;
}
/**
* @return null|string
*/
public function getEmail(){
if($this->hasMember()){
return $this->member->getEmail();
}
if($this->hasRegistrationRequest()){
return $this->registration_request->getEmail();
}
return null;
}
/**
* @return bool
*/
public function hasRegistrationRequest(){
return $this->getRegistrationRequestId() > 0;
}
/**
* @return int
*/
public function getRegistrationRequestId()
{
try{
if(is_null($this->registration_request)) return 0;
return $this->registration_request->getId();
}
catch(\Exception $ex){
return 0;
}
}
/**
* @return PresentationSpeakerSummitAssistanceConfirmationRequest
*/

View File

@ -69,7 +69,7 @@ class SpeakerRegistrationRequest extends SilverstripeBaseModel
private $token;
/**
* @return mixed
* @return string
*/
public function getEmail()
{

View File

@ -52,6 +52,7 @@ final class DoctrineSpeakerRepository
'first_name' => 'FirstName',
'last_name' => 'LastName',
'email' => 'Email',
'id' => 'ID'
]);
if(!empty($where_conditions)) {
$extra_filters = " WHERE {$where_conditions}";
@ -63,6 +64,8 @@ final class DoctrineSpeakerRepository
{
$extra_orders = $order->toRawSQL(array
(
'id' => 'ID',
'email' => 'Email',
'first_name' => 'FirstName',
'last_name' => 'LastName',
));
@ -155,7 +158,7 @@ FROM (
)
UNION
SELECT
S.ID,
S.ID,
S.ClassName,
S.Created,
S.LastEdited,
@ -242,6 +245,7 @@ SQL;
'first_name' => 'FirstName',
'last_name' => 'LastName',
'email' => 'Email',
'id' => 'ID'
]);
if(!empty($where_conditions)) {
$extra_filters = " WHERE {$where_conditions}";
@ -255,6 +259,8 @@ SQL;
(
'first_name' => 'FirstName',
'last_name' => 'LastName',
'email' => 'Email',
'id' => 'ID'
));
}

View File

@ -13,10 +13,10 @@
**/
class OAuth2SpeakersApiTest extends ProtectedApiTest
{
public function testPostSpeaker($summit_id = 23)
{
$params = [
'id' => $summit_id,
];
@ -166,4 +166,99 @@ class OAuth2SpeakersApiTest extends ProtectedApiTest
$this->assertTrue($speaker->id > 0);
return $speaker;
}
public function testGetCurrentSummitSpeakersOrderByID()
{
$params = [
'id' => 23,
'page' => 1,
'per_page' => 10,
'order' => '+id'
];
$headers = [
"HTTP_Authorization" => " Bearer " . $this->access_token,
"CONTENT_TYPE" => "application/json"
];
$response = $this->action(
"GET",
"OAuth2SummitSpeakersApiController@getSpeakers",
$params,
[],
[],
[],
$headers
);
$content = $response->getContent();
$this->assertResponseStatus(200);
$speakers = json_decode($content);
$this->assertTrue(!is_null($speakers));
}
public function testGetCurrentSummitSpeakersOrderByEmail()
{
$params = [
'id' => 23,
'page' => 1,
'per_page' => 10,
'order' => '+email'
];
$headers = [
"HTTP_Authorization" => " Bearer " . $this->access_token,
"CONTENT_TYPE" => "application/json"
];
$response = $this->action(
"GET",
"OAuth2SummitSpeakersApiController@getSpeakers",
$params,
[],
[],
[],
$headers
);
$content = $response->getContent();
$this->assertResponseStatus(200);
$speakers = json_decode($content);
$this->assertTrue(!is_null($speakers));
}
public function testGetCurrentSummitSpeakersByIDMultiple()
{
$params = [
'id' => 23,
'page' => 1,
'per_page' => 10,
'filter' => 'id==1,id==19'
];
$headers = [
"HTTP_Authorization" => " Bearer " . $this->access_token,
"CONTENT_TYPE" => "application/json"
];
$response = $this->action(
"GET",
"OAuth2SummitSpeakersApiController@getSpeakers",
$params,
[],
[],
[],
$headers
);
$content = $response->getContent();
$this->assertResponseStatus(200);
$speakers = json_decode($content);
$this->assertTrue(!is_null($speakers));
}
}