437 lines
14 KiB
PHP
437 lines
14 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\EpochCellFormatter;
|
|
use App\Http\Utils\PagingConstants;
|
|
use App\Services\Model\ISummitTicketTypeService;
|
|
use Illuminate\Support\Facades\Request;
|
|
use models\summit\ISummitTicketTypeRepository;
|
|
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 ModelSerializers\SerializerRegistry;
|
|
use utils\Filter;
|
|
use utils\FilterParser;
|
|
use utils\OrderParser;
|
|
use utils\PagingInfo;
|
|
use Exception;
|
|
use utils\PagingResponse;
|
|
/**
|
|
* Class OAuth2SummitsTicketTypesApiController
|
|
* @package App\Http\Controllers
|
|
*/
|
|
final class OAuth2SummitsTicketTypesApiController extends OAuth2ProtectedController
|
|
{
|
|
/**
|
|
* @var ISummitRepository
|
|
*/
|
|
private $summit_repository;
|
|
|
|
/**
|
|
* @var ISummitTicketTypeService
|
|
*/
|
|
private $ticket_type_service;
|
|
|
|
/**
|
|
* OAuth2SummitsTicketTypesApiController constructor.
|
|
* @param ISummitTicketTypeRepository $repository
|
|
* @param ISummitRepository $summit_repository
|
|
* @param ISummitTicketTypeService $ticket_type_service
|
|
* @param IResourceServerContext $resource_server_context
|
|
*/
|
|
public function __construct
|
|
(
|
|
ISummitTicketTypeRepository $repository,
|
|
ISummitRepository $summit_repository,
|
|
ISummitTicketTypeService $ticket_type_service,
|
|
IResourceServerContext $resource_server_context
|
|
)
|
|
{
|
|
parent::__construct($resource_server_context);
|
|
$this->repository = $repository;
|
|
$this->summit_repository = $summit_repository;
|
|
$this->ticket_type_service = $ticket_type_service;
|
|
}
|
|
|
|
/**
|
|
* @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::DefaultPageSize, 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'), [
|
|
'name' => ['=@', '=='],
|
|
'description' => ['=@', '=='],
|
|
'external_id' => ['=@', '=='],
|
|
]);
|
|
}
|
|
|
|
if(is_null($filter)) $filter = new Filter();
|
|
|
|
$filter->validate([
|
|
'name' => 'sometimes|string',
|
|
'description' => 'sometimes|string',
|
|
'external_id' => 'sometimes|string',
|
|
]);
|
|
|
|
$order = null;
|
|
|
|
if (Input::has('order'))
|
|
{
|
|
$order = OrderParser::parse(Input::get('order'), [
|
|
'id',
|
|
'name',
|
|
'external_id'
|
|
]);
|
|
}
|
|
|
|
$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);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param $summit_id
|
|
* @return mixed
|
|
*/
|
|
public function getAllBySummitCSV($summit_id){
|
|
|
|
try {
|
|
|
|
$summit = SummitFinderStrategyFactory::build($this->summit_repository, $this->resource_server_context)->find($summit_id);
|
|
if (is_null($summit)) return $this->error404();
|
|
|
|
|
|
// default values
|
|
$page = 1;
|
|
$per_page = PHP_INT_MAX;
|
|
|
|
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' => ['=@', '=='],
|
|
'description' => ['=@', '=='],
|
|
'external_id' => ['=@', '=='],
|
|
]);
|
|
}
|
|
|
|
|
|
if(is_null($filter)) $filter = new Filter();
|
|
|
|
$filter->validate([
|
|
'name' => 'sometimes|string',
|
|
'description' => 'sometimes|string',
|
|
'external_id' => 'sometimes|string',
|
|
]);
|
|
|
|
$order = null;
|
|
|
|
if (Input::has('order'))
|
|
{
|
|
$order = OrderParser::parse(Input::get('order'), [
|
|
'id',
|
|
'name',
|
|
'external_id'
|
|
]);
|
|
}
|
|
|
|
$data = $this->repository->getBySummit($summit, new PagingInfo($page, $per_page), $filter, $order);
|
|
|
|
$filename = "ticket-types-" . date('Ymd');
|
|
$list = $data->toArray();
|
|
return $this->export
|
|
(
|
|
'csv',
|
|
$filename,
|
|
$list['data'],
|
|
[
|
|
'created' => new EpochCellFormatter,
|
|
'last_edited' => new EpochCellFormatter,
|
|
]
|
|
);
|
|
}
|
|
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);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param $summit_id
|
|
* @param $ticket_type_id
|
|
* @return mixed
|
|
*/
|
|
public function getTicketTypeBySummit($summit_id, $ticket_type_id){
|
|
try {
|
|
$summit = SummitFinderStrategyFactory::build($this->summit_repository, $this->resource_server_context)->find($summit_id);
|
|
if (is_null($summit)) return $this->error404();
|
|
$ticket_type = $summit->getTicketTypeById($ticket_type_id);
|
|
if(is_null($ticket_type))
|
|
return $this->error404();
|
|
return $this->ok(SerializerRegistry::getInstance()->getSerializer($ticket_type)->serialize( 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 (Exception $ex) {
|
|
Log::error($ex);
|
|
return $this->error500($ex);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param $summit_id
|
|
* @return mixed
|
|
*/
|
|
public function addTicketTypeBySummit($summit_id){
|
|
try {
|
|
|
|
if(!Request::isJson()) return $this->error400();
|
|
$data = Input::json();
|
|
$payload = $data->all();
|
|
$summit = SummitFinderStrategyFactory::build($this->summit_repository, $this->resource_server_context)->find($summit_id);
|
|
if (is_null($summit)) return $this->error404();
|
|
|
|
$rules = SummitTicketTypeValidationRulesFactory::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
|
|
);
|
|
}
|
|
|
|
$ticket_type = $this->ticket_type_service->addTicketType($summit, $payload);
|
|
|
|
return $this->created(SerializerRegistry::getInstance()->getSerializer($ticket_type)->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 $ticket_type_id
|
|
* @return mixed
|
|
*/
|
|
public function updateTicketTypeBySummit($summit_id, $ticket_type_id){
|
|
try {
|
|
|
|
if(!Request::isJson()) return $this->error400();
|
|
$data = Input::json();
|
|
$payload = $data->all();
|
|
$summit = SummitFinderStrategyFactory::build($this->summit_repository, $this->resource_server_context)->find($summit_id);
|
|
if (is_null($summit)) return $this->error404();
|
|
|
|
$rules = SummitTicketTypeValidationRulesFactory::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
|
|
);
|
|
}
|
|
|
|
$ticket_type = $this->ticket_type_service->updateTicketType($summit, $ticket_type_id, $payload);
|
|
|
|
return $this->updated(SerializerRegistry::getInstance()->getSerializer($ticket_type)->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 $ticket_type_id
|
|
* @return mixed
|
|
*/
|
|
public function deleteTicketTypeBySummit($summit_id, $ticket_type_id){
|
|
try {
|
|
|
|
$summit = SummitFinderStrategyFactory::build($this->summit_repository, $this->resource_server_context)->find($summit_id);
|
|
if (is_null($summit)) return $this->error404();
|
|
|
|
$this->ticket_type_service->deleteTicketType($summit, $ticket_type_id);
|
|
|
|
return $this->deleted();
|
|
}
|
|
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
|
|
* @return mixed
|
|
*/
|
|
public function seedDefaultTicketTypesBySummit($summit_id){
|
|
try {
|
|
|
|
$summit = SummitFinderStrategyFactory::build($this->summit_repository, $this->resource_server_context)->find($summit_id);
|
|
if (is_null($summit)) return $this->error404();
|
|
|
|
$ticket_types = $this->ticket_type_service->seedSummitTicketTypesFromEventBrite($summit);
|
|
|
|
$response = new PagingResponse
|
|
(
|
|
count($ticket_types),
|
|
count($ticket_types),
|
|
1,
|
|
1,
|
|
$ticket_types
|
|
);
|
|
|
|
return $this->created($response->toArray());
|
|
}
|
|
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);
|
|
}
|
|
}
|
|
|
|
} |