diff --git a/app/Classes/Repositories/OrganisationRepository.php b/app/Classes/Repositories/OrganisationRepository.php index a52068e..a2ebb38 100644 --- a/app/Classes/Repositories/OrganisationRepository.php +++ b/app/Classes/Repositories/OrganisationRepository.php @@ -40,6 +40,19 @@ public function all($columns = ['*']) return $this->orgModel->all($columns); } + /** + * @param bool $published + * @return \Illuminate\Database\Eloquent\Collection|static[] + */ + public function allByTranslationPublished($published) + { + return $this->orgModel + ->whereHas('details', function ($query) use ($published) { + $query->where('published', $published); + }) + ->get(); + } + /** * @param array $attributes * @return static diff --git a/app/Classes/Repositories/OrganisationRepositoryInterface.php b/app/Classes/Repositories/OrganisationRepositoryInterface.php index 4ceac9b..01d928d 100644 --- a/app/Classes/Repositories/OrganisationRepositoryInterface.php +++ b/app/Classes/Repositories/OrganisationRepositoryInterface.php @@ -6,6 +6,12 @@ interface OrganisationRepositoryInterface extends RepositoryInterface { + /** + * @param bool $published + * @return \Illuminate\Database\Eloquent\Collection|static[] + */ + public function allByTranslationPublished($published); + /** * @param $code * @return mixed diff --git a/app/Http/Controllers/OrganisationController.php b/app/Http/Controllers/OrganisationController.php index 85eef73..f53f6db 100644 --- a/app/Http/Controllers/OrganisationController.php +++ b/app/Http/Controllers/OrganisationController.php @@ -54,6 +54,13 @@ public function __construct( * summary="Get all organisations (public)", * security={{"ApiKeyAuth": {}}}, * tags={"Organisation"}, + * @OA\Parameter( + * name="published", + * in="query", + * required=false, + * description="Filter organisations by whether they have translations with published=true/false", + * @OA\Schema(type="boolean") + * ), * @OA\Response( * response=200, * description="Successful response", @@ -67,8 +74,27 @@ public function __construct( public function getAll(Request $request) { try { + $publishedParam = $request->query('published', null); + $publishedFilter = null; + + if (!is_null($publishedParam)) { + $publishedFilter = filter_var($publishedParam, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE); + + if (is_null($publishedFilter)) { + return response()->json([ + 'status' => 422, + 'error_message' => 'Invalid published filter. Use true or false.', + 'errors' => ['published query param must be a boolean'], + ], 422); + } + } + /** @var Collection $orgs */ - $orgs = $this->orgRepo->all()->load('details'); + if (!is_null($publishedFilter)) { + $orgs = $this->orgRepo->allByTranslationPublished($publishedFilter)->load('details'); + } else { + $orgs = $this->orgRepo->all()->load('details'); + } } catch (\Exception $e) { Log::error('Could not get Organisations list', ['message' => $e->getMessage()]); diff --git a/routes/api.php b/routes/api.php index 6555624..0c2b04b 100644 --- a/routes/api.php +++ b/routes/api.php @@ -20,6 +20,8 @@ Route::get('alerts', 'AlertController@get'); Route::get('org/{code}/alerts', 'AlertController@getByOrg'); Route::get('org/{code}/alerts/rss', 'AlertController@getRssByOrg'); + Route::get('org/', 'OrganisationController@getAll'); + Route::get('organisations', 'OrganisationController@getAll'); }); Route::group(['middleware' => 'BasicAuth', 'prefix' => config('app.api_version')], function () { @@ -32,7 +34,6 @@ 'prefix' => config('app.api_version'), ], function () { // Endpoints requiring API key authentication - Route::get('org/', 'OrganisationController@getAll'); Route::get('org/{code}', 'OrganisationController@getById'); Route::get('org/{code}/whatnow', 'WhatNowController@getFeed'); Route::get('whatnow/{id}', 'WhatNowController@getPublishedById');