Skip to content

Commit 7ace08f

Browse files
authored
Merge pull request #5 from IFRCGo/feature/WN-174
#WN-174# Adapt controllers: Backend endpoints: allow reordering; limit characters; manage status
2 parents c9c7085 + c5105f7 commit 7ace08f

10 files changed

+288
-12
lines changed
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php
2+
3+
namespace App\Classes\Repositories;
4+
5+
use App\Models\KeyMessage;
6+
7+
8+
class KeyMessageRepository implements KeyMessageRepositoryInterface
9+
{
10+
11+
/**
12+
* @var KeyMessage
13+
*/
14+
protected $keyMessageModel;
15+
16+
/**
17+
* @param KeyMessage $keyMessageModel
18+
*/
19+
public function __construct(KeyMessage $keyMessageModel)
20+
{
21+
$this->keyMessageModel = $keyMessageModel;
22+
}
23+
24+
/**
25+
* @param array $attributes
26+
* @return static
27+
*/
28+
public function newInstance(array $attributes = [])
29+
{
30+
return $this->keyMessageModel->newInstance($attributes);
31+
}
32+
33+
/**
34+
* @param array $columns
35+
* @return \Illuminate\Database\Eloquent\Collection|static[]
36+
*/
37+
public function all($columns = ['*'])
38+
{
39+
return $this->keyMessageModel->all($columns);
40+
}
41+
42+
/**
43+
* @param array $attributes
44+
* @return static
45+
*/
46+
public function create(array $attributes)
47+
{
48+
return $this->keyMessageModel->create($attributes);
49+
}
50+
51+
/**
52+
* @param $id
53+
* @param array $columns
54+
* @return mixed
55+
*/
56+
public function find($id, $columns = ['*'])
57+
{
58+
return $this->keyMessageModel->findOrFail($id, $columns);
59+
}
60+
61+
/**
62+
* @param $id
63+
* @return int
64+
*/
65+
public function destroy($id)
66+
{
67+
return $this->keyMessageModel->destroy($id);
68+
}
69+
70+
/**
71+
* @param $id
72+
* @param array $input
73+
* @return mixed
74+
*/
75+
public function updateWithIdAndInput($id, array $input)
76+
{
77+
return $this->keyMessageModel->where('id', $id)->update($input);
78+
}
79+
80+
81+
/**
82+
* @param $stageId
83+
* @return mixed
84+
*/
85+
public function findItemsByStageId($stageId)
86+
{
87+
return $this->keyMessageModel->where('stage_id', $stageId)->get();
88+
}
89+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace App\Classes\Repositories;
4+
5+
interface KeyMessageRepositoryInterface extends RepositoryInterface
6+
{
7+
8+
/**
9+
* @param $stageId
10+
* @return mixed
11+
*/
12+
public function findItemsByStageId($stageId);
13+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
3+
namespace App\Classes\Repositories;
4+
5+
use App\Models\SupportingMessage;
6+
7+
class SupportingMessageRepository implements SupportingMessageRepositoryInterface
8+
{
9+
10+
/**
11+
* @var SupportingMessage
12+
*/
13+
protected $suppotingMessageModel;
14+
15+
/**
16+
* @param SupportingMessage $model
17+
*/
18+
public function __construct(SupportingMessage $model)
19+
{
20+
$this->suppotingMessageModel = $model;
21+
}
22+
23+
/**
24+
* @param array $attributes
25+
* @return static
26+
*/
27+
public function newInstance(array $attributes = [])
28+
{
29+
return $this->suppotingMessageModel->newInstance($attributes);
30+
}
31+
32+
/**
33+
* @param array $columns
34+
* @return \Illuminate\Database\Eloquent\Collection|static[]
35+
*/
36+
public function all($columns = ['*'])
37+
{
38+
return $this->suppotingMessageModel->all($columns);
39+
}
40+
41+
/**
42+
* @param array $attributes
43+
* @return static
44+
*/
45+
public function create(array $attributes)
46+
{
47+
return $this->suppotingMessageModel->create($attributes);
48+
}
49+
50+
/**
51+
* @param $id
52+
* @param array $columns
53+
* @return mixed
54+
*/
55+
public function find($id, $columns = ['*'])
56+
{
57+
return $this->suppotingMessageModel->findOrFail($id, $columns);
58+
}
59+
60+
/**
61+
* @param $id
62+
* @return int
63+
*/
64+
public function destroy($id)
65+
{
66+
return $this->suppotingMessageModel->destroy($id);
67+
}
68+
69+
/**
70+
* @param $id
71+
* @param array $input
72+
* @return mixed
73+
*/
74+
public function updateWithIdAndInput($id, array $input)
75+
{
76+
return $this->suppotingMessageModel->where('id', $id)->update($input);
77+
}
78+
79+
public function findItemsByKeyMessageId($keyMessageId)
80+
{
81+
return $this->suppotingMessageModel->where('key_message_id', $keyMessageId)->get();
82+
}
83+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace App\Classes\Repositories;
4+
5+
interface SupportingMessageRepositoryInterface extends RepositoryInterface
6+
{
7+
8+
/**
9+
* @param $keyMessageId
10+
* @return mixed
11+
*/
12+
public function findItemsByKeyMessageId($keyMessageId);
13+
}

app/Classes/Repositories/WhatNowRepository.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,15 @@
99
class WhatNowRepository implements WhatNowRepositoryInterface
1010
{
1111
public const EVENT_STAGES = [
12-
'mitigation',
13-
'seasonalForecast',
14-
'watch',
1512
'warning',
1613
'immediate',
1714
'recover',
15+
'anticipated',
16+
'assess_and_plan',
17+
'mitigate_risks',
18+
'prepare_to_respond',
19+
20+
1821
];
1922

2023
/**

app/Classes/Repositories/WhatNowTranslationRepository.php

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,20 @@ class WhatNowTranslationRepository implements WhatNowTranslationRepositoryInterf
1515
*/
1616
protected $whatNowTranslationModel;
1717

18+
/** @var KeyMessageRepositoryInterface */
19+
protected $keyMessageRepository;
20+
21+
/** @var SupportingMessageRepositoryInterface */
22+
protected $supportingMessageRepository;
23+
1824
/**
1925
* @param WhatNowEntityTranslation $whatNowTranslationModel
2026
*/
21-
public function __construct(WhatNowEntityTranslation $whatNowTranslationModel)
27+
public function __construct(WhatNowEntityTranslation $whatNowTranslationModel, KeyMessageRepositoryInterface $keyMessageRepository, SupportingMessageRepositoryInterface $supportingMessageRepository)
2228
{
2329
$this->whatNowTranslationModel = $whatNowTranslationModel;
30+
$this->keyMessageRepository = $keyMessageRepository;
31+
$this->supportingMessageRepository = $supportingMessageRepository;
2432
}
2533

2634
/**
@@ -73,11 +81,23 @@ public function addTranslations(WhatNowEntity $entity, array $translations)
7381
if (empty($transStage)) {
7482
if (! empty($content)) {
7583
try {
76-
$translation->stages()->create([
84+
$stage = $translation->stages()->create([
7785
'language_code' => $trans['lang'],
7886
'stage' => $stage,
79-
'content' => json_encode($content),
8087
]);
88+
foreach ($content as $message) {
89+
$keyMessage = $this->keyMessageRepository->create([
90+
'entities_stage_id' => $stage->id,
91+
'title' => $message['title'],
92+
]);
93+
94+
foreach ($message['content'] as $supportMessage) {
95+
$this->supportingMessageRepository->create([
96+
'key_message_id' => $keyMessage->id,
97+
'content' => $supportMessage,
98+
]);
99+
}
100+
}
81101
} catch (\Exception $e) {
82102
Log::error('ERROR', [$e->getMessage()]);
83103
}

app/Classes/Transformers/WhatNowEntityTransformer.php

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class WhatNowEntityTransformer extends TransformerAbstract
2828
* @param WhatNowTranslationRepositoryInterface $repo
2929
* @param array $configuration
3030
*/
31+
3132
function __construct(WhatNowTranslationRepositoryInterface $repo, $configuration = [])
3233
{
3334
if(isset($configuration['unpublished']) && is_bool($configuration['unpublished'])) {
@@ -70,7 +71,7 @@ public function transform(WhatNowEntity $model)
7071
'url' => $model->organisation->attribution_url,
7172
'imageUrl' => $model->organisation->attribution_file_name ? $model->organisation->getAttributionImageUrl() : null,
7273
'translations' => null
73-
]
74+
],
7475
];
7576

7677
if ($model->organisation->details->count()) {
@@ -88,7 +89,7 @@ public function transform(WhatNowEntity $model)
8889
}
8990

9091
if ($this->unpublished) {
91-
$translations = $this->wnTransRepo->getLatestTranslations($model->id);
92+
$translations = $this->wnTransRepo->getLatestTranslations($model->id) ?? [];
9293
} else {
9394
$translations = $this->wnTransRepo->getLatestPublishedTranslations($model->id, $this->lang);
9495
}
@@ -99,14 +100,28 @@ public function transform(WhatNowEntity $model)
99100
}
100101

101102
if ($translations) {
102-
103+
$response['translations'] = [];
103104
/** @var WhatNowEntityTranslation $trans */
104105
foreach ($translations as $trans) {
105106

106107
$stages = $defaultStages;
107108
if($trans->stages){
108109
foreach ($trans->stages as $stage) {
109-
$stages[$stage->stage] = json_decode($stage['content']);
110+
$stage->load('keyMessages');
111+
$keyMessages = $stage->keyMessages;
112+
113+
foreach ($keyMessages as $keyMessage) {
114+
$keyMessage->load('supportingMessages');
115+
}
116+
117+
$stages[$stage->stage] = $keyMessages->map(function($keyMessage) {
118+
return [
119+
'title' => $keyMessage->title,
120+
'content' => $keyMessage->supportingMessages->map(function($supportingMessage) {
121+
return $supportingMessage->content;
122+
})->toArray()
123+
];
124+
})->toArray();
110125
}
111126
}
112127

app/Providers/RepositoryServiceProvider.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,13 @@ public function register()
4242
'App\Classes\Repositories\WhatNowTranslationRepositoryInterface',
4343
'App\Classes\Repositories\WhatNowTranslationRepository'
4444
);
45+
$this->app->bind(
46+
'App\Classes\Repositories\KeyMessageRepositoryInterface',
47+
'App\Classes\Repositories\KeyMessageRepository'
48+
);
49+
$this->app->bind(
50+
'App\Classes\Repositories\SupportingMessageRepositoryInterface',
51+
'App\Classes\Repositories\SupportingMessageRepository'
52+
);
4553
}
4654
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Support\Facades\DB;
5+
use Illuminate\Database\Schema\Blueprint;
6+
use Illuminate\Support\Facades\Schema;
7+
8+
class UpdateEnumStageInWhatnowEntityStagesTable extends Migration
9+
{
10+
/**
11+
* Run the migrations.
12+
*
13+
* @return void
14+
*/
15+
public function up()
16+
{
17+
Schema::table('whatnow_entity_stages', function (Blueprint $table) {
18+
DB::statement("ALTER TABLE whatnow_entity_stages MODIFY COLUMN stage ENUM('immediate', 'warning', 'anticipated', 'assess_and_plan', 'mitigate_risks', 'prepare_to_respond', 'recover') NOT NULL");
19+
});
20+
}
21+
22+
/**
23+
* Reverse the migrations.
24+
*
25+
* @return void
26+
*/
27+
public function down()
28+
{
29+
Schema::table('whatnow_entity_stages', function (Blueprint $table) {
30+
DB::statement("ALTER TABLE whatnow_entity_stages MODIFY COLUMN stage ENUM('immediate', 'warning', 'anticipated', 'assess_and_plan', 'mitigate_risks', 'prepare_to_respond', 'recover') NOT NULL");
31+
});
32+
}
33+
}

database/seeds/WhatNowTableSeeder.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,7 @@ public function run()
5858
'id' => $row[0],
5959
'translation_id' => $row[1],
6060
'language_code' => $row[2],
61-
'stage' => $row[3],
62-
'content' => json_encode(json_decode($row[4])),
61+
'stage' => $row[3]
6362
]);
6463

6564
}

0 commit comments

Comments
 (0)