taktik - laravel
This commit is contained in:
103
app/Http/Controllers/CategoryController.php
Normal file
103
app/Http/Controllers/CategoryController.php
Normal file
@@ -0,0 +1,103 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Http\Requests\Category\DestroyCategoryRequest;
|
||||
use App\Http\Requests\Category\ListCategoryRequest;
|
||||
use App\Http\Requests\Category\ShowCategoryRequest;
|
||||
use App\Http\Requests\Category\StoreCategoryRequest;
|
||||
use App\Http\Requests\Category\UpdateCategoryRequest;
|
||||
use App\Http\Resources\CategoryCollection;
|
||||
use App\Http\Resources\CategoryResource;
|
||||
use App\Http\Resources\PaginableResource;
|
||||
use App\Services\Category\CategoryServiceInterface;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use OpenApi\Annotations as OA;
|
||||
|
||||
final class CategoryController
|
||||
{
|
||||
public function __construct(
|
||||
private readonly CategoryServiceInterface $categoryService,
|
||||
)
|
||||
{
|
||||
}
|
||||
|
||||
public function list(ListCategoryRequest $request, int $page): JsonResponse
|
||||
{
|
||||
$categories = $this->categoryService->fetchCategories(
|
||||
$page,
|
||||
$request->filters(),
|
||||
$request->order()
|
||||
);
|
||||
return response()->json(PaginableResource::make($categories, CategoryCollection::class));
|
||||
}
|
||||
|
||||
/**
|
||||
* @OA\Post(
|
||||
* path="/api/v1/categories",
|
||||
* operationId="storeCategory",
|
||||
* tags={"Categories"},
|
||||
* summary="Create a new category",
|
||||
* description="Creates a new category and returns the created category data.",
|
||||
* @OA\RequestBody(
|
||||
* required=true,
|
||||
* @OA\JsonContent(
|
||||
* type="object",
|
||||
* required={"name"},
|
||||
* @OA\Property(property="name", type="string", example="Technology", description="The name of the category")
|
||||
* )
|
||||
* ),
|
||||
* @OA\Response(
|
||||
* response=201,
|
||||
* description="Category successfully created",
|
||||
* @OA\JsonContent(
|
||||
* type="object",
|
||||
* ref="#/components/schemas/CategoryResource"
|
||||
* )
|
||||
* )
|
||||
* ),
|
||||
* @OA\Response(
|
||||
* response=422,
|
||||
* description="Validation error",
|
||||
* @OA\JsonContent(ref="#/components/schemas/ValidationError")
|
||||
* )
|
||||
* )
|
||||
*/
|
||||
public function store(StoreCategoryRequest $request): JsonResponse
|
||||
{
|
||||
return response()->json(CategoryResource::make($this->categoryService->storeCategory($request->all())), 201);
|
||||
}
|
||||
|
||||
public function show(ShowCategoryRequest $request, int $id): JsonResponse
|
||||
{
|
||||
$post = $this->categoryService->findCategory($id);
|
||||
|
||||
if ($post === null) {
|
||||
return response()->json(null, 404);
|
||||
}
|
||||
|
||||
return response()->json(CategoryResource::make($post));
|
||||
}
|
||||
|
||||
public function update(UpdateCategoryRequest $request, int $id): JsonResponse
|
||||
{
|
||||
$post = $this->categoryService->updateCategory($request->all(), $id);
|
||||
|
||||
if ($post === null) {
|
||||
return response()->json(null, 404);
|
||||
}
|
||||
|
||||
return response()->json(CategoryResource::make($post));
|
||||
}
|
||||
|
||||
public function destroy(DestroyCategoryRequest $post, int $id): JsonResponse
|
||||
{
|
||||
$isSuccessfullyDeleted = $this->categoryService->deleteCategory($id);
|
||||
return match ($isSuccessfullyDeleted) {
|
||||
false => response()->json(null, 404),
|
||||
true => response()->json(null, 204),
|
||||
};
|
||||
}
|
||||
}
|
||||
59
app/Http/Controllers/CommentController.php
Normal file
59
app/Http/Controllers/CommentController.php
Normal file
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Http\Requests\Comment\DestroyCommentRequest;
|
||||
use App\Http\Requests\Comment\ListCommentRequest;
|
||||
use App\Http\Requests\Comment\StoreCommentRequest;
|
||||
use App\Http\Requests\Comment\UpdateCommentRequest;
|
||||
use App\Http\Resources\CommentCollection;
|
||||
use App\Http\Resources\CommentResource;
|
||||
use App\Http\Resources\PaginableResource;
|
||||
use App\Services\Comment\PostCommentService;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
|
||||
class CommentController extends Controller
|
||||
{
|
||||
public function __construct(
|
||||
private readonly PostCommentService $commentService,
|
||||
) {
|
||||
}
|
||||
|
||||
public function list(ListCommentRequest $request, int $postId, int $page): JsonResponse
|
||||
{
|
||||
$comments = $this->commentService->fetchComments(
|
||||
$postId,
|
||||
$page,
|
||||
$request->filters(),
|
||||
$request->order()
|
||||
);
|
||||
return response()->json(PaginableResource::make($comments, CommentCollection::class));
|
||||
}
|
||||
|
||||
public function store(StoreCommentRequest $request, int $postId): JsonResponse
|
||||
{
|
||||
return response()->json(CommentResource::make($this->commentService->storeComment($request->all(), $postId)), 201);
|
||||
}
|
||||
|
||||
public function update(UpdateCommentRequest $request, int $postId, int $id): JsonResponse
|
||||
{
|
||||
$comment = $this->commentService->updateComment($request->all(), $postId, $id);
|
||||
|
||||
if ($comment === null) {
|
||||
return response()->json(null, 404);
|
||||
}
|
||||
|
||||
return response()->json(CommentResource::make($comment));
|
||||
}
|
||||
|
||||
public function destroy(DestroyCommentRequest $post, int $postId, int $id): JsonResponse
|
||||
{
|
||||
$isSuccessfullyDeleted = $this->commentService->deleteComment($postId, $id);
|
||||
return match ($isSuccessfullyDeleted) {
|
||||
false => response()->json(null, 404),
|
||||
true => response()->json(null, 204),
|
||||
};
|
||||
}
|
||||
}
|
||||
9
app/Http/Controllers/Controller.php
Normal file
9
app/Http/Controllers/Controller.php
Normal file
@@ -0,0 +1,9 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
abstract class Controller extends \Illuminate\Routing\Controller
|
||||
{
|
||||
}
|
||||
308
app/Http/Controllers/PostController.php
Normal file
308
app/Http/Controllers/PostController.php
Normal file
@@ -0,0 +1,308 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Http\Requests\Post\DestroyPostRequest;
|
||||
use App\Http\Requests\Post\ListPostRequest;
|
||||
use App\Http\Requests\Post\ShowPostRequest;
|
||||
use App\Http\Requests\Post\StorePostRequest;
|
||||
use App\Http\Requests\Post\UpdatePostRequest;
|
||||
use App\Http\Resources\PaginableResource;
|
||||
use App\Http\Resources\PostCollection;
|
||||
use App\Http\Resources\PostResource;
|
||||
use App\Services\Post\PostServiceInterface;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use OpenApi\Annotations as OA;
|
||||
|
||||
final class PostController extends Controller
|
||||
{
|
||||
public function __construct(
|
||||
private readonly PostServiceInterface $postService,
|
||||
)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* @OA\Get(
|
||||
* path="/api/v1/posts/{page}",
|
||||
* summary="List all posts",
|
||||
* description="Fetch a paginated list of all posts.",
|
||||
* tags={"Posts"},
|
||||
* @OA\Parameter(
|
||||
* name="direction",
|
||||
* in="query",
|
||||
* description="Order posts by ascending or descending",
|
||||
* required=false,
|
||||
* @OA\Schema(
|
||||
* type="string",
|
||||
* enum={"asc", "desc"},
|
||||
* example="asc"
|
||||
* )
|
||||
* ),
|
||||
* @OA\Parameter(
|
||||
* name="order",
|
||||
* in="query",
|
||||
* description="Order posts by column name",
|
||||
* required=false,
|
||||
* @OA\Schema(
|
||||
* type="string",
|
||||
* example="title"
|
||||
* )
|
||||
* ),
|
||||
* @OA\Parameter(
|
||||
* name="title",
|
||||
* in="query",
|
||||
* description="Filter posts by title",
|
||||
* required=false,
|
||||
* @OA\Schema(
|
||||
* type="string",
|
||||
* example="Sample Post"
|
||||
* )
|
||||
* ),
|
||||
* @OA\Parameter(
|
||||
* name="page",
|
||||
* in="path",
|
||||
* description="Pagination page number",
|
||||
* required=false,
|
||||
* @OA\Schema(
|
||||
* type="integer",
|
||||
* example=1
|
||||
* )
|
||||
* ),
|
||||
* @OA\Response(
|
||||
* response=200,
|
||||
* description="Successful response",
|
||||
* @OA\JsonContent(
|
||||
* type="object",
|
||||
* @OA\Property(
|
||||
* property="items",
|
||||
* ref="#/components/schemas/PostCollection"
|
||||
* ),
|
||||
* @OA\Property(
|
||||
* property="meta",
|
||||
* type="object",
|
||||
* description="Pagination metadata",
|
||||
* ref="#/components/schemas/PaginableResourceMeta"
|
||||
* )
|
||||
* )
|
||||
* ),
|
||||
* @OA\Response(
|
||||
* response=422,
|
||||
* description="Validation error",
|
||||
* @OA\JsonContent(ref="#/components/schemas/ValidationError")
|
||||
* ),
|
||||
* @OA\Response(
|
||||
* response=429,
|
||||
* description="Rate limiting. Try again later.",
|
||||
* )
|
||||
* )
|
||||
*/
|
||||
public function list(ListPostRequest $request, int $page): JsonResponse
|
||||
{
|
||||
$posts = $this->postService->fetchPosts(
|
||||
$page,
|
||||
$request->filters(),
|
||||
$request->order()
|
||||
);
|
||||
return response()->json(PaginableResource::make($posts, PostCollection::class));
|
||||
}
|
||||
|
||||
/**
|
||||
* @OA\Post(
|
||||
* path="/api/v1/posts",
|
||||
* operationId="storePost",
|
||||
* tags={"Posts"},
|
||||
* summary="Create a new post",
|
||||
* description="Creates a new post in the system based on the provided input data.",
|
||||
* @OA\RequestBody(
|
||||
* description="Payload for creating a new post",
|
||||
* required=true,
|
||||
* @OA\JsonContent(
|
||||
* type="object",
|
||||
* @OA\Property(property="title", type="string", maxLength=255, example="My First Post"),
|
||||
* @OA\Property(property="content", type="string", example="This is the content of my first post."),
|
||||
* @OA\Property(property="category_id", type="integer", example=1),
|
||||
* @OA\Property(property="tags", type="array", @OA\Items(type="string", example="Laravel")),
|
||||
* )
|
||||
* ),
|
||||
* @OA\Response(
|
||||
* response=201,
|
||||
* description="Post successfully created",
|
||||
* @OA\JsonContent(
|
||||
* type="object",
|
||||
* @OA\Property(property="status", type="string", example="success"),
|
||||
* @OA\Property(property="message", type="string", example="Post created successfully."),
|
||||
* @OA\Property(
|
||||
* property="data",
|
||||
* type="object",
|
||||
* ref="#/components/schemas/PostResource"
|
||||
* )
|
||||
* )
|
||||
* ),
|
||||
* @OA\Response(
|
||||
* response=422,
|
||||
* description="Validation error",
|
||||
* @OA\JsonContent(ref="#/components/schemas/ValidationError")
|
||||
* ),
|
||||
* @OA\Response(
|
||||
* response=429,
|
||||
* description="Rate limiting. Try again later.",
|
||||
* )
|
||||
* )
|
||||
*/
|
||||
public function store(StorePostRequest $request): JsonResponse
|
||||
{
|
||||
return response()->json(PostResource::make($this->postService->storePost($request->all())), 201);
|
||||
}
|
||||
|
||||
/**
|
||||
* @OA\Get(
|
||||
* path="/api/v1/posts/{id}",
|
||||
* operationId="getPostById",
|
||||
* tags={"Posts"},
|
||||
* summary="Get a specific post by ID",
|
||||
* description="Returns a specific post identified by its unique ID.",
|
||||
* @OA\Parameter(
|
||||
* name="id",
|
||||
* in="path",
|
||||
* description="ID of the post to retrieve",
|
||||
* required=true,
|
||||
* @OA\Schema(type="integer", example=1)
|
||||
* ),
|
||||
* @OA\Response(
|
||||
* response=200,
|
||||
* description="Post data retrieved successfully",
|
||||
* @OA\JsonContent(
|
||||
* type="object",
|
||||
* @OA\Property(property="status", type="string", example="success"),
|
||||
* @OA\Property(
|
||||
* property="data",
|
||||
* type="object",
|
||||
* ref="#/components/schemas/PostResource"
|
||||
* )
|
||||
* )
|
||||
* ),
|
||||
* @OA\Response(
|
||||
* response=404,
|
||||
* description="Post not found",
|
||||
* ),
|
||||
* @OA\Response(
|
||||
* response=429,
|
||||
* description="Rate limiting. Try again later.",
|
||||
* )
|
||||
* )
|
||||
*/
|
||||
public function show(ShowPostRequest $request, int $id): JsonResponse
|
||||
{
|
||||
$post = $this->postService->findPost($id);
|
||||
|
||||
if ($post === null) {
|
||||
return response()->json(null, 404);
|
||||
}
|
||||
|
||||
return response()->json(PostResource::make($post));
|
||||
}
|
||||
|
||||
/**
|
||||
* @OA\Put(
|
||||
* path="/api/v1/post/{id}",
|
||||
* operationId="updatePost",
|
||||
* tags={"Posts"},
|
||||
* summary="Update a specific post by ID",
|
||||
* description="Updates specific fields of a post identified by its unique ID.",
|
||||
* @OA\Parameter(
|
||||
* name="id",
|
||||
* in="path",
|
||||
* description="ID of the post to update",
|
||||
* required=true,
|
||||
* @OA\Schema(type="integer", example=1)
|
||||
* ),
|
||||
* @OA\RequestBody(
|
||||
* description="Payload for updating a post",
|
||||
* required=true,
|
||||
* @OA\JsonContent(
|
||||
* type="object",
|
||||
* @OA\Property(property="title", type="string", maxLength=255, example="Updated Title"),
|
||||
* @OA\Property(property="content", type="string", example="Updated content of the post."),
|
||||
* @OA\Property(property="category_id", type="integer", example=2),
|
||||
* @OA\Property(property="tags", type="array", @OA\Items(type="string", example="Laravel"))
|
||||
* )
|
||||
* ),
|
||||
* @OA\Response(
|
||||
* response=200,
|
||||
* description="Post data updated sucessfully",
|
||||
* @OA\JsonContent(
|
||||
* type="object",
|
||||
* @OA\Property(property="status", type="string", example="success"),
|
||||
* @OA\Property(
|
||||
* property="data",
|
||||
* type="object",
|
||||
* ref="#/components/schemas/PostResource"
|
||||
* )
|
||||
* )
|
||||
* ),
|
||||
* @OA\Response(
|
||||
* response=404,
|
||||
* description="Post not found",
|
||||
* ),
|
||||
* @OA\Response(
|
||||
* response=422,
|
||||
* description="Validation error",
|
||||
* @OA\JsonContent(ref="#/components/schemas/ValidationError")
|
||||
* ),
|
||||
* @OA\Response(
|
||||
* response=429,
|
||||
* description="Rate limiting. Try again later.",
|
||||
* )
|
||||
* )
|
||||
*/
|
||||
public function update(UpdatePostRequest $request, int $id): JsonResponse
|
||||
{
|
||||
$post = $this->postService->updatePost($request->all(), $id);
|
||||
|
||||
if ($post === null) {
|
||||
return response()->json(null, 404);
|
||||
}
|
||||
|
||||
return response()->json(PostResource::make($post));
|
||||
}
|
||||
|
||||
/**
|
||||
* @OA\Delete(
|
||||
* path="/api/v1/post/{id}",
|
||||
* operationId="deletePost",
|
||||
* tags={"Posts"},
|
||||
* summary="Delete a specific post by ID",
|
||||
* description="Deletes a specific post identified by its unique ID. This operation is irreversible.",
|
||||
* @OA\Parameter(
|
||||
* name="id",
|
||||
* in="path",
|
||||
* description="ID of the post to delete",
|
||||
* required=true,
|
||||
* @OA\Schema(type="integer", example=1)
|
||||
* ),
|
||||
* @OA\Response(
|
||||
* response=204,
|
||||
* description="Post successfully deleted",
|
||||
* ),
|
||||
* @OA\Response(
|
||||
* response=404,
|
||||
* description="Post not found",
|
||||
* ),
|
||||
* @OA\Response(
|
||||
* response=429,
|
||||
* description="Rate limiting. Try again later.",
|
||||
* )
|
||||
* )
|
||||
*/
|
||||
public function destroy(DestroyPostRequest $post, int $id): JsonResponse
|
||||
{
|
||||
$isSuccessfullyDeleted = $this->postService->deletePost($id);
|
||||
return match ($isSuccessfullyDeleted) {
|
||||
false => response()->json(null, 404),
|
||||
true => response()->json(null, 204),
|
||||
};
|
||||
}
|
||||
}
|
||||
41
app/Http/Requests/Category/DestroyCategoryRequest.php
Normal file
41
app/Http/Requests/Category/DestroyCategoryRequest.php
Normal file
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Http\Requests\Category;
|
||||
|
||||
use App\Http\Requests\InvalidDataResponseTrait;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class DestroyCategoryRequest extends FormRequest
|
||||
{
|
||||
use InvalidDataResponseTrait;
|
||||
|
||||
/**
|
||||
* @return array<string, string>
|
||||
*/
|
||||
public function all($keys = null): array
|
||||
{
|
||||
$request = parent::all($keys);
|
||||
|
||||
$request['id'] = $this->route('id');
|
||||
|
||||
return $request;
|
||||
}
|
||||
|
||||
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, string>
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'id' => 'required|integer|min:0',
|
||||
];
|
||||
}
|
||||
}
|
||||
74
app/Http/Requests/Category/ListCategoryRequest.php
Normal file
74
app/Http/Requests/Category/ListCategoryRequest.php
Normal file
@@ -0,0 +1,74 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Http\Requests\Category;
|
||||
|
||||
use App\Http\Requests\InvalidDataResponseTrait;
|
||||
use App\Services\QueryRequestModifiers\Category\CategoryFilter;
|
||||
use App\Services\QueryRequestModifiers\Category\CategoryFilterDTO;
|
||||
use App\Services\QueryRequestModifiers\Category\CategoryOrder;
|
||||
use App\Services\QueryRequestModifiers\Category\CategoryOrderDTO;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class ListCategoryRequest extends FormRequest
|
||||
{
|
||||
use InvalidDataResponseTrait;
|
||||
|
||||
public function __construct(
|
||||
private readonly CategoryOrder $categoryOrder,
|
||||
private readonly CategoryFilter $categoryFilter,
|
||||
) {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return array<string, string>
|
||||
*/
|
||||
public function all($keys = null): array
|
||||
{
|
||||
$request = parent::all($keys);
|
||||
|
||||
$request['page'] = $this->route('page');
|
||||
|
||||
return $request;
|
||||
}
|
||||
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, string>
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'page' => 'required|integer|min:1',
|
||||
... $this->categoryOrder->validateRules(),
|
||||
... $this->categoryFilter->validateRules()
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return array<string, string>
|
||||
*/
|
||||
public function attributes(): array
|
||||
{
|
||||
return [
|
||||
'page' => 'page number',
|
||||
];
|
||||
}
|
||||
|
||||
public function filters(): ?CategoryFilterDTO
|
||||
{
|
||||
return $this->categoryFilter->makeFromRequest($this);
|
||||
}
|
||||
|
||||
public function order(): ?CategoryOrderDTO
|
||||
{
|
||||
return $this->categoryOrder->makeFromRequest($this);
|
||||
}
|
||||
}
|
||||
45
app/Http/Requests/Category/ShowCategoryRequest.php
Normal file
45
app/Http/Requests/Category/ShowCategoryRequest.php
Normal file
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Http\Requests\Category;
|
||||
|
||||
use App\Http\Requests\InvalidDataResponseTrait;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class ShowCategoryRequest extends FormRequest
|
||||
{
|
||||
use InvalidDataResponseTrait;
|
||||
|
||||
/**
|
||||
* @return array<string, string>
|
||||
*/
|
||||
public function all($keys = null): array
|
||||
{
|
||||
$request = parent::all($keys);
|
||||
|
||||
$request['id'] = $this->route('id');
|
||||
|
||||
return $request;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*/
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*
|
||||
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'id' => 'required|integer|min:0',
|
||||
];
|
||||
}
|
||||
}
|
||||
47
app/Http/Requests/Category/StoreCategoryRequest.php
Normal file
47
app/Http/Requests/Category/StoreCategoryRequest.php
Normal file
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Http\Requests\Category;
|
||||
|
||||
use App\Http\Requests\InvalidDataResponseTrait;
|
||||
use App\Http\Resources\CategoryResource;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class StoreCategoryRequest extends FormRequest
|
||||
{
|
||||
use InvalidDataResponseTrait;
|
||||
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*/
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*
|
||||
* @return array<string, string>
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return static::rulesDefinition();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, string>
|
||||
*/
|
||||
public static function rulesDefinition(): array
|
||||
{
|
||||
return [
|
||||
'name' => 'required|string',
|
||||
];
|
||||
}
|
||||
|
||||
public function getCategory(): CategoryResource
|
||||
{
|
||||
return CategoryResource::make($this->all());
|
||||
}
|
||||
}
|
||||
47
app/Http/Requests/Category/UpdateCategoryRequest.php
Normal file
47
app/Http/Requests/Category/UpdateCategoryRequest.php
Normal file
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Http\Requests\Category;
|
||||
|
||||
use App\Http\Requests\InvalidDataResponseTrait;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class UpdateCategoryRequest extends FormRequest
|
||||
{
|
||||
use InvalidDataResponseTrait;
|
||||
|
||||
/**
|
||||
* @return array<string, string>
|
||||
*/
|
||||
public function all($keys = null): array
|
||||
{
|
||||
$request = parent::all($keys);
|
||||
|
||||
$request['id'] = $this->route('id');
|
||||
|
||||
return $request;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*/
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*
|
||||
* @return array<string, string>
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
...StoreCategoryRequest::rulesDefinition(),
|
||||
'id' => 'required|integer|min:0',
|
||||
];
|
||||
}
|
||||
}
|
||||
43
app/Http/Requests/Comment/DestroyCommentRequest.php
Normal file
43
app/Http/Requests/Comment/DestroyCommentRequest.php
Normal file
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Http\Requests\Comment;
|
||||
|
||||
use App\Http\Requests\InvalidDataResponseTrait;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class DestroyCommentRequest extends FormRequest
|
||||
{
|
||||
use InvalidDataResponseTrait;
|
||||
|
||||
/**
|
||||
* @return array<string, string>
|
||||
*/
|
||||
public function all($keys = null): array
|
||||
{
|
||||
$request = parent::all($keys);
|
||||
|
||||
$request['id'] = $this->route('id');
|
||||
$request['post_id'] = $this->route('post_id');
|
||||
|
||||
return $request;
|
||||
}
|
||||
|
||||
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, string>
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'id' => 'required|integer|exists:comments,id',
|
||||
'post_id' => 'required|exists:posts,id',
|
||||
];
|
||||
}
|
||||
}
|
||||
77
app/Http/Requests/Comment/ListCommentRequest.php
Normal file
77
app/Http/Requests/Comment/ListCommentRequest.php
Normal file
@@ -0,0 +1,77 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Http\Requests\Comment;
|
||||
|
||||
use App\Http\Requests\InvalidDataResponseTrait;
|
||||
use App\Services\QueryRequestModifiers\Comment\CommentFilter;
|
||||
use App\Services\QueryRequestModifiers\Comment\CommentFilterDTO;
|
||||
use App\Services\QueryRequestModifiers\Comment\CommentOrder;
|
||||
use App\Services\QueryRequestModifiers\Comment\CommentOrderDTO;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class ListCommentRequest extends FormRequest
|
||||
{
|
||||
use InvalidDataResponseTrait;
|
||||
|
||||
public function __construct(
|
||||
private readonly CommentOrder $commentOrder,
|
||||
private readonly CommentFilter $commentFilter,
|
||||
) {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return array<string, string>
|
||||
*/
|
||||
public function all($keys = null): array
|
||||
{
|
||||
$request = parent::all($keys);
|
||||
|
||||
$request['page'] = $this->route('page');
|
||||
$request['post_id'] = $this->route('post_id');
|
||||
|
||||
return $request;
|
||||
}
|
||||
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, string>
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'page' => 'required|integer|min:1',
|
||||
'post_id' => 'required|integer|min:0',
|
||||
... $this->commentFilter->validateRules(),
|
||||
... $this->commentOrder->validateRules()
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return array<string, string>
|
||||
*/
|
||||
public function attributes(): array
|
||||
{
|
||||
return [
|
||||
'page' => 'page number',
|
||||
'post_id' => 'post id',
|
||||
];
|
||||
}
|
||||
|
||||
public function filters(): ?CommentFilterDTO
|
||||
{
|
||||
return $this->commentFilter->makeFromRequest($this);
|
||||
}
|
||||
|
||||
public function order(): ?CommentOrderDTO
|
||||
{
|
||||
return $this->commentOrder->makeFromRequest($this);
|
||||
}
|
||||
}
|
||||
49
app/Http/Requests/Comment/StoreCommentRequest.php
Normal file
49
app/Http/Requests/Comment/StoreCommentRequest.php
Normal file
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Http\Requests\Comment;
|
||||
|
||||
use App\Http\Requests\InvalidDataResponseTrait;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class StoreCommentRequest extends FormRequest
|
||||
{
|
||||
use InvalidDataResponseTrait;
|
||||
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, string>
|
||||
*/
|
||||
public function all($keys = null): array
|
||||
{
|
||||
$request = parent::all($keys);
|
||||
|
||||
$request['post_id'] = $this->route('post_id');
|
||||
|
||||
return $request;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return static::rulesDefinition();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, string>
|
||||
*/
|
||||
public static function rulesDefinition()
|
||||
{
|
||||
return [
|
||||
'content' => 'required|string',
|
||||
'post_id' => 'required|exists:posts,id',
|
||||
];
|
||||
}
|
||||
}
|
||||
46
app/Http/Requests/Comment/UpdateCommentRequest.php
Normal file
46
app/Http/Requests/Comment/UpdateCommentRequest.php
Normal file
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Http\Requests\Comment;
|
||||
|
||||
use App\Http\Requests\InvalidDataResponseTrait;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class UpdateCommentRequest extends FormRequest
|
||||
{
|
||||
use InvalidDataResponseTrait;
|
||||
|
||||
/**
|
||||
* @return array<string, string>
|
||||
*/
|
||||
public function all($keys = null): array
|
||||
{
|
||||
$request = parent::all($keys);
|
||||
|
||||
$request['id'] = $this->route('id');
|
||||
$request['post_id'] = $this->route('post_id');
|
||||
|
||||
return $request;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*/
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
...StoreCommentRequest::rulesDefinition(),
|
||||
'id' => 'required|integer|exists:comments,id',
|
||||
];
|
||||
}
|
||||
}
|
||||
35
app/Http/Requests/InvalidDataResponseTrait.php
Normal file
35
app/Http/Requests/InvalidDataResponseTrait.php
Normal file
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Http\Requests;
|
||||
|
||||
use Illuminate\Contracts\Validation\Validator;
|
||||
use Illuminate\Http\Exceptions\HttpResponseException;
|
||||
use OpenApi\Annotations as OA;
|
||||
|
||||
/**
|
||||
* @OA\Schema(
|
||||
* schema="ValidationError",
|
||||
* type="object",
|
||||
* @OA\Property(property="message", type="string", example="The given data was invalid."),
|
||||
* @OA\Property(
|
||||
* property="errors",
|
||||
* type="object",
|
||||
* @OA\AdditionalProperties(
|
||||
* type="array",
|
||||
* @OA\Items(type="string", example="The title field is required.")
|
||||
* )
|
||||
* )
|
||||
* )
|
||||
*/
|
||||
trait InvalidDataResponseTrait
|
||||
{
|
||||
protected function failedValidation(Validator $validator): void
|
||||
{
|
||||
throw new HttpResponseException(response()->json([
|
||||
'message' => 'Invalid data.',
|
||||
'errors' => $validator->errors(),
|
||||
], 422));
|
||||
}
|
||||
}
|
||||
41
app/Http/Requests/Post/DestroyPostRequest.php
Normal file
41
app/Http/Requests/Post/DestroyPostRequest.php
Normal file
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Http\Requests\Post;
|
||||
|
||||
use App\Http\Requests\InvalidDataResponseTrait;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class DestroyPostRequest extends FormRequest
|
||||
{
|
||||
use InvalidDataResponseTrait;
|
||||
|
||||
/**
|
||||
* @return array<string, string>
|
||||
*/
|
||||
public function all($keys = null): array
|
||||
{
|
||||
$request = parent::all($keys);
|
||||
|
||||
$request['id'] = $this->route('id');
|
||||
|
||||
return $request;
|
||||
}
|
||||
|
||||
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, string>
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'id' => 'required|integer|min:0',
|
||||
];
|
||||
}
|
||||
}
|
||||
73
app/Http/Requests/Post/ListPostRequest.php
Normal file
73
app/Http/Requests/Post/ListPostRequest.php
Normal file
@@ -0,0 +1,73 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Http\Requests\Post;
|
||||
|
||||
use App\Http\Requests\InvalidDataResponseTrait;
|
||||
use App\Services\QueryRequestModifiers\Post\PostFilter;
|
||||
use App\Services\QueryRequestModifiers\Post\PostFilterDTO;
|
||||
use App\Services\QueryRequestModifiers\Post\PostOrder;
|
||||
use App\Services\QueryRequestModifiers\Post\PostOrderDTO;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class ListPostRequest extends FormRequest
|
||||
{
|
||||
use InvalidDataResponseTrait;
|
||||
|
||||
public function __construct(
|
||||
private readonly PostOrder $postOrder,
|
||||
private readonly PostFilter $postFilter,
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, string>
|
||||
*/
|
||||
public function all($keys = null): array
|
||||
{
|
||||
$request = parent::all($keys);
|
||||
|
||||
$request['page'] = $this->route('page');
|
||||
|
||||
return $request;
|
||||
}
|
||||
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, string>
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'page' => 'required|integer|min:1',
|
||||
... $this->postFilter->validateRules(),
|
||||
... $this->postOrder->validateRules()
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, string>
|
||||
*/
|
||||
public function attributes(): array
|
||||
{
|
||||
return [
|
||||
'page' => 'page number',
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
public function filters(): ?PostFilterDTO
|
||||
{
|
||||
return $this->postFilter->makeFromRequest($this);
|
||||
}
|
||||
|
||||
public function order(): ?PostOrderDTO
|
||||
{
|
||||
return $this->postOrder->makeFromRequest($this);
|
||||
}
|
||||
}
|
||||
45
app/Http/Requests/Post/ShowPostRequest.php
Normal file
45
app/Http/Requests/Post/ShowPostRequest.php
Normal file
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Http\Requests\Post;
|
||||
|
||||
use App\Http\Requests\InvalidDataResponseTrait;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class ShowPostRequest extends FormRequest
|
||||
{
|
||||
use InvalidDataResponseTrait;
|
||||
|
||||
/**
|
||||
* @return array<string, string>
|
||||
*/
|
||||
public function all($keys = null): array
|
||||
{
|
||||
$request = parent::all($keys);
|
||||
|
||||
$request['id'] = $this->route('id');
|
||||
|
||||
return $request;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*/
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*
|
||||
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'id' => 'required|integer|min:0',
|
||||
];
|
||||
}
|
||||
}
|
||||
50
app/Http/Requests/Post/StorePostRequest.php
Normal file
50
app/Http/Requests/Post/StorePostRequest.php
Normal file
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Http\Requests\Post;
|
||||
|
||||
use App\Http\Requests\InvalidDataResponseTrait;
|
||||
use App\Http\Resources\PostResource;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class StorePostRequest extends FormRequest
|
||||
{
|
||||
use InvalidDataResponseTrait;
|
||||
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*/
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*
|
||||
* @return array<string, string>
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return static::rulesDefinition();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, string>
|
||||
*/
|
||||
public static function rulesDefinition(): array
|
||||
{
|
||||
return [
|
||||
'title' => 'required|string|max:255',
|
||||
'content' => 'required|string',
|
||||
'category_id' => 'nullable|exists:categories,id',
|
||||
'tags' => 'nullable|array',
|
||||
];
|
||||
}
|
||||
|
||||
public function getPost(): PostResource
|
||||
{
|
||||
return PostResource::make($this->all());
|
||||
}
|
||||
}
|
||||
47
app/Http/Requests/Post/UpdatePostRequest.php
Normal file
47
app/Http/Requests/Post/UpdatePostRequest.php
Normal file
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Http\Requests\Post;
|
||||
|
||||
use App\Http\Requests\InvalidDataResponseTrait;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class UpdatePostRequest extends FormRequest
|
||||
{
|
||||
use InvalidDataResponseTrait;
|
||||
|
||||
/**
|
||||
* @return array<string, string>
|
||||
*/
|
||||
public function all($keys = null): array
|
||||
{
|
||||
$request = parent::all($keys);
|
||||
|
||||
$request['id'] = $this->route('id');
|
||||
|
||||
return $request;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*/
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*
|
||||
* @return array<string, string>
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
...StorePostRequest::rulesDefinition(),
|
||||
'id' => 'required|integer|min:0',
|
||||
];
|
||||
}
|
||||
}
|
||||
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