118 lines
4.6 KiB
PHP
118 lines
4.6 KiB
PHP
<?php
|
|
namespace App\Http\Resources;
|
|
|
|
use App\Services\ThumbnailPresenter;
|
|
use Illuminate\Http\Resources\Json\JsonResource;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Schema;
|
|
|
|
class ArtworkResource extends JsonResource
|
|
{
|
|
/**
|
|
* Transform the resource into an array.
|
|
*/
|
|
public function toArray($request): array
|
|
{
|
|
$md = ThumbnailPresenter::present($this->resource, 'md');
|
|
$lg = ThumbnailPresenter::present($this->resource, 'lg');
|
|
$xl = ThumbnailPresenter::present($this->resource, 'xl');
|
|
$sq = ThumbnailPresenter::present($this->resource, 'sq');
|
|
|
|
$canonicalSlug = \Illuminate\Support\Str::slug((string) ($this->slug ?: $this->title));
|
|
if ($canonicalSlug === '') {
|
|
$canonicalSlug = (string) $this->id;
|
|
}
|
|
|
|
$followerCount = (int) ($this->user?->profile?->followers_count ?? 0);
|
|
if (($followerCount <= 0) && Schema::hasTable('friends_list') && !empty($this->user?->id)) {
|
|
$followerCount = (int) DB::table('friends_list')
|
|
->where('friend_id', (int) $this->user->id)
|
|
->count();
|
|
}
|
|
|
|
$viewerId = (int) optional($request->user())->id;
|
|
$isLiked = false;
|
|
$isFavorited = false;
|
|
$isFollowing = false;
|
|
|
|
if ($viewerId > 0) {
|
|
if (Schema::hasTable('artwork_likes')) {
|
|
$isLiked = DB::table('artwork_likes')
|
|
->where('user_id', $viewerId)
|
|
->where('artwork_id', (int) $this->id)
|
|
->exists();
|
|
}
|
|
|
|
if (Schema::hasTable('user_favorites')) {
|
|
$isFavorited = DB::table('user_favorites')
|
|
->where('user_id', $viewerId)
|
|
->where('artwork_id', (int) $this->id)
|
|
->exists();
|
|
}
|
|
|
|
if (Schema::hasTable('friends_list') && !empty($this->user?->id)) {
|
|
$isFollowing = DB::table('friends_list')
|
|
->where('user_id', $viewerId)
|
|
->where('friend_id', (int) $this->user->id)
|
|
->exists();
|
|
}
|
|
}
|
|
|
|
return [
|
|
'id' => (int) $this->id,
|
|
'slug' => (string) $this->slug,
|
|
'title' => (string) $this->title,
|
|
'description' => (string) ($this->description ?? ''),
|
|
'dimensions' => [
|
|
'width' => (int) ($this->width ?? 0),
|
|
'height' => (int) ($this->height ?? 0),
|
|
],
|
|
'published_at' => optional($this->published_at)->toIsoString(),
|
|
'canonical_url' => route('art.show', ['id' => (int) $this->id, 'slug' => $canonicalSlug]),
|
|
'thumbs' => [
|
|
'md' => $md,
|
|
'lg' => $lg,
|
|
'xl' => $xl,
|
|
'sq' => $sq,
|
|
],
|
|
'file' => [
|
|
'url' => $lg['url'] ?? null,
|
|
'srcset' => ThumbnailPresenter::srcsetForArtwork($this->resource),
|
|
'mime_type' => 'image/webp',
|
|
],
|
|
'user' => [
|
|
'id' => (int) ($this->user?->id ?? 0),
|
|
'name' => (string) ($this->user?->name ?? ''),
|
|
'username' => (string) ($this->user?->username ?? ''),
|
|
'profile_url' => $this->user?->username ? '/@' . $this->user->username : null,
|
|
'avatar_url' => $this->user?->profile?->avatar_url,
|
|
'followers_count' => $followerCount,
|
|
],
|
|
'viewer' => [
|
|
'is_liked' => $isLiked,
|
|
'is_favorited' => $isFavorited,
|
|
'is_following_author' => $isFollowing,
|
|
'is_authenticated' => $viewerId > 0,
|
|
'id' => $viewerId > 0 ? $viewerId : null,
|
|
],
|
|
'stats' => [
|
|
'views' => (int) ($this->stats?->views ?? 0),
|
|
'downloads' => (int) ($this->stats?->downloads ?? 0),
|
|
'favorites' => (int) ($this->stats?->favorites ?? 0),
|
|
'likes' => (int) ($this->stats?->rating_count ?? 0),
|
|
],
|
|
'categories' => $this->categories->map(fn ($category) => [
|
|
'id' => (int) $category->id,
|
|
'slug' => (string) $category->slug,
|
|
'name' => (string) $category->name,
|
|
'content_type_slug' => (string) ($category->contentType?->slug ?? ''),
|
|
])->values(),
|
|
'tags' => $this->tags->map(fn ($tag) => [
|
|
'id' => (int) $tag->id,
|
|
'slug' => (string) $tag->slug,
|
|
'name' => (string) $tag->name,
|
|
])->values(),
|
|
];
|
|
}
|
|
}
|