
by external user this is a change required after this update https://review.opendev.org/#/c/661661/ Change-Id: I13283c0a491ffb924d67bdeac84b5adcf3afe45c
689 lines
22 KiB
PHP
689 lines
22 KiB
PHP
<?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 App\Http\Utils\PagingConstants;
|
|
use App\Models\Foundation\Summit\Events\RSVP\RSVPMultiValueQuestionTemplate;
|
|
use App\Models\Foundation\Summit\Repositories\IRSVPTemplateRepository;
|
|
use App\Services\Model\IRSVPTemplateService;
|
|
use Illuminate\Support\Facades\Input;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Illuminate\Support\Facades\Request;
|
|
use Illuminate\Support\Facades\Validator;
|
|
use models\exceptions\EntityNotFoundException;
|
|
use models\exceptions\ValidationException;
|
|
use models\main\IMemberRepository;
|
|
use models\oauth2\IResourceServerContext;
|
|
use models\summit\ISummitRepository;
|
|
use ModelSerializers\SerializerRegistry;
|
|
use utils\Filter;
|
|
use utils\FilterParser;
|
|
use utils\OrderParser;
|
|
use utils\PagingInfo;
|
|
use Exception;
|
|
/**
|
|
* Class OAuth2SummitRSVPTemplatesApiController
|
|
* @package App\Http\Controllers
|
|
*/
|
|
final class OAuth2SummitRSVPTemplatesApiController extends OAuth2ProtectedController
|
|
{
|
|
|
|
/**
|
|
* @var ISummitRepository
|
|
*/
|
|
private $summit_repository;
|
|
|
|
/**
|
|
* @var IRSVPTemplateRepository
|
|
*/
|
|
private $rsvp_template_repository;
|
|
|
|
/**
|
|
* @var IRSVPTemplateService
|
|
*/
|
|
private $rsvp_template_service;
|
|
|
|
/**
|
|
* @var IMemberRepository
|
|
*/
|
|
private $member_repository;
|
|
|
|
/**
|
|
* OAuth2SummitRSVPTemplatesApiController constructor.
|
|
* @param ISummitRepository $summit_repository
|
|
* @param IRSVPTemplateRepository $rsvp_template_repository
|
|
* @param IMemberRepository $member_repository
|
|
* @param IRSVPTemplateService $rsvp_template_service
|
|
* @param IResourceServerContext $resource_server_context
|
|
*/
|
|
public function __construct
|
|
(
|
|
ISummitRepository $summit_repository,
|
|
IRSVPTemplateRepository $rsvp_template_repository,
|
|
IMemberRepository $member_repository,
|
|
IRSVPTemplateService $rsvp_template_service,
|
|
IResourceServerContext $resource_server_context
|
|
)
|
|
{
|
|
parent::__construct($resource_server_context);
|
|
$this->summit_repository = $summit_repository;
|
|
$this->member_repository = $member_repository;
|
|
$this->rsvp_template_service = $rsvp_template_service;
|
|
$this->rsvp_template_repository = $rsvp_template_repository;
|
|
}
|
|
|
|
/**
|
|
* Template endpoints
|
|
*/
|
|
|
|
/**
|
|
* @param $summit_id
|
|
* @return mixed
|
|
*/
|
|
public function getAllBySummit($summit_id){
|
|
|
|
$values = Input::all();
|
|
$rules = [
|
|
'page' => 'integer|min:1',
|
|
'per_page' => sprintf('required_with:page|integer|min:%s|max:%s', PagingConstants::MinPageSize, PagingConstants::MaxPageSize),
|
|
];
|
|
|
|
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 = PagingConstants::DefaultPageSize;
|
|
|
|
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'), [
|
|
'title' => ['=@', '=='],
|
|
'is_enabled' => [ '=='],
|
|
]);
|
|
}
|
|
|
|
if(is_null($filter)) $filter = new Filter();
|
|
|
|
$filter->validate([
|
|
'title' => 'sometimes|string',
|
|
'is_enabled' => 'sometimes|boolean',
|
|
]);
|
|
|
|
$order = null;
|
|
|
|
if (Input::has('order'))
|
|
{
|
|
$order = OrderParser::parse(Input::get('order'), [
|
|
'id',
|
|
'title',
|
|
]);
|
|
}
|
|
|
|
$data = $this->rsvp_template_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([ $ex1->getMessage()]);
|
|
}
|
|
catch (EntityNotFoundException $ex2)
|
|
{
|
|
Log::warning($ex2);
|
|
return $this->error404(['message' => $ex2->getMessage()]);
|
|
}
|
|
catch(\HTTP401UnauthorizedException $ex3)
|
|
{
|
|
Log::warning($ex3);
|
|
return $this->error401();
|
|
}
|
|
catch (\Exception $ex) {
|
|
Log::error($ex);
|
|
return $this->error500($ex);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param $summit_id
|
|
* @param $template_id
|
|
* @return mixed
|
|
*/
|
|
public function getRSVPTemplate($summit_id, $template_id){
|
|
try {
|
|
|
|
$expand = Request::input('expand', '');
|
|
$relations = Request::input('relations', '');
|
|
$relations = !empty($relations) ? explode(',', $relations) : [];
|
|
|
|
$summit = SummitFinderStrategyFactory::build($this->summit_repository, $this->resource_server_context)->find($summit_id);
|
|
if (is_null($summit)) return $this->error404();
|
|
|
|
$template = $summit->getRSVPTemplateById($template_id);
|
|
|
|
if (is_null($template)) {
|
|
return $this->error404();
|
|
}
|
|
|
|
return $this->ok(SerializerRegistry::getInstance()->getSerializer($template)->serialize($expand,[], $relations));
|
|
}
|
|
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);
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* @param $summit_id
|
|
* @return mixed
|
|
*/
|
|
public function getRSVPTemplateQuestionsMetadata($summit_id){
|
|
$summit = SummitFinderStrategyFactory::build($this->summit_repository, $this->resource_server_context)->find($summit_id);
|
|
if (is_null($summit)) return $this->error404();
|
|
|
|
return $this->ok
|
|
(
|
|
$this->rsvp_template_repository->getQuestionsMetadata($summit)
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @param $summit_id
|
|
* @param $template_id
|
|
* @return mixed
|
|
*/
|
|
public function deleteRSVPTemplate($summit_id, $template_id){
|
|
try {
|
|
|
|
$summit = SummitFinderStrategyFactory::build($this->summit_repository, $this->resource_server_context)->find($summit_id);
|
|
if (is_null($summit)) return $this->error404();
|
|
|
|
$this->rsvp_template_service->deleteTemplate($summit, $template_id);
|
|
|
|
return $this->deleted();
|
|
}
|
|
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);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param $summit_id
|
|
* @return mixed
|
|
*/
|
|
public function addRSVPTemplate($summit_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 = SummitRSVPTemplateValidationRulesFactory::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
|
|
);
|
|
}
|
|
|
|
$template = $this->rsvp_template_service->addTemplate($summit, $this->resource_server_context->getCurrentUser(), $payload);
|
|
|
|
return $this->created(SerializerRegistry::getInstance()->getSerializer($template)->serialize());
|
|
}
|
|
catch (ValidationException $ex1) {
|
|
Log::warning($ex1);
|
|
return $this->error412([$ex1->getMessage()]);
|
|
}
|
|
catch(EntityNotFoundException $ex2)
|
|
{
|
|
Log::warning($ex2);
|
|
return $this->error404(['message'=> $ex2->getMessage()]);
|
|
}
|
|
catch (Exception $ex) {
|
|
Log::error($ex);
|
|
return $this->error500($ex);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param $summit_id
|
|
* @param $template_id
|
|
* @return mixed
|
|
*/
|
|
public function updateRSVPTemplate($summit_id, $template_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 = SummitRSVPTemplateValidationRulesFactory::build($payload, true);
|
|
// Creates a Validator instance and validates the data.
|
|
$validation = Validator::make($payload, $rules);
|
|
|
|
if ($validation->fails()) {
|
|
$messages = $validation->messages()->toArray();
|
|
|
|
return $this->error412
|
|
(
|
|
$messages
|
|
);
|
|
}
|
|
|
|
$template = $this->rsvp_template_service->updateTemplate($summit, $template_id, $payload);
|
|
|
|
return $this->updated(SerializerRegistry::getInstance()->getSerializer($template)->serialize());
|
|
}
|
|
catch (ValidationException $ex1) {
|
|
Log::warning($ex1);
|
|
return $this->error412([$ex1->getMessage()]);
|
|
}
|
|
catch(EntityNotFoundException $ex2)
|
|
{
|
|
Log::warning($ex2);
|
|
return $this->error404(['message'=> $ex2->getMessage()]);
|
|
}
|
|
catch (Exception $ex) {
|
|
Log::error($ex);
|
|
return $this->error500($ex);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Questions endpoints
|
|
*/
|
|
|
|
/**
|
|
* @param $summit_id
|
|
* @param $template_id
|
|
* @param $question_id
|
|
* @return mixed
|
|
*/
|
|
public function getRSVPTemplateQuestion($summit_id, $template_id, $question_id){
|
|
try {
|
|
|
|
$summit = SummitFinderStrategyFactory::build($this->summit_repository, $this->resource_server_context)->find($summit_id);
|
|
if (is_null($summit)) return $this->error404();
|
|
|
|
$template = $summit->getRSVPTemplateById($template_id);
|
|
if (is_null($template)) return $this->error404();
|
|
|
|
$question = $template->getQuestionById($question_id);
|
|
if (is_null($question)) return $this->error404();
|
|
return $this->ok(SerializerRegistry::getInstance()->getSerializer($question)->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);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param $summit_id
|
|
* @param $template_id
|
|
* @return mixed
|
|
*/
|
|
public function addRSVPTemplateQuestion($summit_id, $template_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 = SummitRSVPTemplateQuestionValidationRulesFactory::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
|
|
);
|
|
}
|
|
|
|
$question = $this->rsvp_template_service->addQuestion($summit, $template_id, $payload);
|
|
|
|
return $this->created(SerializerRegistry::getInstance()->getSerializer($question)->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);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param $summit_id
|
|
* @param $template_id
|
|
* @param $question_id
|
|
* @return mixed
|
|
*/
|
|
public function updateRSVPTemplateQuestion($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 = SummitRSVPTemplateQuestionValidationRulesFactory::build($payload, true);
|
|
// Creates a Validator instance and validates the data.
|
|
$validation = Validator::make($payload, $rules);
|
|
|
|
if ($validation->fails()) {
|
|
$messages = $validation->messages()->toArray();
|
|
|
|
return $this->error412
|
|
(
|
|
$messages
|
|
);
|
|
}
|
|
|
|
$question = $this->rsvp_template_service->updateQuestion($summit, $template_id, $question_id, $payload);
|
|
|
|
return $this->updated(SerializerRegistry::getInstance()->getSerializer($question)->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);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param $summit_id
|
|
* @param $template_id
|
|
* @param $question_id
|
|
* @return mixed
|
|
*/
|
|
public function deleteRSVPTemplateQuestion($summit_id, $template_id, $question_id){
|
|
try {
|
|
|
|
$summit = SummitFinderStrategyFactory::build($this->summit_repository, $this->resource_server_context)->find($summit_id);
|
|
if (is_null($summit)) return $this->error404();
|
|
|
|
$this->rsvp_template_service->deleteQuestion($summit, $template_id, $question_id);
|
|
|
|
return $this->deleted();
|
|
}
|
|
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);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* values endpoints
|
|
*/
|
|
|
|
/**
|
|
* @param $summit_id
|
|
* @param $template_id
|
|
* @param $question_id
|
|
* @param $value_id
|
|
* @return mixed
|
|
*/
|
|
public function getRSVPTemplateQuestionValue($summit_id, $template_id, $question_id, $value_id){
|
|
try {
|
|
|
|
$summit = SummitFinderStrategyFactory::build($this->summit_repository, $this->resource_server_context)->find($summit_id);
|
|
if (is_null($summit)) return $this->error404();
|
|
|
|
$template = $summit->getRSVPTemplateById($template_id);
|
|
if (is_null($template)) return $this->error404();
|
|
|
|
$question = $template->getQuestionById($question_id);
|
|
if (is_null($question)) return $this->error404();
|
|
|
|
if (!$question instanceof RSVPMultiValueQuestionTemplate) return $this->error404();
|
|
|
|
$value = $question->getValueById($value_id);
|
|
if (is_null($value)) return $this->error404();
|
|
|
|
return $this->ok(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);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param $summit_id
|
|
* @param $template_id
|
|
* @param $question_id
|
|
* @return mixed
|
|
*/
|
|
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);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param $summit_id
|
|
* @param $template_id
|
|
* @param $question_id
|
|
* @param $value_id
|
|
* @return mixed
|
|
*/
|
|
public function updateRSVPTemplateQuestionValue($summit_id, $template_id, $question_id, $value_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, true);
|
|
// 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->updateQuestionValue($summit, $template_id, $question_id, $value_id, $payload);
|
|
|
|
return $this->updated(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);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param $summit_id
|
|
* @param $template_id
|
|
* @param $question_id
|
|
* @param $value_id
|
|
* @return mixed
|
|
*/
|
|
public function deleteRSVPTemplateQuestionValue($summit_id, $template_id, $question_id, $value_id){
|
|
try {
|
|
|
|
$summit = SummitFinderStrategyFactory::build($this->summit_repository, $this->resource_server_context)->find($summit_id);
|
|
if (is_null($summit)) return $this->error404();
|
|
|
|
$this->rsvp_template_service->deleteQuestionValue($summit, $template_id, $question_id, $value_id);
|
|
|
|
return $this->deleted();
|
|
}
|
|
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);
|
|
}
|
|
}
|
|
} |