Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions app/Classes/Repositories/OrganisationRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions app/Classes/Repositories/OrganisationRepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 27 additions & 1 deletion app/Http/Controllers/OrganisationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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()]);

Expand Down
3 changes: 2 additions & 1 deletion routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand All @@ -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');
Expand Down
Loading