diff --git a/legacy/app/Classes/Transformers/OrganisationTransformer.php b/legacy/app/Classes/Transformers/OrganisationTransformer.php new file mode 100644 index 0000000..74beac7 --- /dev/null +++ b/legacy/app/Classes/Transformers/OrganisationTransformer.php @@ -0,0 +1,58 @@ +unpublished = $configuration['unpublished']; + } + } + + /** + * Turn this item object into a generic array + * + * @param Organisation $model + * @return array + */ + public function transform(Organisation $model) + { + $response = [ + 'countryCode' => $model->country_code, + 'name' => $model->org_name, + 'url' => $model->attribution_url, + 'imageUrl' => $model->attribution_file_name ? $model->getAttributionImageUrl() : null, + 'translations' => null, + ]; + + if ($model->details->count()) { + $response['translations'] = []; + foreach ($model->details as $detail) { + if ($this->unpublished || $detail->published) { + $response['translations'][$detail->language_code] = [ + 'languageCode' => $detail->language_code, + 'name' => $detail->org_name, + 'attributionMessage' => $detail->attribution_message, + 'published' => (bool) $detail->published, + ]; + } + } + } + + return $response; + } +} + diff --git a/legacy/app/Http/Controllers/OrganisationController.php b/legacy/app/Http/Controllers/OrganisationController.php new file mode 100644 index 0000000..a26a66e --- /dev/null +++ b/legacy/app/Http/Controllers/OrganisationController.php @@ -0,0 +1,108 @@ +orgRepo = $orgRepo; + $this->request = $request; + $this->manager = $manager; + } + + /** + * @param Request $request + * @return \Symfony\Component\HttpFoundation\Response + */ + public function getAll(Request $request) + { + try { + /** @var Collection $orgs */ + $orgs = $this->orgRepo->all(); + } catch (\Exception $e) { + Log::error('Could not get Organisations list', ['message' => $e->getMessage()]); + return response()->json([ + 'status' => 500, + 'error_message' => 'Could not get Organisations list', + 'errors' => [], + ], 500); + } + + $orgs->each(function (Organisation $org) { + $org->load('details'); + }); + + $resource = new FractalCollection($orgs, new OrganisationTransformer([ + 'unpublished' => $request->header('x-api-key') ? false : true, + ])); + + $response = $this->manager->createData($resource); + return response()->json($response->toArray(), 200); + } + + /** + * @param string $code + * @param Request $request + * @return \Symfony\Component\HttpFoundation\Response + */ + public function getById($code, Request $request) + { + try { + /** @var Organisation $org */ + $org = $this->orgRepo->findByCountryCode($code); + } catch (\Exception $e) { + Log::error('Organisation not found', ['message' => $e->getMessage()]); + return response()->json([ + 'status' => 404, + 'error_message' => 'Organisation does not exist', + 'errors' => ['No matching organisation for country code'], + ], 404); + } + + $org->load('details'); + + $resource = new Item($org, new OrganisationTransformer([ + 'unpublished' => $request->header('x-api-key') ? false : true, + ])); + + $response = $this->manager->createData($resource); + return response()->json($response->toArray(), 200); + } +} + diff --git a/legacy/app/Models/Organisation.php b/legacy/app/Models/Organisation.php index e438fbe..709fa7e 100644 --- a/legacy/app/Models/Organisation.php +++ b/legacy/app/Models/Organisation.php @@ -24,7 +24,8 @@ class Organisation extends Model 'org_name', 'oid', 'attribution_url', - 'attribution_file_name' + 'attribution_file_name', + 'country_code', ]; public function alerts() diff --git a/routes/api.php b/routes/api.php index 766de6b..5874aac 100644 --- a/routes/api.php +++ b/routes/api.php @@ -95,7 +95,10 @@ }); Route::group(['middleware' => 'ApiAuth', 'prefix' => 'v1'], function () { + Route::get('org/', '\\App\\Legacy\\Http\\Controllers\\OrganisationController@getAll'); Route::get('org/{code}/whatnow', '\\App\\Legacy\\Http\\Controllers\\WhatNowController@getFeed'); + Route::get('org/{code}', '\\App\\Legacy\\Http\\Controllers\\OrganisationController@getById'); + Route::get('whatnow/{id}', '\\App\\Legacy\\Http\\Controllers\\WhatNowController@getPublishedById'); Route::any('{any}', function () { return response()->json([