Skip to content

Commit

Permalink
Merge pull request #164 from IFRCGo/feature/WN-146
Browse files Browse the repository at this point in the history
WN-146
  • Loading branch information
l-dufour authored Feb 23, 2025
2 parents e530a99 + 9f4f1ff commit f2c19b2
Show file tree
Hide file tree
Showing 14 changed files with 1,039 additions and 351 deletions.
10 changes: 10 additions & 0 deletions app/Classes/RcnApi/Entities/Organisation.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@ class Organisation implements \JsonSerializable, Arrayable

protected $translations = [];

protected $imageUrl;


public static function createFromArray(array $array)
{
$attribution = new self();
$attribution->name = $array['name'];
$attribution->countryCode = $array['countryCode'];
$attribution->url = $array['url'];
$attribution->imageUrl = $array['imageUrl'];

if (is_array($array['translations'])) {
foreach ($array['translations'] as $translationArray) {
Expand Down Expand Up @@ -65,6 +68,11 @@ public function getTranslations(): array
return $this->translations;
}

public function getLogo(): ?string
{
return $this->imageUrl;
}


public function getTranslationsByLanguage(string $languageCode): ?OrganisationTranslation
{
Expand Down Expand Up @@ -99,6 +107,7 @@ public function toArray(): array
'countryCode' => $this->countryCode,
'name' => $this->name,
'url' => $this->url,
'imageUrl' => $this->imageUrl,
'translations' => array_values(array_map(function (OrganisationTranslation $translation) {
return $translation->toArray();
}, $this->translations))
Expand All @@ -111,6 +120,7 @@ public function jsonSerialize(): array
return [
'name' => $this->name,
'url' => $this->url,
'imageUrl' => $this->imageUrl,
'translations' => $this->translations
];
}
Expand Down
14 changes: 12 additions & 2 deletions app/Classes/RcnApi/Entities/OrganisationTranslation.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ class OrganisationTranslation implements \JsonSerializable, Arrayable

protected $isPublished;

protected $contributors = [];


public static function createFromResponse(array $array)
{
Expand All @@ -26,6 +28,7 @@ public static function createFromResponse(array $array)
$translation->attributionMessage = $array['attributionMessage'];
$translation->languageCode = $array['languageCode'];
$translation->isPublished = $array['published'];
$translation->contributors = $array['contributors'];
return $translation;
}

Expand Down Expand Up @@ -53,14 +56,20 @@ public function isPublished(): bool
return $this->isPublished;
}

public function getContributors(): array
{
return $this->contributors;
}


public function toArray()
{
return [
'languageCode' => $this->languageCode,
'name' => $this->name,
'attributionMessage' => $this->attributionMessage,
'published' => $this->isPublished
'published' => $this->isPublished,
'contributors' => $this->contributors
];
}

Expand All @@ -71,7 +80,8 @@ public function jsonSerialize(): array
'languageCode' => $this->languageCode,
'name' => $this->name,
'attributionMessage' => $this->attributionMessage,
'published' => $this->isPublished
'published' => $this->isPublished,
'contributors' => $this->contributors
];
}
}
47 changes: 33 additions & 14 deletions app/Classes/RcnApi/Resources/WhatNowResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

class WhatNowResource extends AbstractResource implements WhatNowResourceInterface
{

public function getOrganisationByCountryCode(string $countryCode)
{
return $this->handleApiCall(function () use ($countryCode) {
Expand All @@ -22,7 +22,7 @@ public function getOrganisationByCountryCode(string $countryCode)
});
}


public function getOrganisations()
{
return $this->handleApiCall(function () {
Expand All @@ -36,7 +36,7 @@ public function getOrganisations()
});
}


public function updateOrganisationByCountryCode(Organisation $organisation)
{
return $this->handleApiCall(function () use ($organisation) {
Expand All @@ -50,7 +50,7 @@ public function updateOrganisationByCountryCode(Organisation $organisation)
});
}


public function getPublishedInstructionsByCountryCode(string $countryCode): Collection
{
return $this->handleApiCall(function () use ($countryCode) {
Expand All @@ -64,7 +64,7 @@ public function getPublishedInstructionsByCountryCode(string $countryCode): Coll
});
}


public function getLatestInstructionsByCountryCode(string $countryCode): Collection
{
return $this->handleApiCall(function () use ($countryCode) {
Expand All @@ -78,7 +78,7 @@ public function getLatestInstructionsByCountryCode(string $countryCode): Collect
});
}


public function getAllInstructions(): Collection
{
return $this->handleApiCall(function () {
Expand All @@ -92,7 +92,7 @@ public function getAllInstructions(): Collection
});
}


public function getInstruction(int $id): Instruction
{
return $this->handleApiCall(function () use ($id) {
Expand All @@ -103,7 +103,7 @@ public function getInstruction(int $id): Instruction
});
}


public function getLatestInstructionRevision(int $id): Instruction
{
return $this->handleApiCall(function () use ($id) {
Expand All @@ -114,7 +114,7 @@ public function getLatestInstructionRevision(int $id): Instruction
});
}


public function createInstruction(Instruction $instruction): Instruction
{
return $this->handleApiCall(function () use ($instruction) {
Expand All @@ -128,7 +128,7 @@ public function createInstruction(Instruction $instruction): Instruction
});
}


public function updateInstruction(Instruction $instruction): Instruction
{
if (! $instruction->getId()) {
Expand All @@ -150,7 +150,7 @@ public function updateInstruction(Instruction $instruction): Instruction
});
}


public function createTranslation($id, InstructionTranslation $translation): Instruction
{
return $this->handleApiCall(function () use ($id, $translation) {
Expand All @@ -164,7 +164,7 @@ public function createTranslation($id, InstructionTranslation $translation): Ins
});
}


public function patchTranslation($id, $translationId, $patch)
{
return $this->handleApiCall(function () use ($id, $translationId, $patch) {
Expand All @@ -178,7 +178,7 @@ public function patchTranslation($id, $translationId, $patch)
});
}


public function publishTranslations($ids)
{
$this->handleApiCall(function () use ($ids) {
Expand All @@ -188,7 +188,7 @@ public function publishTranslations($ids)
});
}


public function deleteInstruction(int $id): void
{
$this->handleApiCall(function () use ($id) {
Expand Down Expand Up @@ -238,4 +238,23 @@ public function deleteRegion(int $regionId)
$response = $this->http->delete("regions/region/$regionId");
});
}
public function uploadFile($filePath, $fileName)
{
try {
$response = $this->http->post('upload', [
'multipart' => [
[
'name' => 'file',
'contents' => fopen($filePath, 'r'),
'filename' => $fileName
]
]
]);

return json_decode($response->getBody(), true);
} catch (\Exception $e) {
$this->logger->error('File upload failed: ' . $e->getMessage());
throw $e;
}
}
}
34 changes: 34 additions & 0 deletions app/Http/Controllers/FileController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace App\Http\Controllers;

use App\Classes\RcnApi\RcnApiClient;
use Illuminate\Http\Request;

class FileController extends Controller
{
protected $rcnApiClient;

public function __construct(RcnApiClient $rcnApiClient)
{
$this->rcnApiClient = $rcnApiClient;
}

public function uploadFile(Request $request)
{
$request->validate([
'file' => 'required|file'
]);

$file = $request->file('file');
$filePath = $file->getPathname();
$fileName = $file->getClientOriginalName();

try {
$response = $this->rcnApiClient->whatnow()->uploadFile($filePath, $fileName);
return response()->json($response);
} catch (\Exception $e) {
return response()->json(['error' => 'File upload failed'], 500);
}
}
}
42 changes: 41 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit f2c19b2

Please sign in to comment.