Skip to content

Commit 87603b9

Browse files
author
LDFOUR\luisd
committed
WN-252,
I added the Contributor model with a relationship for each OrganizationDetail to maintain the translation of each contributor. I updated the GET and PUT endpoints for organizations.
1 parent 453fbad commit 87603b9

7 files changed

Lines changed: 243 additions & 4 deletions

File tree

app/Classes/Repositories/OrganisationRepository.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace App\Classes\Repositories;
44

55
use App\Models\Organisation;
6+
use App\Models\Contributor;
67
use Illuminate\Support\Facades\DB;
78
use Illuminate\Support\Facades\Log;
89

@@ -87,16 +88,31 @@ public function updateDetailsWithInput(Organisation $org, array $input)
8788
if (count($input['translations'])) {
8889
DB::transaction(function () use ($org, $input) {
8990
$org->details()->delete();
90-
91+
9192
foreach ($input['translations'] as $lang => $data) {
92-
$org->details()->updateOrCreate([
93+
$currentDetail = $org->details()->updateOrCreate([
9394
'language_code' => strtolower($lang),
9495
], [
9596
'language_code' => strtolower($lang),
9697
'org_name' => $data['name'] ?? '',
9798
'attribution_message' => $data['attributionMessage'] ?? '',
9899
'published' => $data['published'] ?? false,
99100
]);
101+
102+
if (array_key_exists('contributors', $data)) {
103+
$currentDetail->contributors()->delete();
104+
105+
foreach ($data['contributors'] as $contributor) {
106+
$contributor = new Contributor([
107+
'name' => $contributor['name'],
108+
'logo' => $contributor['logo'],
109+
]);
110+
111+
$contributor->validate($contributor->toArray());
112+
113+
$currentDetail->contributors()->save($contributor);
114+
}
115+
}
100116
}
101117
});
102118
}

app/Classes/Transformers/OrganisationTransformer.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,21 +37,37 @@ public function transform(Organisation $model)
3737
'name' => $model->org_name,
3838
'url' => $model->attribution_url,
3939
'imageUrl' => $model->attribution_file_name ? $model->getAttributionImageUrl() : null,
40-
'translations' => null
40+
'translations' => null,
4141
];
4242

4343
if ($model->details->count()) {
4444
$response['translations'] = [];
4545

4646
foreach ($model->details as $detail) {
4747

48+
$model->details->each(function ($detail) {
49+
$detail->load('contributors');
50+
});
51+
4852
if($this->unpublished || $detail->published) {
4953
$response['translations'][$detail->language_code] = [
5054
'languageCode' => $detail->language_code,
5155
'name' => $detail->org_name,
5256
'attributionMessage' => $detail->attribution_message,
5357
];
5458

59+
$response['translations'][$detail->language_code]['contributors'] = [];
60+
if ($detail->contributors->count()) {
61+
62+
$response['translations'][$detail->language_code]['contributors'] = $detail->contributors->map(function ($contributor) {
63+
return [
64+
'id' => $contributor->id,
65+
'name' => $contributor->name,
66+
'logo' => $contributor->logo,
67+
];
68+
});
69+
}
70+
5571
$response['translations'][$detail->language_code]['published'] = (bool) $detail->published;
5672
}
5773
}

app/Classes/Transformers/WhatNowEntityTransformer.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,20 +70,34 @@ public function transform(WhatNowEntity $model)
7070
'countryCode' => $model->organisation->country_code,
7171
'url' => $model->organisation->attribution_url,
7272
'imageUrl' => $model->organisation->attribution_file_name ? $model->organisation->getAttributionImageUrl() : null,
73-
'translations' => null
73+
'translations' => null,
7474
],
7575
];
7676

7777
if ($model->organisation->details->count()) {
7878
$response['attribution']['translations'] = [];
7979

8080
foreach ($model->organisation->details as $detail) {
81+
$model->organisation->details->each(function ($detail) {
82+
$detail->load('contributors');
83+
});
8184
$response['attribution']['translations'][$detail->language_code] = [
8285
'languageCode' => $detail->language_code,
8386
'name' => $detail->org_name,
8487
'attributionMessage' => $detail->attribution_message,
88+
'contributors' => []
8589
];
8690

91+
if ($detail->contributors->count()) {
92+
$response['attribution']['translations'][$detail->language_code]['contributors'] = $detail->contributors->map(function ($contributor) {
93+
return [
94+
'id' => $contributor->id,
95+
'name' => $contributor->name,
96+
'logo' => $contributor->logo,
97+
];
98+
});
99+
}
100+
87101
$response['attribution']['translations'][$detail->language_code]['published'] = (bool) $detail->published;
88102
}
89103
}

app/Http/Controllers/OrganisationController.php

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@
1111
use Illuminate\Support\Facades\Storage;
1212
use League\Fractal\Manager;
1313

14+
/**
15+
* @OA\Tag(
16+
* name="Organisation",
17+
* description="Operations about Organisations"
18+
* )
19+
*/
1420
class OrganisationController extends Controller
1521
{
1622
/**
@@ -101,6 +107,77 @@ public function getById($code, Request $request)
101107
/**
102108
* @param $code
103109
* @return \Symfony\Component\HttpFoundation\Response
110+
*/
111+
/**
112+
* @OA\Put(
113+
* path="/organisation/{code}",
114+
* summary="Update organisation by country code",
115+
* tags={"Organisation"},
116+
* @OA\Parameter(
117+
* name="code",
118+
* in="path",
119+
* description="Country code of the organisation",
120+
* required=true,
121+
* @OA\Schema(type="string")
122+
* ),
123+
* @OA\RequestBody(
124+
* required=true,
125+
* @OA\JsonContent(
126+
* type="object",
127+
* @OA\Property(property="countryCode", type="string", example="USA"),
128+
* @OA\Property(property="name", type="string", example="American Red Cross"),
129+
* @OA\Property(property="url", type="string", nullable=true, example=null),
130+
* @OA\Property(
131+
* property="translations",
132+
* type="array",
133+
* @OA\Items(
134+
* type="object",
135+
* @OA\Property(property="languageCode", type="string", example="en"),
136+
* @OA\Property(property="name", type="string", example="Organization name"),
137+
* @OA\Property(property="attributionMessage", type="string", example="Attribution Message"),
138+
* @OA\Property(property="published", type="boolean", example=true),
139+
* @OA\Property(
140+
* property="contributors",
141+
* type="array",
142+
* @OA\Items(
143+
* type="object",
144+
* @OA\Property(property="name", type="string", example="Contributor name"),
145+
* @OA\Property(property="logo", type="string", example="logo.png")
146+
* )
147+
* )
148+
* )
149+
* )
150+
* )
151+
* ),
152+
* @OA\Response(
153+
* response=200,
154+
* description="Successful response",
155+
* @OA\JsonContent(
156+
* type="object",
157+
* @OA\Property(property="data", type="object")
158+
* )
159+
* ),
160+
* @OA\Response(
161+
* response=404,
162+
* description="Organisation not found",
163+
* @OA\JsonContent(
164+
* type="object",
165+
* @OA\Property(property="status", type="integer", example=404),
166+
* @OA\Property(property="error_message", type="string", example="Organisation does not exist"),
167+
* @OA\Property(property="errors", type="array", @OA\Items(type="string"))
168+
* )
169+
* ),
170+
* @OA\Response(
171+
* response=500,
172+
* description="Organisation could not be updated",
173+
* @OA\JsonContent(
174+
* type="object",
175+
* @OA\Property(property="status", type="integer", example=500),
176+
* @OA\Property(property="error_message", type="string", example="Organisation could not be updated"),
177+
* @OA\Property(property="errors", type="array", @OA\Items(type="string"))
178+
* )
179+
* )
180+
* )
104181
*/
105182
public function putById($code)
106183
{

app/Models/Contributor.php

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
3+
namespace App\Models;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
use Illuminate\Support\Facades\Validator;
7+
8+
class Contributor extends Model
9+
{
10+
11+
/**
12+
* table
13+
*
14+
* @var string
15+
*/
16+
protected $table = 'contributors';
17+
18+
19+
/**
20+
* @var
21+
*/
22+
protected $errors;
23+
24+
/**
25+
* @var bool
26+
*/
27+
public $timestamps = false;
28+
29+
/**
30+
* The attributes that are mass assignable.
31+
* @var array
32+
*/
33+
protected $fillable = ['name', 'logo', 'org_detail_id'];
34+
35+
/**
36+
* Model validation rules
37+
* @var array
38+
*/
39+
protected $rules = [
40+
'name' => 'required|string|between:2,100',
41+
'logo' => 'string|between:2,100',
42+
'org_id' => 'required|integer',
43+
];
44+
45+
46+
/**
47+
* @param $data
48+
* @return bool
49+
*/
50+
public function validate($data)
51+
{
52+
$v = Validator::make($data, $this->rules);
53+
54+
if ($v->fails()) {
55+
$this->errors = $v->errors();
56+
57+
return false;
58+
}
59+
60+
return true;
61+
}
62+
63+
/**
64+
* @return mixed
65+
*/
66+
public function errors()
67+
{
68+
return $this->errors;
69+
}
70+
71+
public function organisation()
72+
{
73+
return $this->belongsTo(OrganisationDetails::class, 'org_detail_id');
74+
}
75+
76+
}

app/Models/OrganisationDetails.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,10 @@ public function organisation()
3434
{
3535
return $this->belongsTo('App\Models\Details', 'org_id', 'id');
3636
}
37+
38+
public function contributors()
39+
{
40+
return $this->hasMany(Contributor::class, 'org_detail_id');
41+
}
3742
}
3843

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
class CreateContributorsTable extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::dropIfExists('contributors');
17+
Schema::create('contributors', function (Blueprint $table) {
18+
$table->increments('id');
19+
$table->text('name');
20+
$table->text('logo');
21+
$table->integer('org_detail_id')->unsigned();
22+
$table->foreign('org_detail_id')->references('id')->on('organisation_details')->onDelete('cascade');
23+
});
24+
}
25+
26+
/**
27+
* Reverse the migrations.
28+
*
29+
* @return void
30+
*/
31+
public function down()
32+
{
33+
Schema::dropIfExists('contributors');
34+
}
35+
}

0 commit comments

Comments
 (0)