mirror of https://github.com/kcal-app/kcal.git
Add media to API responses
This commit is contained in:
parent
ac57f6d33e
commit
521479ad88
|
@ -0,0 +1,61 @@
|
|||
<?php
|
||||
|
||||
namespace App\JsonApi\Adapters;
|
||||
|
||||
use CloudCreativity\LaravelJsonApi\Eloquent\AbstractAdapter;
|
||||
use CloudCreativity\LaravelJsonApi\Eloquent\BelongsTo;
|
||||
use CloudCreativity\LaravelJsonApi\Pagination\StandardStrategy;
|
||||
use Illuminate\Support\Collection;
|
||||
use Spatie\MediaLibrary\MediaCollections\Models\Media;
|
||||
|
||||
/**
|
||||
* Media adapter.
|
||||
*
|
||||
* "Medium" is the singular form of "media" so it is used for the class name
|
||||
* here.
|
||||
*
|
||||
* @package App\JsonApi\Adapters
|
||||
*/
|
||||
class MediumAdapter extends AbstractAdapter
|
||||
{
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected $attributes = [];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected $filterScopes = [];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected $defaultSort = [];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function __construct(StandardStrategy $paging)
|
||||
{
|
||||
parent::__construct(new Media(), $paging);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function filter($query, Collection $filters)
|
||||
{
|
||||
$this->filterWithScopes($query, $filters);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parent model (Recipe or Food).
|
||||
*/
|
||||
protected function owner(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo('model');
|
||||
}
|
||||
|
||||
}
|
|
@ -62,6 +62,14 @@ class RecipeAdapter extends AbstractAdapter
|
|||
return $this->morphMany($this->hasMany('ingredientAmounts'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Media relationships.
|
||||
*/
|
||||
protected function media(): MorphHasMany
|
||||
{
|
||||
return $this->morphMany($this->hasMany('media'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Step relationships.
|
||||
*/
|
||||
|
|
|
@ -0,0 +1,81 @@
|
|||
<?php
|
||||
|
||||
namespace App\JsonApi\Schemas;
|
||||
|
||||
use Neomerx\JsonApi\Schema\SchemaProvider;
|
||||
|
||||
/**
|
||||
* Media schema.
|
||||
*
|
||||
* "Medium" is the singular form of "media" so it is used for the class name
|
||||
* here.
|
||||
*
|
||||
* @package App\JsonApi\Schemas
|
||||
*/
|
||||
class MediumSchema extends SchemaProvider
|
||||
{
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected $resourceType = 'media';
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getId($resource): string
|
||||
{
|
||||
return (string) $resource->getRouteKey();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getAttributes($resource): array
|
||||
{
|
||||
/** @var \Spatie\MediaLibrary\MediaCollections\Models\Media $resource */
|
||||
$attributes = [
|
||||
'uuid' => $resource->uuid,
|
||||
'collectionName' => $resource->collection_name,
|
||||
'name' => $resource->name,
|
||||
'fileName' => $resource->file_name,
|
||||
'url' => $resource->getUrl(),
|
||||
'mimeType' => $resource->mime_type,
|
||||
'size' => $resource->size,
|
||||
'sizeFormatted' => $resource->getHumanReadableSizeAttribute(),
|
||||
'manipulations' => $resource->manipulations,
|
||||
'customProperties' => $resource->custom_properties,
|
||||
'conversions' => [],
|
||||
'responsiveImages' => $resource->responsive_images,
|
||||
'orderColumn' => $resource->order_column,
|
||||
'createdAt' => $resource->created_at,
|
||||
'updatedAt' => $resource->updated_at,
|
||||
];
|
||||
|
||||
// Add all conversion URLs.
|
||||
foreach ($resource->getGeneratedConversions() as $conversion_name => $generated) {
|
||||
if ($generated) {
|
||||
$attributes['conversions'][$conversion_name] = $resource->getUrl($conversion_name);
|
||||
}
|
||||
}
|
||||
|
||||
return $attributes;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function getRelationships($resource, $isPrimary, array $includeRelationships): array
|
||||
{
|
||||
return [
|
||||
'owner' => [
|
||||
self::SHOW_SELF => true,
|
||||
self::SHOW_RELATED => true,
|
||||
self::SHOW_DATA => isset($includeRelationships['model']),
|
||||
self::DATA => function () use ($resource) {
|
||||
return $resource->model;
|
||||
},
|
||||
]
|
||||
];
|
||||
}
|
||||
}
|
|
@ -69,6 +69,14 @@ class RecipeSchema extends SchemaProvider
|
|||
return $resource->ingredientAmounts;
|
||||
},
|
||||
],
|
||||
'media' => [
|
||||
self::SHOW_SELF => true,
|
||||
self::SHOW_RELATED => true,
|
||||
self::SHOW_DATA => isset($includeRelationships['media']),
|
||||
self::DATA => function () use ($resource) {
|
||||
return $resource->media;
|
||||
},
|
||||
],
|
||||
'steps' => [
|
||||
self::SHOW_SELF => true,
|
||||
self::SHOW_RELATED => true,
|
||||
|
|
|
@ -68,6 +68,7 @@ return [
|
|||
'resources' => [
|
||||
'foods' => \App\Models\Food::class,
|
||||
'ingredient-amounts' => \App\Models\IngredientAmount::class,
|
||||
'media' => \Spatie\MediaLibrary\MediaCollections\Models\Media::class,
|
||||
'journal-entries' => \App\Models\JournalEntry::class,
|
||||
'recipes' => \App\Models\Recipe::class,
|
||||
'recipe-steps' => \App\Models\RecipeStep::class,
|
||||
|
|
|
@ -19,6 +19,9 @@ JsonApi::register('v1')->routes(function ($api) {
|
|||
$relations->hasOne('ingredient')->readOnly();
|
||||
$relations->hasOne('parent')->readOnly();
|
||||
})->readOnly();
|
||||
$api->resource('media')->relationships(function ($relations) {
|
||||
$relations->hasOne('owner')->readOnly();
|
||||
})->readOnly();
|
||||
$api->resource('journal-entries')->relationships(function ($relations) {
|
||||
$relations->hasMany('foods')->readOnly();
|
||||
$relations->hasMany('recipes')->readOnly();
|
||||
|
@ -26,6 +29,7 @@ JsonApi::register('v1')->routes(function ($api) {
|
|||
})->readOnly();
|
||||
$api->resource('recipes')->relationships(function ($relations) {
|
||||
$relations->hasMany('ingredient-amounts')->readOnly();
|
||||
$relations->hasMany('media')->readOnly();
|
||||
$relations->hasMany('steps')->readOnly();
|
||||
$relations->hasMany('tags')->readOnly();
|
||||
})->readOnly();
|
||||
|
|
Loading…
Reference in New Issue