taktik - laravel
This commit is contained in:
30
app/Http/Resources/CategoryCollection.php
Normal file
30
app/Http/Resources/CategoryCollection.php
Normal file
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Http\Resources;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Resources\Json\ResourceCollection;
|
||||
use OpenApi\Annotations as OA;
|
||||
|
||||
/**
|
||||
* @OA\Schema(
|
||||
* schema="CategoryCollection",
|
||||
* type="array",
|
||||
* title="Category Collection",
|
||||
* description="A collection of CategoryResource",
|
||||
* @OA\Items(ref="#/components/schemas/CategoryResource")
|
||||
* )
|
||||
*/
|
||||
class CategoryCollection extends ResourceCollection
|
||||
{
|
||||
/**
|
||||
* @param Request $request
|
||||
* @return array<string, mixed>|\Illuminate\Contracts\Support\Arrayable<string, mixed>|\JsonSerializable
|
||||
*/
|
||||
public function toArray(Request $request): array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
|
||||
{
|
||||
return parent::toArray($request);
|
||||
}
|
||||
}
|
||||
63
app/Http/Resources/CategoryResource.php
Normal file
63
app/Http/Resources/CategoryResource.php
Normal file
@@ -0,0 +1,63 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Http\Resources;
|
||||
|
||||
use App\Models\Category;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
use OpenApi\Annotations as OA;
|
||||
|
||||
/**
|
||||
* @mixin Category
|
||||
* @OA\Schema(
|
||||
* schema="CategoryResource",
|
||||
* type="object",
|
||||
* title="Category Resource",
|
||||
* description="Resource representing a single category",
|
||||
* @OA\Property(
|
||||
* property="id",
|
||||
* type="integer",
|
||||
* description="ID of the category",
|
||||
* example=1
|
||||
* ),
|
||||
* @OA\Property(
|
||||
* property="name",
|
||||
* type="string",
|
||||
* description="Name of the category",
|
||||
* example="Technology"
|
||||
* ),
|
||||
* @OA\Property(
|
||||
* property="created_at",
|
||||
* type="string",
|
||||
* format="date-time",
|
||||
* description="Timestamp when the category was created",
|
||||
* example="2023-12-10T14:17:00Z"
|
||||
* ),
|
||||
* @OA\Property(
|
||||
* property="updated_at",
|
||||
* type="string",
|
||||
* format="date-time",
|
||||
* description="Timestamp when the category was last updated",
|
||||
* example="2023-12-11T15:20:00Z"
|
||||
* )
|
||||
* )
|
||||
*/
|
||||
class CategoryResource extends JsonResource
|
||||
{
|
||||
/**
|
||||
* Transform the resource into an array.
|
||||
*
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function toArray(Request $request): array
|
||||
{
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'name' => $this->name,
|
||||
'created_at' => $this->created_at->toDateTimeString(),
|
||||
'updated_at' => $this->updated_at->toDateTimeString(),
|
||||
];
|
||||
}
|
||||
}
|
||||
30
app/Http/Resources/CommentCollection.php
Normal file
30
app/Http/Resources/CommentCollection.php
Normal file
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Http\Resources;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Resources\Json\ResourceCollection;
|
||||
use OpenApi\Annotations as OA;
|
||||
|
||||
/**
|
||||
* @OA\Schema(
|
||||
* schema="CommentCollection",
|
||||
* type="array",
|
||||
* title="Comment Collection",
|
||||
* description="A collection of CommentCollection",
|
||||
* @OA\Items(ref="#/components/schemas/CommentResource")
|
||||
* )
|
||||
*/
|
||||
class CommentCollection extends ResourceCollection
|
||||
{
|
||||
/**
|
||||
* @param Request $request
|
||||
* @return array<string, mixed>|\Illuminate\Contracts\Support\Arrayable<string, mixed>|\JsonSerializable
|
||||
*/
|
||||
public function toArray(Request $request): array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
|
||||
{
|
||||
return parent::toArray($request);
|
||||
}
|
||||
}
|
||||
62
app/Http/Resources/CommentResource.php
Normal file
62
app/Http/Resources/CommentResource.php
Normal file
@@ -0,0 +1,62 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Http\Resources;
|
||||
|
||||
use App\Models\Comment;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
use OpenApi\Annotations as OA;
|
||||
|
||||
/**
|
||||
* @mixin Comment
|
||||
* @OA\Schema(
|
||||
* schema="CommentResource",
|
||||
* type="object",
|
||||
* title="Comment Resource",
|
||||
* description="Resource representing a single comment",
|
||||
* @OA\Property(
|
||||
* property="id",
|
||||
* type="integer",
|
||||
* description="ID of the comment",
|
||||
* example=1
|
||||
* ),
|
||||
* @OA\Property(
|
||||
* property="content",
|
||||
* type="string",
|
||||
* description="Content of the comment",
|
||||
* example="This is a sample comment."
|
||||
* ),
|
||||
* @OA\Property(
|
||||
* property="created_at",
|
||||
* type="string",
|
||||
* format="date-time",
|
||||
* description="Timestamp when the comment was created",
|
||||
* example="2023-12-10T15:24:00Z"
|
||||
* ),
|
||||
* @OA\Property(
|
||||
* property="updated_at",
|
||||
* type="string",
|
||||
* format="date-time",
|
||||
* description="Timestamp when the comment was last updated",
|
||||
* example="2023-12-10T16:30:00Z"
|
||||
* )
|
||||
* )
|
||||
*/
|
||||
class CommentResource extends JsonResource
|
||||
{
|
||||
/** Transform the resource into an array.
|
||||
*
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function toArray(Request $request): array
|
||||
{
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'content' => $this->content,
|
||||
'created_at' => $this->created_at->toDateTimeString(),
|
||||
'updated_at' => $this->updated_at->toDateTimeString(),
|
||||
];
|
||||
}
|
||||
}
|
||||
47
app/Http/Resources/PaginableResource.php
Normal file
47
app/Http/Resources/PaginableResource.php
Normal file
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Http\Resources;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
use OpenApi\Annotations as OA;
|
||||
|
||||
/**
|
||||
* @template T
|
||||
* @mixin \App\Services\PaginableResource<T>
|
||||
* Souhrnný formát výstupu pro paginovaná data s metadaty.
|
||||
*
|
||||
* @OA\Schema(
|
||||
* schema="PaginableResourceMeta",
|
||||
* type="object",
|
||||
* title="Paginable Resource (meta)",
|
||||
* description="A paginated collection with meta information",
|
||||
* @OA\Property(property="totalCount", type="integer", example=1),
|
||||
* @OA\Property(property="pages", type="integer", example=10),
|
||||
* )
|
||||
*/
|
||||
class PaginableResource extends JsonResource
|
||||
{
|
||||
public function __construct(mixed $resource, protected readonly string $classResource)
|
||||
{
|
||||
parent::__construct($resource);
|
||||
}
|
||||
|
||||
/**
|
||||
* Transform the resource collection into an array.
|
||||
*
|
||||
* @return array<int|string, mixed>
|
||||
*/
|
||||
public function toArray(Request $request): array
|
||||
{
|
||||
return [
|
||||
'items' => $this->classResource::make($this->data),
|
||||
'meta' => [
|
||||
'totalCount' => $this->totalCount,
|
||||
'pages' => $this->totalPages,
|
||||
]
|
||||
];
|
||||
}
|
||||
}
|
||||
30
app/Http/Resources/PostCollection.php
Normal file
30
app/Http/Resources/PostCollection.php
Normal file
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Http\Resources;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Resources\Json\ResourceCollection;
|
||||
use OpenApi\Annotations as OA;
|
||||
|
||||
/**
|
||||
* @OA\Schema(
|
||||
* schema="PostCollection",
|
||||
* type="array",
|
||||
* title="Post Collection",
|
||||
* description="A collection of PostResource",
|
||||
* @OA\Items(ref="#/components/schemas/PostResource")
|
||||
* )
|
||||
*/
|
||||
class PostCollection extends ResourceCollection
|
||||
{
|
||||
/**
|
||||
* @param Request $request
|
||||
* @return array<string, mixed>|\Illuminate\Contracts\Support\Arrayable<string, mixed>|\JsonSerializable
|
||||
*/
|
||||
public function toArray(Request $request): array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
|
||||
{
|
||||
return parent::toArray($request);
|
||||
}
|
||||
}
|
||||
89
app/Http/Resources/PostResource.php
Normal file
89
app/Http/Resources/PostResource.php
Normal file
@@ -0,0 +1,89 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Http\Resources;
|
||||
|
||||
use App\Models\Post;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
use OpenApi\Annotations as OA;
|
||||
|
||||
/**
|
||||
* @mixin Post
|
||||
* @OA\Schema(
|
||||
* schema="PostResource",
|
||||
* type="object",
|
||||
* title="Post Resource",
|
||||
* description="Resource representing a single post",
|
||||
* @OA\Property(
|
||||
* property="id",
|
||||
* type="integer",
|
||||
* description="ID of the post",
|
||||
* example=1
|
||||
* ),
|
||||
* @OA\Property(
|
||||
* property="title",
|
||||
* type="string",
|
||||
* description="Title of the post",
|
||||
* example="Sample Post Title"
|
||||
* ),
|
||||
* @OA\Property(
|
||||
* property="content",
|
||||
* type="string",
|
||||
* description="Content of the post",
|
||||
* example="This is a sample post content."
|
||||
* ),
|
||||
* @OA\Property(
|
||||
* property="category_id",
|
||||
* type="integer",
|
||||
* description="Category ID the post belongs to",
|
||||
* example=1
|
||||
* ),
|
||||
* @OA\Property(
|
||||
* property="category",
|
||||
* description="Category object",
|
||||
* ref="#/components/schemas/CategoryResource",
|
||||
* ),
|
||||
* @OA\Property(
|
||||
* property="tags",
|
||||
* ref="#/components/schemas/TagCollection",
|
||||
* description="Collection of tags related to the post"
|
||||
* ),
|
||||
* @OA\Property(
|
||||
* property="created_at",
|
||||
* type="string",
|
||||
* format="date-time",
|
||||
* description="Timestamp when the post was created",
|
||||
* example="2023-12-10T15:24:00Z"
|
||||
* ),
|
||||
* @OA\Property(
|
||||
* property="updated_at",
|
||||
* type="string",
|
||||
* format="date-time",
|
||||
* description="Timestamp when the post was last updated",
|
||||
* example="2023-12-10T15:26:00Z"
|
||||
* )
|
||||
* )
|
||||
*/
|
||||
class PostResource extends JsonResource
|
||||
{
|
||||
/**
|
||||
* Transform the resource collection into an array.
|
||||
*
|
||||
* @return array<int|string, mixed>
|
||||
*/
|
||||
public function toArray(Request $request): array
|
||||
{
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'title' => $this->title,
|
||||
'content' => $this->content,
|
||||
'category_id' => $this->category_id,
|
||||
'category' => new CategoryResource($this->whenLoaded('category')),
|
||||
'tags' => new TagCollection($this->whenLoaded('tags')),
|
||||
'created_at' => $this->created_at->toDateTimeString(),
|
||||
'updated_at' => $this->updated_at->toDateTimeString(),
|
||||
];
|
||||
}
|
||||
}
|
||||
30
app/Http/Resources/TagCollection.php
Normal file
30
app/Http/Resources/TagCollection.php
Normal file
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Http\Resources;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Resources\Json\ResourceCollection;
|
||||
use OpenApi\Annotations as OA;
|
||||
|
||||
/**
|
||||
* @OA\Schema(
|
||||
* schema="TagCollection",
|
||||
* type="array",
|
||||
* title="Tag Collection",
|
||||
* description="A collection of TagResource",
|
||||
* @OA\Items(ref="#/components/schemas/TagResource")
|
||||
* )
|
||||
*/
|
||||
class TagCollection extends ResourceCollection
|
||||
{
|
||||
/**
|
||||
* @param Request $request
|
||||
* @return array<string, mixed>|\Illuminate\Contracts\Support\Arrayable<string, mixed>|\JsonSerializable
|
||||
*/
|
||||
public function toArray(Request $request): array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
|
||||
{
|
||||
return parent::toArray($request);
|
||||
}
|
||||
}
|
||||
67
app/Http/Resources/TagResource.php
Normal file
67
app/Http/Resources/TagResource.php
Normal file
@@ -0,0 +1,67 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Http\Resources;
|
||||
|
||||
use App\Models\Tag;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
use OpenApi\Annotations as OA;
|
||||
|
||||
/**
|
||||
* @mixin Tag
|
||||
*/
|
||||
|
||||
/**
|
||||
* @mixin Tag
|
||||
* @OA\Schema(
|
||||
* schema="TagResource",
|
||||
* type="object",
|
||||
* title="Tag Resource",
|
||||
* description="Resource representing a single tag",
|
||||
* @OA\Property(
|
||||
* property="id",
|
||||
* type="integer",
|
||||
* description="ID of the tag",
|
||||
* example=1
|
||||
* ),
|
||||
* @OA\Property(
|
||||
* property="name",
|
||||
* type="string",
|
||||
* description="Name of the tag",
|
||||
* example="Laravel"
|
||||
* ),
|
||||
* @OA\Property(
|
||||
* property="created_at",
|
||||
* type="string",
|
||||
* format="date-time",
|
||||
* description="Timestamp when the tag was created",
|
||||
* example="2023-12-10T14:17:00Z"
|
||||
* ),
|
||||
* @OA\Property(
|
||||
* property="updated_at",
|
||||
* type="string",
|
||||
* format="date-time",
|
||||
* description="Timestamp when the tag was last updated",
|
||||
* example="2023-12-11T15:20:00Z"
|
||||
* )
|
||||
* )
|
||||
*/
|
||||
class TagResource extends JsonResource
|
||||
{
|
||||
/**
|
||||
* Transform the resource into an array.
|
||||
*
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function toArray(Request $request): array
|
||||
{
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'name' => $this->name,
|
||||
'created_at' => $this->created_at->toDateTimeString(),
|
||||
'updated_at' => $this->updated_at->toDateTimeString(),
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user