Added new get companies endpoint
GET api/v1/companies params: filter ** name ( available operators : [==, =@] order ** id ** name Change-Id: Ief26f6ac5ce9c584fbf4dea439cc6ed21221233f
This commit is contained in:
parent
d2fbe6be07
commit
56b33f3926
133
app/Http/Controllers/OAuth2CompaniesApiController.php
Normal file
133
app/Http/Controllers/OAuth2CompaniesApiController.php
Normal file
@ -0,0 +1,133 @@
|
||||
<?php namespace App\Http\Controllers;
|
||||
/**
|
||||
* 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\main\ICompanyRepository;
|
||||
use models\oauth2\IResourceServerContext;
|
||||
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 OAuth2CompaniesApiController
|
||||
* @package App\Http\Controllers
|
||||
*/
|
||||
final class OAuth2CompaniesApiController extends OAuth2ProtectedController
|
||||
{
|
||||
/**
|
||||
* OAuth2MembersApiController constructor.
|
||||
* @param ICompanyRepository $company_repository
|
||||
* @param IResourceServerContext $resource_server_context
|
||||
*/
|
||||
public function __construct
|
||||
(
|
||||
ICompanyRepository $company_repository,
|
||||
IResourceServerContext $resource_server_context
|
||||
)
|
||||
{
|
||||
parent::__construct($resource_server_context);
|
||||
$this->repository = $company_repository;
|
||||
}
|
||||
|
||||
public function getCompanies(){
|
||||
|
||||
$values = Input::all();
|
||||
|
||||
$rules = array
|
||||
(
|
||||
'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'), array
|
||||
(
|
||||
'name' => ['=@', '=='],
|
||||
));
|
||||
}
|
||||
|
||||
$order = null;
|
||||
|
||||
if (Input::has('order'))
|
||||
{
|
||||
$order = OrderParser::parse(Input::get('order'), array
|
||||
(
|
||||
'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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -107,6 +107,11 @@ Route::group([
|
||||
Route::get('', 'OAuth2TagsApiController@getTags');
|
||||
});
|
||||
|
||||
// companies
|
||||
Route::group(['prefix'=>'companies'], function(){
|
||||
Route::get('', 'OAuth2CompaniesApiController@getCompanies');
|
||||
});
|
||||
|
||||
// teams
|
||||
Route::group(['prefix'=>'teams'], function(){
|
||||
Route::get('', 'OAuth2TeamsApiController@getMyTeams');
|
||||
|
@ -25,6 +25,16 @@ use models\utils\SilverstripeBaseModel;
|
||||
*/
|
||||
class Company extends SilverstripeBaseModel
|
||||
{
|
||||
/**
|
||||
* @ORM\Column(name="Name", type="string")
|
||||
*/
|
||||
private $name;
|
||||
|
||||
/**
|
||||
* @ORM\ManyToMany(targetEntity="models\summit\SummitEvent", mappedBy="sponsors")
|
||||
*/
|
||||
private $sponsorships;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
@ -47,14 +57,4 @@ class Company extends SilverstripeBaseModel
|
||||
$this->name = $name;
|
||||
}
|
||||
|
||||
/**
|
||||
* @ORM\Column(name="Name", type="string")
|
||||
*/
|
||||
private $name;
|
||||
|
||||
/**
|
||||
* @ORM\ManyToMany(targetEntity="models\summit\SummitEvent", mappedBy="sponsors")
|
||||
*/
|
||||
private $sponsorships;
|
||||
|
||||
}
|
@ -24,6 +24,27 @@ final class DoctrineCompanyRepository
|
||||
implements ICompanyRepository
|
||||
{
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
protected function getFilterMappings()
|
||||
{
|
||||
return [
|
||||
'name' => 'e.name:json_string'
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
protected function getOrderMappings()
|
||||
{
|
||||
return [
|
||||
'id' => 'e.id',
|
||||
'name' => 'e.name',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
|
@ -32,6 +32,7 @@ class ApiEndpointsSeeder extends Seeder
|
||||
$this->seedMemberEndpoints();
|
||||
$this->seedTeamEndpoints();
|
||||
$this->seedTagsEndpoints();
|
||||
$this->seedCompaniesEndpoints();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -493,6 +494,24 @@ class ApiEndpointsSeeder extends Seeder
|
||||
);
|
||||
}
|
||||
|
||||
private function seedCompaniesEndpoints(){
|
||||
$current_realm = Config::get('app.url');
|
||||
|
||||
$this->seedApiEndpoints('companies', [
|
||||
// members
|
||||
array(
|
||||
'name' => 'get-companies',
|
||||
'route' => '/api/v1/companies',
|
||||
'http_method' => 'GET',
|
||||
'scopes' => [
|
||||
sprintf('%s/summits/read', $current_realm),
|
||||
sprintf('%s/companies/read', $current_realm)
|
||||
],
|
||||
)
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
private function seedTeamEndpoints(){
|
||||
$current_realm = Config::get('app.url');
|
||||
|
||||
|
@ -32,6 +32,8 @@ final class ApiScopesSeeder extends Seeder
|
||||
$this->seedSummitScopes();
|
||||
$this->seedMembersScopes();
|
||||
$this->seedTeamsScopes();
|
||||
$this->seedTagsScopes();
|
||||
$this->seedCompaniesScopes();
|
||||
}
|
||||
|
||||
private function seedSummitScopes()
|
||||
@ -185,6 +187,33 @@ final class ApiScopesSeeder extends Seeder
|
||||
EntityManager::flush();
|
||||
}
|
||||
|
||||
|
||||
private function seedCompaniesScopes(){
|
||||
$current_realm = Config::get('app.url');
|
||||
$api = EntityManager::getRepository(\App\Models\ResourceServer\Api::class)->findOneBy(['name' => 'companies']);
|
||||
|
||||
$scopes = [
|
||||
array(
|
||||
'name' => sprintf('%s/companies/read', $current_realm),
|
||||
'short_description' => 'Get Companies Data',
|
||||
'description' => 'Grants read only access for Companies Data',
|
||||
),
|
||||
];
|
||||
|
||||
foreach ($scopes as $scope_info) {
|
||||
$scope = new ApiScope();
|
||||
$scope->setName($scope_info['name']);
|
||||
$scope->setShortDescription($scope_info['short_description']);
|
||||
$scope->setDescription($scope_info['description']);
|
||||
$scope->setActive(true);
|
||||
$scope->setDefault(false);
|
||||
$scope->setApi($api);
|
||||
EntityManager::persist($scope);
|
||||
}
|
||||
|
||||
EntityManager::flush();
|
||||
}
|
||||
|
||||
private function seedTeamsScopes(){
|
||||
$current_realm = Config::get('app.url');
|
||||
$api = EntityManager::getRepository(\App\Models\ResourceServer\Api::class)->findOneBy(['name' => 'teams']);
|
||||
|
@ -64,6 +64,18 @@ final class ApiSeeder extends Seeder
|
||||
|
||||
EntityManager::flush();
|
||||
|
||||
//tags
|
||||
|
||||
$api = new Api();
|
||||
$api->setName('companies');
|
||||
$api->setActive(true);
|
||||
$api->setDescription('companies API');
|
||||
|
||||
EntityManager::persist($api);
|
||||
|
||||
EntityManager::flush();
|
||||
|
||||
|
||||
// teams
|
||||
|
||||
$api = new Api();
|
||||
|
44
tests/OAuth2CompaniesApiTest.php
Normal file
44
tests/OAuth2CompaniesApiTest.php
Normal file
@ -0,0 +1,44 @@
|
||||
<?php
|
||||
/**
|
||||
* 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.
|
||||
**/
|
||||
|
||||
class OAuth2CompaniesApiTest extends ProtectedApiTest
|
||||
{
|
||||
|
||||
public function testGetCompanies()
|
||||
{
|
||||
|
||||
$params = [
|
||||
//AND FILTER
|
||||
'filter' => ['name=@tip'],
|
||||
'order' => '-id'
|
||||
];
|
||||
|
||||
$headers = array("HTTP_Authorization" => " Bearer " . $this->access_token);
|
||||
$response = $this->action(
|
||||
"GET",
|
||||
"OAuth2CompaniesApiController@getCompanies",
|
||||
$params,
|
||||
array(),
|
||||
array(),
|
||||
array(),
|
||||
$headers
|
||||
);
|
||||
|
||||
$content = $response->getContent();
|
||||
$companies = json_decode($content);
|
||||
$this->assertTrue(!is_null($companies));
|
||||
$this->assertResponseStatus(200);
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user