Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions app/Classes/Repositories/OrganisationRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Classes\Repositories;

use App\Models\Organisation;
use App\Models\Contributor;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

Expand Down Expand Up @@ -87,16 +88,35 @@ public function updateDetailsWithInput(Organisation $org, array $input)
if (count($input['translations'])) {
DB::transaction(function () use ($org, $input) {
$org->details()->delete();

foreach ($input['translations'] as $lang => $data) {
$org->details()->updateOrCreate([
$currentDetail = $org->details()->updateOrCreate([
'language_code' => strtolower($lang),
], [
'language_code' => strtolower($lang),
'org_name' => $data['name'] ?? '',
'attribution_message' => $data['attributionMessage'] ?? '',
'published' => $data['published'] ?? false,
]);

if (array_key_exists('contributors', $data)) {
$currentDetail->contributors()->delete();

foreach ($data['contributors'] as $contributor) {
$contributor = new Contributor([
'name' => $contributor['name'],
'logo' => $contributor['logo'],
]);

$isValid = $contributor->validate($contributor->toArray());

if (!$isValid) {
Log::error('Contributor validation failed', ['errors' => $contributor->errors()->toArray()]);
}

$currentDetail->contributors()->save($contributor);
}
}
}
});
}
Expand Down
18 changes: 17 additions & 1 deletion app/Classes/Transformers/OrganisationTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,37 @@ public function transform(Organisation $model)
'name' => $model->org_name,
'url' => $model->attribution_url,
'imageUrl' => $model->attribution_file_name ? $model->getAttributionImageUrl() : null,
'translations' => null
'translations' => null,
];

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

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

$model->details->each(function ($detail) {
$detail->load('contributors');
});

if($this->unpublished || $detail->published) {
$response['translations'][$detail->language_code] = [
'languageCode' => $detail->language_code,
'name' => $detail->org_name,
'attributionMessage' => $detail->attribution_message,
];

$response['translations'][$detail->language_code]['contributors'] = [];
if ($detail->contributors->count()) {

$response['translations'][$detail->language_code]['contributors'] = $detail->contributors->map(function ($contributor) {
return [
'id' => $contributor->id,
'name' => $contributor->name,
'logo' => $contributor->logo,
];
});
}

$response['translations'][$detail->language_code]['published'] = (bool) $detail->published;
}
}
Expand Down
16 changes: 15 additions & 1 deletion app/Classes/Transformers/WhatNowEntityTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,20 +70,34 @@ public function transform(WhatNowEntity $model)
'countryCode' => $model->organisation->country_code,
'url' => $model->organisation->attribution_url,
'imageUrl' => $model->organisation->attribution_file_name ? $model->organisation->getAttributionImageUrl() : null,
'translations' => null
'translations' => null,
],
];

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

foreach ($model->organisation->details as $detail) {
$model->organisation->details->each(function ($detail) {
$detail->load('contributors');
});
$response['attribution']['translations'][$detail->language_code] = [
'languageCode' => $detail->language_code,
'name' => $detail->org_name,
'attributionMessage' => $detail->attribution_message,
'contributors' => []
];

if ($detail->contributors->count()) {
$response['attribution']['translations'][$detail->language_code]['contributors'] = $detail->contributors->map(function ($contributor) {
return [
'id' => $contributor->id,
'name' => $contributor->name,
'logo' => $contributor->logo,
];
});
}

$response['attribution']['translations'][$detail->language_code]['published'] = (bool) $detail->published;
}
}
Expand Down
7 changes: 7 additions & 0 deletions app/Http/Controllers/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;

/**
* @OA\Info(
* title="Whatnow API",
* description="Whatnow API documentation",
* version="1.0.0",
* )
*/
class Controller extends BaseController
{
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
Expand Down
77 changes: 77 additions & 0 deletions app/Http/Controllers/OrganisationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@
use Illuminate\Support\Facades\Storage;
use League\Fractal\Manager;

/**
* @OA\Tag(
* name="Organisation",
* description="Operations about Organisations"
* )
*/
class OrganisationController extends Controller
{
/**
Expand Down Expand Up @@ -101,6 +107,77 @@ public function getById($code, Request $request)
/**
* @param $code
* @return \Symfony\Component\HttpFoundation\Response
*/
/**
* @OA\Put(
* path="/organisation/{code}",
* summary="Update organisation by country code",
* tags={"Organisation"},
* @OA\Parameter(
* name="code",
* in="path",
* description="Country code of the organisation",
* required=true,
* @OA\Schema(type="string")
* ),
* @OA\RequestBody(
* required=true,
* @OA\JsonContent(
* type="object",
* @OA\Property(property="countryCode", type="string", example="USA"),
* @OA\Property(property="name", type="string", example="American Red Cross"),
* @OA\Property(property="url", type="string", nullable=true, example=null),
* @OA\Property(
* property="translations",
* type="array",
* @OA\Items(
* type="object",
* @OA\Property(property="languageCode", type="string", example="en"),
* @OA\Property(property="name", type="string", example="Organization name"),
* @OA\Property(property="attributionMessage", type="string", example="Attribution Message"),
* @OA\Property(property="published", type="boolean", example=true),
* @OA\Property(
* property="contributors",
* type="array",
* @OA\Items(
* type="object",
* @OA\Property(property="name", type="string", example="Contributor name"),
* @OA\Property(property="logo", type="string", example="logo.png")
* )
* )
* )
* )
* )
* ),
* @OA\Response(
* response=200,
* description="Successful response",
* @OA\JsonContent(
* type="object",
* @OA\Property(property="data", type="object")
* )
* ),
* @OA\Response(
* response=404,
* description="Organisation not found",
* @OA\JsonContent(
* type="object",
* @OA\Property(property="status", type="integer", example=404),
* @OA\Property(property="error_message", type="string", example="Organisation does not exist"),
* @OA\Property(property="errors", type="array", @OA\Items(type="string"))
* )
* ),
* @OA\Response(
* response=500,
* description="Organisation could not be updated",
* @OA\JsonContent(
* type="object",
* @OA\Property(property="status", type="integer", example=500),
* @OA\Property(property="error_message", type="string", example="Organisation could not be updated"),
* @OA\Property(property="errors", type="array", @OA\Items(type="string"))
* )
* )
* )
*/
public function putById($code)
{
Expand Down
75 changes: 75 additions & 0 deletions app/Models/Contributor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Validator;

class Contributor extends Model
{

/**
* table
*
* @var string
*/
protected $table = 'contributors';


/**
* @var
*/
protected $errors;

/**
* @var bool
*/
public $timestamps = false;

/**
* The attributes that are mass assignable.
* @var array
*/
protected $fillable = ['name', 'logo', 'org_detail_id'];

/**
* Model validation rules
* @var array
*/
protected $rules = [
'name' => 'required|string|between:2,100',
'logo' => 'string|between:2,2048',
];


/**
* @param $data
* @return bool
*/
public function validate($data)
{
$v = Validator::make($data, $this->rules);

if ($v->fails()) {
$this->errors = $v->errors();

return false;
}

return true;
}

/**
* @return mixed
*/
public function errors()
{
return $this->errors;
}

public function organisation()
{
return $this->belongsTo(OrganisationDetails::class, 'org_detail_id');
}

}
5 changes: 5 additions & 0 deletions app/Models/OrganisationDetails.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,10 @@ public function organisation()
{
return $this->belongsTo('App\Models\Details', 'org_id', 'id');
}

public function contributors()
{
return $this->hasMany(Contributor::class, 'org_detail_id');
}
}

1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"license": "MIT",
"require": {
"php": "^7.4",
"darkaonline/l5-swagger": "^8.6",
"fideloper/proxy": "^4.0",
"laravel/framework": "^7.0",
"laravel/helpers": "^1.6",
Expand Down
Loading
Loading