2024-06-27 16:58:11 -04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Resources\Api\V0;
|
|
|
|
|
2024-08-07 23:30:09 -04:00
|
|
|
use App\Http\Controllers\Api\V0\ApiController;
|
2024-06-27 16:58:11 -04:00
|
|
|
use App\Models\Mod;
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use Illuminate\Http\Resources\Json\JsonResource;
|
|
|
|
|
|
|
|
/** @mixin Mod */
|
|
|
|
class ModResource extends JsonResource
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Transform the resource into an array.
|
|
|
|
*/
|
|
|
|
public function toArray(Request $request): array
|
|
|
|
{
|
2024-10-12 13:19:54 -06:00
|
|
|
$this->load(['users', 'versions', 'license']);
|
|
|
|
|
2024-06-27 16:58:11 -04:00
|
|
|
return [
|
|
|
|
'type' => 'mod',
|
|
|
|
'id' => $this->id,
|
|
|
|
'attributes' => [
|
2024-08-08 18:18:05 -04:00
|
|
|
'hub_id' => $this->hub_id,
|
2024-06-27 16:58:11 -04:00
|
|
|
'name' => $this->name,
|
|
|
|
'slug' => $this->slug,
|
2024-07-04 22:10:48 -04:00
|
|
|
'teaser' => $this->teaser,
|
2024-06-27 16:58:11 -04:00
|
|
|
'description' => $this->when(
|
|
|
|
$request->routeIs('api.v0.mods.show'),
|
|
|
|
$this->description
|
|
|
|
),
|
|
|
|
'license_id' => $this->license_id,
|
2024-07-04 22:10:48 -04:00
|
|
|
'source_code_link' => $this->source_code_link,
|
|
|
|
'featured' => $this->featured,
|
|
|
|
'contains_ai_content' => $this->contains_ai_content,
|
|
|
|
'contains_ads' => $this->contains_ads,
|
2024-06-27 16:58:11 -04:00
|
|
|
'created_at' => $this->created_at,
|
2024-07-04 22:10:48 -04:00
|
|
|
'updated_at' => $this->updated_at,
|
2024-07-26 09:34:57 -04:00
|
|
|
'published_at' => $this->published_at,
|
2024-06-27 16:58:11 -04:00
|
|
|
],
|
|
|
|
'relationships' => [
|
2024-08-07 23:30:09 -04:00
|
|
|
'users' => $this->users->map(fn ($user) => [
|
|
|
|
'data' => [
|
2024-06-27 16:58:11 -04:00
|
|
|
'type' => 'user',
|
2024-07-04 22:10:48 -04:00
|
|
|
'id' => $user->id,
|
2024-08-07 23:30:09 -04:00
|
|
|
],
|
|
|
|
'links' => [
|
|
|
|
'self' => $user->profileUrl(),
|
|
|
|
],
|
|
|
|
])->toArray(),
|
2024-08-08 16:11:50 -04:00
|
|
|
'versions' => $this->versions->map(fn ($version) => [
|
|
|
|
'data' => [
|
|
|
|
'type' => 'version',
|
|
|
|
'id' => $version->id,
|
|
|
|
],
|
|
|
|
'links' => [
|
2024-10-12 13:20:40 -06:00
|
|
|
'self' => $version->downloadUrl(absolute: true),
|
2024-08-08 16:11:50 -04:00
|
|
|
],
|
|
|
|
|
|
|
|
])->toArray(),
|
2024-06-27 16:58:11 -04:00
|
|
|
'license' => [
|
2024-08-07 23:30:09 -04:00
|
|
|
[
|
|
|
|
'data' => [
|
|
|
|
'type' => 'license',
|
|
|
|
'id' => $this->license_id,
|
|
|
|
],
|
2024-06-27 16:58:11 -04:00
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
2024-08-07 23:30:09 -04:00
|
|
|
'includes' => $this->when(
|
2024-08-08 16:11:50 -04:00
|
|
|
ApiController::shouldInclude(['users', 'license', 'versions']),
|
|
|
|
fn () => collect([
|
|
|
|
'users' => $this->users->map(fn ($user) => new UserResource($user)),
|
|
|
|
'license' => new LicenseResource($this->license),
|
|
|
|
'versions' => $this->versions->map(fn ($version) => new ModVersionResource($version)),
|
|
|
|
])
|
|
|
|
->filter(fn ($value, $key) => ApiController::shouldInclude($key))
|
|
|
|
->flatten(1)
|
|
|
|
->values()
|
2024-08-07 23:30:09 -04:00
|
|
|
),
|
2024-06-27 16:58:11 -04:00
|
|
|
'links' => [
|
2024-08-09 12:35:46 -04:00
|
|
|
'self' => $this->detailUrl(),
|
2024-06-27 16:58:11 -04:00
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|