From d395d6e3f9b2f0b87a18248d8b7aa32a8e859101 Mon Sep 17 00:00:00 2001 From: Sebastian Marcet Date: Mon, 3 Sep 2018 15:40:35 -0300 Subject: [PATCH] Added new endpoints get organizations GET /api/v1/organizations params Change-Id: I86c864b6a861cdb28382c0cbdbcae405d82eb74f filter: name order: id, name --- .../Main/OAuth2OrganizationsApiController.php | 131 ++++++++++++++++++ app/Http/routes.php | 5 + .../Main/DoctrineOrganizationRepository.php | 21 +++ database/seeds/ApiEndpointsSeeder.php | 20 +++ database/seeds/ApiSeeder.php | 11 ++ tests/OAuth2OrganizationsApiTest.php | 44 ++++++ 6 files changed, 232 insertions(+) create mode 100644 app/Http/Controllers/Apis/Protected/Main/OAuth2OrganizationsApiController.php create mode 100644 tests/OAuth2OrganizationsApiTest.php diff --git a/app/Http/Controllers/Apis/Protected/Main/OAuth2OrganizationsApiController.php b/app/Http/Controllers/Apis/Protected/Main/OAuth2OrganizationsApiController.php new file mode 100644 index 00000000..0f406ddc --- /dev/null +++ b/app/Http/Controllers/Apis/Protected/Main/OAuth2OrganizationsApiController.php @@ -0,0 +1,131 @@ +repository = $company_repository; + } + + 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); + } + } +} \ No newline at end of file diff --git a/app/Http/routes.php b/app/Http/routes.php index 337ce7cc..c3d0a958 100644 --- a/app/Http/routes.php +++ b/app/Http/routes.php @@ -71,6 +71,11 @@ Route::group([ Route::get('', 'OAuth2CompaniesApiController@getAll'); }); + // organizations + Route::group(['prefix'=>'organizations'], function(){ + Route::get('', 'OAuth2OrganizationsApiController@getAll'); + }); + // groups Route::group(['prefix'=>'groups'], function(){ Route::get('', 'OAuth2GroupsApiController@getAll'); diff --git a/app/Repositories/Main/DoctrineOrganizationRepository.php b/app/Repositories/Main/DoctrineOrganizationRepository.php index 14f49ebd..0799e81d 100644 --- a/app/Repositories/Main/DoctrineOrganizationRepository.php +++ b/app/Repositories/Main/DoctrineOrganizationRepository.php @@ -30,4 +30,25 @@ final class DoctrineOrganizationRepository { return Organization::class; } + + /** + * @return array + */ + protected function getFilterMappings() + { + return [ + 'name' => 'e.name:json_string' + ]; + } + + /** + * @return array + */ + protected function getOrderMappings() + { + return [ + 'id' => 'e.id', + 'name' => 'e.name', + ]; + } } \ No newline at end of file diff --git a/database/seeds/ApiEndpointsSeeder.php b/database/seeds/ApiEndpointsSeeder.php index 9aa61ab3..6d604460 100644 --- a/database/seeds/ApiEndpointsSeeder.php +++ b/database/seeds/ApiEndpointsSeeder.php @@ -33,6 +33,7 @@ class ApiEndpointsSeeder extends Seeder $this->seedTagsEndpoints(); $this->seedCompaniesEndpoints(); $this->seedGroupsEndpoints(); + $this->seedOrganizationsEndpoints(); } /** @@ -1855,6 +1856,25 @@ class ApiEndpointsSeeder extends Seeder ); } + private function seedOrganizationsEndpoints(){ + $current_realm = Config::get('app.url'); + + $this->seedApiEndpoints('organizations', [ + // organizations + [ + 'name' => 'get-organizations', + 'route' => '/api/v1/organizations', + 'http_method' => 'GET', + 'scopes' => [ + sprintf(SummitScopes::ReadAllSummitData, $current_realm), + sprintf(SummitScopes::ReadSummitData, $current_realm), + sprintf('%s/organizations/read', $current_realm) + ], + ] + ] + ); + } + private function seedGroupsEndpoints(){ $current_realm = Config::get('app.url'); diff --git a/database/seeds/ApiSeeder.php b/database/seeds/ApiSeeder.php index 62e155d8..3eb62e23 100644 --- a/database/seeds/ApiSeeder.php +++ b/database/seeds/ApiSeeder.php @@ -98,5 +98,16 @@ final class ApiSeeder extends Seeder EntityManager::flush(); + // organizations + + $api = new Api(); + $api->setName('organizations'); + $api->setActive(true); + $api->setDescription('Organizations API'); + + EntityManager::persist($api); + + EntityManager::flush(); + } } \ No newline at end of file diff --git a/tests/OAuth2OrganizationsApiTest.php b/tests/OAuth2OrganizationsApiTest.php new file mode 100644 index 00000000..a1721219 --- /dev/null +++ b/tests/OAuth2OrganizationsApiTest.php @@ -0,0 +1,44 @@ + ['name=@tip'], + 'order' => '-id' + ]; + + $headers = array("HTTP_Authorization" => " Bearer " . $this->access_token); + $response = $this->action( + "GET", + "OAuth2OrganizationsApiController@getAll", + $params, + array(), + array(), + array(), + $headers + ); + + $content = $response->getContent(); + $organizations = json_decode($content); + $this->assertTrue(!is_null($organizations)); + $this->assertResponseStatus(200); + } + +} \ No newline at end of file