Skip to content

Commit 10df2f1

Browse files
Merge pull request #64 from IFRCGo/WN-359
Filter organisations by translation published
2 parents a9b444c + 5b6cbb3 commit 10df2f1

4 files changed

Lines changed: 48 additions & 2 deletions

File tree

app/Classes/Repositories/OrganisationRepository.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,19 @@ public function all($columns = ['*'])
4040
return $this->orgModel->all($columns);
4141
}
4242

43+
/**
44+
* @param bool $published
45+
* @return \Illuminate\Database\Eloquent\Collection|static[]
46+
*/
47+
public function allByTranslationPublished($published)
48+
{
49+
return $this->orgModel
50+
->whereHas('details', function ($query) use ($published) {
51+
$query->where('published', $published);
52+
})
53+
->get();
54+
}
55+
4356
/**
4457
* @param array $attributes
4558
* @return static

app/Classes/Repositories/OrganisationRepositoryInterface.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@
66

77
interface OrganisationRepositoryInterface extends RepositoryInterface
88
{
9+
/**
10+
* @param bool $published
11+
* @return \Illuminate\Database\Eloquent\Collection|static[]
12+
*/
13+
public function allByTranslationPublished($published);
14+
915
/**
1016
* @param $code
1117
* @return mixed

app/Http/Controllers/OrganisationController.php

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,13 @@ public function __construct(
5454
* summary="Get all organisations (public)",
5555
* security={{"ApiKeyAuth": {}}},
5656
* tags={"Organisation"},
57+
* @OA\Parameter(
58+
* name="published",
59+
* in="query",
60+
* required=false,
61+
* description="Filter organisations by whether they have translations with published=true/false",
62+
* @OA\Schema(type="boolean")
63+
* ),
5764
* @OA\Response(
5865
* response=200,
5966
* description="Successful response",
@@ -67,8 +74,27 @@ public function __construct(
6774
public function getAll(Request $request)
6875
{
6976
try {
77+
$publishedParam = $request->query('published', null);
78+
$publishedFilter = null;
79+
80+
if (!is_null($publishedParam)) {
81+
$publishedFilter = filter_var($publishedParam, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
82+
83+
if (is_null($publishedFilter)) {
84+
return response()->json([
85+
'status' => 422,
86+
'error_message' => 'Invalid published filter. Use true or false.',
87+
'errors' => ['published query param must be a boolean'],
88+
], 422);
89+
}
90+
}
91+
7092
/** @var Collection $orgs */
71-
$orgs = $this->orgRepo->all()->load('details');
93+
if (!is_null($publishedFilter)) {
94+
$orgs = $this->orgRepo->allByTranslationPublished($publishedFilter)->load('details');
95+
} else {
96+
$orgs = $this->orgRepo->all()->load('details');
97+
}
7298
} catch (\Exception $e) {
7399
Log::error('Could not get Organisations list', ['message' => $e->getMessage()]);
74100

routes/api.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
Route::get('alerts', 'AlertController@get');
2121
Route::get('org/{code}/alerts', 'AlertController@getByOrg');
2222
Route::get('org/{code}/alerts/rss', 'AlertController@getRssByOrg');
23+
Route::get('org/', 'OrganisationController@getAll');
24+
Route::get('organisations', 'OrganisationController@getAll');
2325
});
2426

2527
Route::group(['middleware' => 'BasicAuth', 'prefix' => config('app.api_version')], function () {
@@ -32,7 +34,6 @@
3234
'prefix' => config('app.api_version'),
3335
], function () {
3436
// Endpoints requiring API key authentication
35-
Route::get('org/', 'OrganisationController@getAll');
3637
Route::get('org/{code}', 'OrganisationController@getById');
3738
Route::get('org/{code}/whatnow', 'WhatNowController@getFeed');
3839
Route::get('whatnow/{id}', 'WhatNowController@getPublishedById');

0 commit comments

Comments
 (0)