diff --git a/app/Http/Controllers/OAuth2CompaniesApiController.php b/app/Http/Controllers/OAuth2CompaniesApiController.php new file mode 100644 index 00000000..48582376 --- /dev/null +++ b/app/Http/Controllers/OAuth2CompaniesApiController.php @@ -0,0 +1,133 @@ +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); + } + } + +} \ No newline at end of file diff --git a/app/Http/routes.php b/app/Http/routes.php index 5d516566..b3452049 100644 --- a/app/Http/routes.php +++ b/app/Http/routes.php @@ -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'); diff --git a/app/Models/Foundation/Main/Company.php b/app/Models/Foundation/Main/Company.php index b3c01840..8c5f913f 100644 --- a/app/Models/Foundation/Main/Company.php +++ b/app/Models/Foundation/Main/Company.php @@ -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; - } \ No newline at end of file diff --git a/app/Repositories/Main/DoctrineCompanyRepository.php b/app/Repositories/Main/DoctrineCompanyRepository.php index 89025a09..4535ba7d 100644 --- a/app/Repositories/Main/DoctrineCompanyRepository.php +++ b/app/Repositories/Main/DoctrineCompanyRepository.php @@ -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 */ diff --git a/database/seeds/ApiEndpointsSeeder.php b/database/seeds/ApiEndpointsSeeder.php index 585b2910..a8c14406 100644 --- a/database/seeds/ApiEndpointsSeeder.php +++ b/database/seeds/ApiEndpointsSeeder.php @@ -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'); diff --git a/database/seeds/ApiScopesSeeder.php b/database/seeds/ApiScopesSeeder.php index b2fb5141..3c52e7bf 100644 --- a/database/seeds/ApiScopesSeeder.php +++ b/database/seeds/ApiScopesSeeder.php @@ -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']); diff --git a/database/seeds/ApiSeeder.php b/database/seeds/ApiSeeder.php index 13bec71e..6af6fc5c 100644 --- a/database/seeds/ApiSeeder.php +++ b/database/seeds/ApiSeeder.php @@ -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(); diff --git a/tests/OAuth2CompaniesApiTest.php b/tests/OAuth2CompaniesApiTest.php new file mode 100644 index 00000000..0b4504ad --- /dev/null +++ b/tests/OAuth2CompaniesApiTest.php @@ -0,0 +1,44 @@ + ['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); + } + +} \ No newline at end of file