
POST /api/v1/organizations payload Change-Id: I713fcb70f380693b8b20a21a569064a0b3c3ed84 name: required|string|max:255
184 lines
5.5 KiB
PHP
184 lines
5.5 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\Services\Model\IOrganizationService;
|
|
use models\main\IOrganizationRepository;
|
|
use models\oauth2\IResourceServerContext;
|
|
use ModelSerializers\SerializerRegistry;
|
|
use utils\Filter;
|
|
use utils\FilterParser;
|
|
use utils\FilterParserException;
|
|
use utils\OrderParser;
|
|
use Illuminate\Support\Facades\Request;
|
|
use Illuminate\Support\Facades\Log;
|
|
use utils\PagingInfo;
|
|
use models\exceptions\EntityNotFoundException;
|
|
use models\exceptions\ValidationException;
|
|
use Illuminate\Support\Facades\Input;
|
|
use Illuminate\Support\Facades\Validator;
|
|
/**
|
|
* Class OAuth2OrganizationsApiController
|
|
* @package App\Http\Controllers
|
|
*/
|
|
final class OAuth2OrganizationsApiController extends OAuth2ProtectedController
|
|
{
|
|
/**
|
|
* @var IOrganizationService
|
|
*/
|
|
private $service;
|
|
|
|
/**
|
|
* OAuth2OrganizationsApiController constructor.
|
|
* @param IOrganizationRepository $company_repository
|
|
* @param IResourceServerContext $resource_server_context
|
|
* @param IOrganizationService $service
|
|
*/
|
|
public function __construct
|
|
(
|
|
IOrganizationRepository $company_repository,
|
|
IResourceServerContext $resource_server_context,
|
|
IOrganizationService $service
|
|
)
|
|
{
|
|
parent::__construct($resource_server_context);
|
|
$this->repository = $company_repository;
|
|
$this->service = $service;
|
|
}
|
|
|
|
public function getAll(){
|
|
|
|
$values = Input::all();
|
|
|
|
$rules = [
|
|
|
|
'page' => 'integer|min:1',
|
|
'per_page' => 'required_with:page|integer|min:5|max:100',
|
|
];
|
|
|
|
try {
|
|
|
|
$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' => ['=@', '=='],
|
|
]);
|
|
}
|
|
|
|
$order = null;
|
|
|
|
if (Input::has('order'))
|
|
{
|
|
$order = OrderParser::parse(Input::get('order'), [
|
|
|
|
'name',
|
|
'id',
|
|
]);
|
|
}
|
|
|
|
if(is_null($filter)) $filter = new Filter();
|
|
|
|
$data = $this->repository->getAllByPage(new PagingInfo($page, $per_page), $filter, $order);
|
|
$fields = Request::input('fields', '');
|
|
$fields = !empty($fields) ? explode(',', $fields) : [];
|
|
$relations = Request::input('relations', '');
|
|
$relations = !empty($relations) ? explode(',', $relations) : [];
|
|
|
|
return $this->ok
|
|
(
|
|
$data->toArray
|
|
(
|
|
Request::input('expand', ''),
|
|
$fields,
|
|
$relations
|
|
)
|
|
);
|
|
}
|
|
catch (EntityNotFoundException $ex1) {
|
|
Log::warning($ex1);
|
|
return $this->error404();
|
|
}
|
|
catch (ValidationException $ex2) {
|
|
Log::warning($ex2);
|
|
return $this->error412($ex2->getMessages());
|
|
}
|
|
catch(FilterParserException $ex3){
|
|
Log::warning($ex3);
|
|
return $this->error412($ex3->getMessages());
|
|
}
|
|
catch (\Exception $ex) {
|
|
Log::error($ex);
|
|
return $this->error500($ex);
|
|
}
|
|
}
|
|
|
|
|
|
public function addOrganization(){
|
|
try {
|
|
|
|
if(!Request::isJson()) return $this->error400();
|
|
|
|
$data = Input::json();
|
|
|
|
$rules = [
|
|
'name' => 'required|string|max:255',
|
|
];
|
|
|
|
// Creates a Validator instance and validates the data.
|
|
$validation = Validator::make($data->all(), $rules);
|
|
|
|
if ($validation->fails()) {
|
|
$messages = $validation->messages()->toArray();
|
|
|
|
return $this->error412
|
|
(
|
|
$messages
|
|
);
|
|
}
|
|
|
|
$organization = $this->service->addOrganization($data->all());
|
|
|
|
return $this->created(SerializerRegistry::getInstance()->getSerializer($organization)->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);
|
|
}
|
|
}
|
|
} |