Added endpoint

POST /api/v1/summits/{id}/rsvp-templates/{template_id}/questions/{question_id}/values

Payload
* label (required|string)
* value (required|string)

Change-Id: I80818505f2877b38241416bd211610593a300267
This commit is contained in:
Sebastian Marcet 2018-03-20 18:05:48 -03:00
parent 3b92c8470a
commit 6d5e0d637b
18 changed files with 466 additions and 10 deletions

View File

@ -109,7 +109,7 @@ final class SummitJsonGenerator extends Command {
$key_current = sprintf('/api/v1/summits/%s.expand=%s','current', urlencode($expand));
$key_id = sprintf('/api/v1/summits/%s.expand=%s', $summit->getIdentifier(), urlencode($expand));
$cache_lifetime = intval(Config::get('server.response_cache_lifetime', 300));
$cache_lifetime = intval(Config::get('cache_api_response.get_summit_response_lifetime', 300));
if($summit->isActive())
{

View File

@ -48,7 +48,7 @@ final class SummitRSVPTemplateQuestionValidationRulesFactory
'name' => 'sometimes|alpha_dash|max:255',
'label' => 'sometimes|string',
'is_mandatory' => 'sometimes|boolean',
'order' => 'sometimes|int|min:1',
'order' => 'sometimes|integer|min:1',
'is_read_only;' => 'sometimes|boolean',
]);
}

View File

@ -0,0 +1,34 @@
<?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 SummitRSVPTemplateQuestionValueValidationRulesFactory
* @package App\Http\Controllers
*/
final class SummitRSVPTemplateQuestionValueValidationRulesFactory
{
public static function build(array $data, $update = false){
if($update){
return [
'value' => 'sometimes|string|max:255',
'label' => 'sometimes|string',
'order' => 'sometimes|integer|min:1',
];
}
return [
'value' => 'required|string|max:255',
'label' => 'required|string',
];
}
}

View File

@ -396,4 +396,49 @@ final class OAuth2SummitRSVPTemplatesApiController extends OAuth2ProtectedContro
return $this->error500($ex);
}
}
/**
* values endpoints
*/
public function addRSVPTemplateQuestionValue($summit_id, $template_id, $question_id){
try {
if(!Request::isJson()) return $this->error400();
$payload = Input::json()->all();
$summit = SummitFinderStrategyFactory::build($this->summit_repository, $this->resource_server_context)->find($summit_id);
if (is_null($summit)) return $this->error404();
$rules = SummitRSVPTemplateQuestionValueValidationRulesFactory::build($payload);
// Creates a Validator instance and validates the data.
$validation = Validator::make($payload, $rules);
if ($validation->fails()) {
$messages = $validation->messages()->toArray();
return $this->error412
(
$messages
);
}
$value = $this->rsvp_template_service->addQuestionValue($summit, $template_id, $question_id, $payload);
return $this->created(SerializerRegistry::getInstance()->getSerializer($value)->serialize());
}
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 (Exception $ex) {
Log::error($ex);
return $this->error500($ex);
}
}
}

View File

@ -178,7 +178,7 @@ Route::group([
Route::delete('', [ 'middleware' => 'auth.user:administrators|summit-front-end-administrators', 'uses' => 'OAuth2SummitRSVPTemplatesApiController@deleteRSVPTemplateQuestion']);
// multi values questions
Route::group(['prefix' => 'values'], function () {
Route::post('', [ 'middleware' => 'auth.user:administrators|summit-front-end-administrators', 'uses' => 'OAuth2SummitRSVPTemplatesApiController@addRSVPTemplateQuestionValue']);
Route::group(['prefix' => '{value_id}'], function () {
});

View File

@ -0,0 +1,26 @@
<?php namespace App\Models\Foundation\Main;
/**
* 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.
**/
interface IOrderable
{
/**
* @param int $order
* @return void
*/
public function setOrder($order);
/**
* @return int
*/
public function getOrder();
}

View File

@ -0,0 +1,61 @@
<?php namespace App\Models\Foundation\Main;
/**
* 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 Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Criteria;
use models\exceptions\ValidationException;
/**
* Trait OrderableChilds
* @package App\Models\Foundation\Main
*/
trait OrderableChilds
{
/**
* @param ArrayCollection $collection
* @param IOrderable $element
* @param $new_order
* @throws ValidationException
*/
private static function recalculateOrderFor(ArrayCollection $collection, IOrderable $element, $new_order){
$criteria = Criteria::create();
$criteria->orderBy(['order'=> 'ASC']);
$elements = $collection->matching($criteria)->toArray();
$elements = array_slice($elements,0, count($elements), false);
$max_order = count($elements);
$former_order = 1;
foreach ($elements as $e){
if($e->getId() == $element->getId()) break;
$former_order++;
}
if($new_order > $max_order)
throw new ValidationException(sprintf("max order is %s", $max_order));
unset($elements[$former_order - 1]);
$elements = array_merge
(
array_slice($elements, 0, $new_order -1 , true) ,
[$elements] ,
array_slice($elements, $new_order -1 , count($elements), true)
);
$order = 1;
foreach($elements as $q){
$q->setOrder($order);
$order++;
}
}
}

View File

@ -14,6 +14,8 @@
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Criteria;
use Doctrine\ORM\Mapping AS ORM;
use models\exceptions\ValidationException;
/**
* @ORM\Table(name="RSVPMultiValueQuestionTemplate")
* @ORM\Entity
@ -123,4 +125,86 @@ class RSVPMultiValueQuestionTemplate extends RSVPQuestionTemplate
}
}
/**
* @param int $id
* @return RSVPQuestionValueTemplate
*/
public function getValueById($id){
$criteria = Criteria::create();
$criteria->where(Criteria::expr()->eq('id', intval($id)));
$res = $this->values->matching($criteria)->first();
return $res ? $res : null;
}
/**
* @param string $value
* @return RSVPQuestionValueTemplate
*/
public function getValueByValue($value){
$criteria = Criteria::create();
$criteria->where(Criteria::expr()->eq('value', trim($value)));
$res = $this->values->matching($criteria)->first();
return $res ? $res : null;
}
/**
* @param RSVPQuestionValueTemplate $value
* @return $this
*/
public function addValue(RSVPQuestionValueTemplate $value){
$values = $this->getValues();
$this->values->add($value);
$value->setOwner($this);
$value->setOrder(count($values) + 1);
return $this;
}
/**
* @param RSVPQuestionValueTemplate $value
* @param int $new_order
* @throws ValidationException
*/
public function recalculateValueOrder(RSVPQuestionValueTemplate $value, $new_order){
$criteria = Criteria::create();
$criteria->orderBy(['order'=> 'ASC']);
$values = $this->values->matching($criteria)->toArray();
$values = array_slice($values,0, count($values), false);
$max_order = count($values);
$former_order = 1;
foreach ($values as $v){
if($v->getId() == $value->getId()) break;
$former_order++;
}
if($new_order > $max_order)
throw new ValidationException(sprintf("max order is %s", $max_order));
unset($values[$former_order - 1]);
$values = array_merge
(
array_slice($values, 0, $new_order -1 , true) ,
[$values] ,
array_slice($values, $new_order -1 , count($values), true)
);
$order = 1;
foreach($values as $v){
$v->setOrder($order);
$order++;
}
}
/**
* @param RSVPQuestionValueTemplate $value
* @return $this
*/
public function removeValue(RSVPQuestionValueTemplate $value){
$this->values->removeElement($value);
$value->clearOwner();
return $this;
}
}

View File

@ -11,6 +11,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
**/
use App\Models\Foundation\Main\IOrderable;
use Doctrine\ORM\Mapping AS ORM;
use models\utils\SilverstripeBaseModel;
/**
@ -35,7 +36,7 @@ use models\utils\SilverstripeBaseModel;
* Class RSVPQuestionTemplate
* @package App\Models\Foundation\Summit\Events\RSVP
*/
class RSVPQuestionTemplate extends SilverstripeBaseModel
class RSVPQuestionTemplate extends SilverstripeBaseModel implements IOrderable
{
/**
@ -57,8 +58,8 @@ class RSVPQuestionTemplate extends SilverstripeBaseModel
protected $is_mandatory;
/**
* @ORM\Column(name="`Order`", type="string")
* @var string
* @ORM\Column(name="`Order`", type="integer")
* @var int
*/
protected $order;
@ -130,7 +131,7 @@ class RSVPQuestionTemplate extends SilverstripeBaseModel
}
/**
* @return string
* @return int
*/
public function getOrder()
{
@ -138,7 +139,7 @@ class RSVPQuestionTemplate extends SilverstripeBaseModel
}
/**
* @param string $order
* @param int $order
*/
public function setOrder($order)
{

View File

@ -121,4 +121,8 @@ class RSVPQuestionValueTemplate extends SilverstripeBaseModel
return 0;
}
}
public function clearOwner(){
$this->owner = null;
}
}

View File

@ -11,8 +11,10 @@
* See the License for the specific language governing permissions and
* limitations under the License.
**/
use App\Models\Foundation\Main\OrderableChilds;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Criteria;
use models\exceptions\ValidationException;
use models\main\Member;
use models\summit\SummitOwned;
use models\utils\SilverstripeBaseModel;
@ -177,6 +179,17 @@ class RSVPTemplate extends SilverstripeBaseModel
return $this;
}
use OrderableChilds;
/**
* @param RSVPQuestionTemplate $question
* @param int $new_order
* @throws ValidationException
*/
public function recalculateQuestionOrder(RSVPQuestionTemplate $question, $new_order){
self::recalculateOrderFor($this->questions, $question, $new_order);
}
/**
* @param RSVPQuestionTemplate $question
* @return $this
@ -186,5 +199,4 @@ class RSVPTemplate extends SilverstripeBaseModel
$question->clearTemplate();
return $this;
}
}

View File

@ -0,0 +1,44 @@
<?php namespace App\Models\Foundation\Summit\Factories;
/**
* 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\Events\RSVP\RSVPQuestionValueTemplate;
/**
* Class SummitRSVPTemplateQuestionValueFactory
* @package App\Models\Foundation\Summit\Factories
*/
final class SummitRSVPTemplateQuestionValueFactory
{
/**
* @param array $data
* @return RSVPQuestionValueTemplate
*/
public static function build(array $data){
return self::populate(new RSVPQuestionValueTemplate, $data);
}
/**
* @param RSVPQuestionValueTemplate $value
* @param array $data
* @return RSVPQuestionValueTemplate
*/
public static function populate(RSVPQuestionValueTemplate $value, array $data){
if(isset($data['value']))
$value->setValue(trim($data['value']));
if(isset($data['label']))
$value->setLabel(trim($data['label']));
return $value;
}
}

View File

@ -12,6 +12,7 @@
* limitations under the License.
**/
use App\Models\Foundation\Summit\Events\RSVP\RSVPQuestionTemplate;
use App\Models\Foundation\Summit\Events\RSVP\RSVPQuestionValueTemplate;
use models\exceptions\EntityNotFoundException;
use models\exceptions\ValidationException;
use models\summit\Summit;
@ -61,4 +62,15 @@ interface IRSVPTemplateService
*/
public function deleteQuestion(Summit $summit, $template_id, $question_id);
/**
* @param Summit $summit
* @param int $template_id
* @param int $question_id
* @param array $payload
* @return RSVPQuestionValueTemplate
* @throws EntityNotFoundException
* @throws ValidationException
*/
public function addQuestionValue($summit, $template_id, $question_id, $payload);
}

View File

@ -11,8 +11,11 @@
* See the License for the specific language governing permissions and
* limitations under the License.
**/
use App\Models\Foundation\Summit\Events\RSVP\RSVPMultiValueQuestionTemplate;
use App\Models\Foundation\Summit\Events\RSVP\RSVPQuestionTemplate;
use App\Models\Foundation\Summit\Events\RSVP\RSVPQuestionValueTemplate;
use App\Models\Foundation\Summit\Factories\SummitRSVPTemplateQuestionFactory;
use App\Models\Foundation\Summit\Factories\SummitRSVPTemplateQuestionValueFactory;
use App\Models\Foundation\Summit\Repositories\IRSVPTemplateRepository;
use libs\utils\ITransactionService;
use models\exceptions\EntityNotFoundException;
@ -183,6 +186,11 @@ final class RSVPTemplateService implements IRSVPTemplateService
}
}
if (isset($data['order']) && intval($data['order']) != $question->getOrder()) {
// request to update order
$template->recalculateQuestionOrder($question, intval($data['order']));
}
return SummitRSVPTemplateQuestionFactory::populate($question, $data);
});
@ -233,4 +241,87 @@ final class RSVPTemplateService implements IRSVPTemplateService
$template->removeQuestion($question);
});
}
/**
* @param Summit $summit
* @param int $template_id
* @param int $question_id
* @param array $data
* @return RSVPQuestionValueTemplate
* @throws EntityNotFoundException
* @throws ValidationException
*/
public function addQuestionValue($summit, $template_id, $question_id, $data)
{
return $this->tx_service->transaction(function() use($summit, $template_id, $question_id, $data){
$template = $summit->getRSVPTemplateById($template_id);
if(is_null($template))
throw new EntityNotFoundException
(
trans
(
'not_found_errors.RSVPTemplateService.addQuestionValue.TemplateNotFound',
[
'summit_id' => $summit->getId(),
'template_id' => $template_id,
]
)
);
$question = $template->getQuestionById($question_id);
if(is_null($question))
throw new EntityNotFoundException
(
trans
(
'not_found_errors.RSVPTemplateService.addQuestionValue.QuestionNotFound',
[
'summit_id' => $summit->getId(),
'template_id' => $template_id,
'question_id' => $question_id,
]
)
);
if(!$question instanceof RSVPMultiValueQuestionTemplate){
throw new EntityNotFoundException
(
trans
(
'not_found_errors.RSVPTemplateService.addQuestionValue.QuestionNotFound',
[
'summit_id' => $summit->getId(),
'template_id' => $template_id,
'question_id' => $question_id,
]
)
);
}
$former_value = $question->getValueByValue($data['value']);
if(!is_null($former_value)){
throw new ValidationException
(
trans
(
'validation_errors.RSVPTemplateService.addQuestionValue.ValueAlreadyExist',
[
'summit_id' => $summit->getId(),
'template_id' => $template_id,
'question_id' => $question_id,
'value' => $data['value']
]
)
);
}
$value = SummitRSVPTemplateQuestionValueFactory::build($data);
$question->addValue($value);
return $value;
});
}
}

View File

@ -13,7 +13,7 @@
**/
return [
'get_summit_response_lifetime' => env('CACHE_API_RESPONSE_GET_SUMMIT_LIFETIME', 300),
'get_summit_response_lifetime' => env('CACHE_API_RESPONSE_GET_SUMMIT_LIFETIME', 600),
'get_event_feedback_response_lifetime' => env('CACHE_API_RESPONSE_GET_EVENT_FEEDBACK_LIFETIME', 300),
'get_published_event_response_lifetime' => env('CACHE_API_RESPONSE_GET_PUBLISHED_EVENT_LIFETIME', 300),
'get_summits_response_lifetime' => env('CACHE_API_RESPONSE_GET_SUMMITS_LIFETIME', 600),

View File

@ -48,4 +48,6 @@ return [
'RSVPTemplateService.updateQuestion.QuestionNotFound' => 'question :question_id not found on template :template_id',
'RSVPTemplateService.deleteQuestion.TemplateNotFound' => 'template :template_id not found on summit :summit_id',
'RSVPTemplateService.deleteQuestion.QuestionNotFound' => 'question :question_id not found on template :template_id',
'RSVPTemplateService.addQuestionValue.TemplateNotFound' => 'template :template_id not found on summit :summit_id',
'RSVPTemplateService.addQuestionValue.QuestionNotFound' => 'question :question_id not found on template :template_id',
];

View File

@ -54,4 +54,5 @@ return [
// RSVP Template Service
'RSVPTemplateService.addQuestion.QuestionNameAlreadyExists' => 'question name :name already exists for template :template_id',
'RSVPTemplateService.updateQuestion.QuestionNameAlreadyExists' => 'question name :name already exists for template :template_id',
'RSVPTemplateService.addQuestionValue.ValueAlreadyExist' => 'value :value already exists on question :question_id',
];

View File

@ -269,4 +269,43 @@ final class OAuth2SummitRSVPTemplateApiTest extends ProtectedApiTest
$content = $response->getContent();
$this->assertResponseStatus(204);
}
public function testAddRSVPQuestionValue($summit_id = 24, $template_id = 13, $question_id = 86){
$params = [
'id' => $summit_id,
'template_id' => $template_id,
'question_id' => $question_id
];
$value = str_random(16).'_value';
$label = str_random(16).'_label';
$data = [
'value' => $value,
'label' => $label,
];
$headers = [
"HTTP_Authorization" => " Bearer " . $this->access_token,
"CONTENT_TYPE" => "application/json"
];
$response = $this->action(
"POST",
"OAuth2SummitRSVPTemplatesApiController@addRSVPTemplateQuestionValue",
$params,
[],
[],
[],
$headers,
json_encode($data)
);
$content = $response->getContent();
$this->assertResponseStatus(201);
$value = json_decode($content);
$this->assertTrue(!is_null($value));
return $value;
}
}