Skip to content

Commit 86d7fa4

Browse files
Merge pull request #59 from IFRCGo/staging
Staging
2 parents 715ba2d + af0dfaa commit 86d7fa4

4 files changed

Lines changed: 171 additions & 1 deletion

File tree

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
namespace App\Legacy\Classes\Transformers;
4+
5+
use League\Fractal\TransformerAbstract;
6+
use App\Legacy\Models\Organisation;
7+
8+
class OrganisationTransformer extends TransformerAbstract
9+
{
10+
/**
11+
* @var bool
12+
*/
13+
private $unpublished = false;
14+
15+
/**
16+
* @param array $configuration
17+
*/
18+
public function __construct($configuration = [])
19+
{
20+
if (isset($configuration['unpublished']) && is_bool($configuration['unpublished'])) {
21+
$this->unpublished = $configuration['unpublished'];
22+
}
23+
}
24+
25+
/**
26+
* Turn this item object into a generic array
27+
*
28+
* @param Organisation $model
29+
* @return array
30+
*/
31+
public function transform(Organisation $model)
32+
{
33+
$response = [
34+
'countryCode' => $model->country_code,
35+
'name' => $model->org_name,
36+
'url' => $model->attribution_url,
37+
'imageUrl' => $model->attribution_file_name ? $model->getAttributionImageUrl() : null,
38+
'translations' => null,
39+
];
40+
41+
if ($model->details->count()) {
42+
$response['translations'] = [];
43+
foreach ($model->details as $detail) {
44+
if ($this->unpublished || $detail->published) {
45+
$response['translations'][$detail->language_code] = [
46+
'languageCode' => $detail->language_code,
47+
'name' => $detail->org_name,
48+
'attributionMessage' => $detail->attribution_message,
49+
'published' => (bool) $detail->published,
50+
];
51+
}
52+
}
53+
}
54+
55+
return $response;
56+
}
57+
}
58+
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
<?php
2+
3+
namespace App\Legacy\Http\Controllers;
4+
5+
use App\Legacy\Classes\Repositories\OrganisationRepositoryInterface;
6+
use App\Legacy\Classes\Transformers\OrganisationTransformer;
7+
use App\Legacy\Models\Organisation;
8+
use Illuminate\Database\Eloquent\Collection;
9+
use Illuminate\Http\Request;
10+
use Illuminate\Support\Facades\Log;
11+
use League\Fractal\Manager;
12+
use League\Fractal\Resource\Collection as FractalCollection;
13+
use League\Fractal\Resource\Item;
14+
15+
use App\Http\Controllers\Controller;
16+
17+
class OrganisationController extends Controller
18+
{
19+
/**
20+
* @var OrganisationRepositoryInterface
21+
*/
22+
protected $orgRepo;
23+
24+
/**
25+
* @var Request
26+
*/
27+
protected $request;
28+
29+
/**
30+
* @var Manager
31+
*/
32+
protected $manager;
33+
34+
/**
35+
* @param OrganisationRepositoryInterface $orgRepo
36+
* @param Request $request
37+
* @param Manager $manager
38+
*/
39+
public function __construct(
40+
OrganisationRepositoryInterface $orgRepo,
41+
Request $request,
42+
Manager $manager
43+
) {
44+
$this->orgRepo = $orgRepo;
45+
$this->request = $request;
46+
$this->manager = $manager;
47+
}
48+
49+
/**
50+
* @param Request $request
51+
* @return \Symfony\Component\HttpFoundation\Response
52+
*/
53+
public function getAll(Request $request)
54+
{
55+
try {
56+
/** @var Collection $orgs */
57+
$orgs = $this->orgRepo->all();
58+
} catch (\Exception $e) {
59+
Log::error('Could not get Organisations list', ['message' => $e->getMessage()]);
60+
return response()->json([
61+
'status' => 500,
62+
'error_message' => 'Could not get Organisations list',
63+
'errors' => [],
64+
], 500);
65+
}
66+
67+
$orgs->each(function (Organisation $org) {
68+
$org->load('details');
69+
});
70+
71+
$resource = new FractalCollection($orgs, new OrganisationTransformer([
72+
'unpublished' => $request->header('x-api-key') ? false : true,
73+
]));
74+
75+
$response = $this->manager->createData($resource);
76+
return response()->json($response->toArray(), 200);
77+
}
78+
79+
/**
80+
* @param string $code
81+
* @param Request $request
82+
* @return \Symfony\Component\HttpFoundation\Response
83+
*/
84+
public function getById($code, Request $request)
85+
{
86+
try {
87+
/** @var Organisation $org */
88+
$org = $this->orgRepo->findByCountryCode($code);
89+
} catch (\Exception $e) {
90+
Log::error('Organisation not found', ['message' => $e->getMessage()]);
91+
return response()->json([
92+
'status' => 404,
93+
'error_message' => 'Organisation does not exist',
94+
'errors' => ['No matching organisation for country code'],
95+
], 404);
96+
}
97+
98+
$org->load('details');
99+
100+
$resource = new Item($org, new OrganisationTransformer([
101+
'unpublished' => $request->header('x-api-key') ? false : true,
102+
]));
103+
104+
$response = $this->manager->createData($resource);
105+
return response()->json($response->toArray(), 200);
106+
}
107+
}
108+

legacy/app/Models/Organisation.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ class Organisation extends Model
2424
'org_name',
2525
'oid',
2626
'attribution_url',
27-
'attribution_file_name'
27+
'attribution_file_name',
28+
'country_code',
2829
];
2930

3031
public function alerts()

routes/api.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,10 @@
9595
});
9696

9797
Route::group(['middleware' => 'ApiAuth', 'prefix' => 'v1'], function () {
98+
Route::get('org/', '\\App\\Legacy\\Http\\Controllers\\OrganisationController@getAll');
9899
Route::get('org/{code}/whatnow', '\\App\\Legacy\\Http\\Controllers\\WhatNowController@getFeed');
100+
Route::get('org/{code}', '\\App\\Legacy\\Http\\Controllers\\OrganisationController@getById');
101+
Route::get('whatnow/{id}', '\\App\\Legacy\\Http\\Controllers\\WhatNowController@getPublishedById');
99102

100103
Route::any('{any}', function () {
101104
return response()->json([

0 commit comments

Comments
 (0)