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(),
|
||||
];
|
||||
}
|
||||
}
|
||||
32
app/Models/Category.php
Normal file
32
app/Models/Category.php
Normal file
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Carbon\CarbonInterface;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
|
||||
/**
|
||||
* @property int $id
|
||||
* @property string $name
|
||||
* @property CarbonInterface $created_at
|
||||
* @property CarbonInterface $updated_at
|
||||
*/
|
||||
class Category extends Model
|
||||
{
|
||||
/** @use HasFactory<\Database\Factories\CategoryFactory> */
|
||||
use HasFactory;
|
||||
|
||||
protected $fillable = ['name'];
|
||||
|
||||
/**
|
||||
* @return HasMany<Post, $this>
|
||||
*/
|
||||
public function posts(): HasMany
|
||||
{
|
||||
return $this->hasMany(Post::class);
|
||||
}
|
||||
}
|
||||
31
app/Models/Comment.php
Normal file
31
app/Models/Comment.php
Normal file
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Carbon\CarbonInterface;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
/**
|
||||
* @property int $id
|
||||
* @property string $content
|
||||
* @property CarbonInterface $created_at
|
||||
* @property CarbonInterface $updated_at
|
||||
*/
|
||||
class Comment extends Model
|
||||
{
|
||||
/** @use HasFactory<\Database\Factories\CommentFactory> */
|
||||
use HasFactory;
|
||||
|
||||
protected $fillable = ['content'];
|
||||
|
||||
/**
|
||||
* @return \Illuminate\Database\Eloquent\Relations\MorphTo<Model, $this>
|
||||
*/
|
||||
public function commentable()
|
||||
{
|
||||
return $this->morphTo();
|
||||
}
|
||||
}
|
||||
61
app/Models/Post.php
Normal file
61
app/Models/Post.php
Normal file
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Carbon\CarbonInterface;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||
|
||||
/**
|
||||
* @property int $id
|
||||
* @property string $title
|
||||
* @property string $content
|
||||
* @property ?Category $category
|
||||
* @property int $category_id
|
||||
* @property CarbonInterface $created_at
|
||||
* @property CarbonInterface $updated_at
|
||||
*/
|
||||
class Post extends Model
|
||||
{
|
||||
/** @use HasFactory<\Database\Factories\PostFactory> */
|
||||
use HasFactory;
|
||||
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*
|
||||
* @var list<string>
|
||||
*/
|
||||
protected $fillable = [
|
||||
'title',
|
||||
'content',
|
||||
'category_id',
|
||||
];
|
||||
|
||||
/**
|
||||
* @return BelongsTo<Category, $this>
|
||||
*/
|
||||
public function category()
|
||||
{
|
||||
return $this->belongsTo(Category::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return BelongsToMany<Tag, $this>
|
||||
*/
|
||||
public function tags()
|
||||
{
|
||||
return $this->belongsToMany(Tag::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \Illuminate\Database\Eloquent\Relations\MorphMany<Comment, $this>
|
||||
*/
|
||||
public function comments()
|
||||
{
|
||||
return $this->morphMany(Comment::class, 'commentable');
|
||||
}
|
||||
}
|
||||
32
app/Models/Tag.php
Normal file
32
app/Models/Tag.php
Normal file
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Carbon\CarbonInterface;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||
|
||||
/**
|
||||
* @property int $id
|
||||
* @property string $name
|
||||
* @property CarbonInterface $created_at
|
||||
* @property CarbonInterface $updated_at
|
||||
*/
|
||||
class Tag extends Model
|
||||
{
|
||||
/** @use HasFactory<\Database\Factories\TagFactory> */
|
||||
use HasFactory;
|
||||
|
||||
protected $fillable = ['name'];
|
||||
|
||||
/**
|
||||
* @return BelongsToMany<Post, $this>
|
||||
*/
|
||||
public function posts()
|
||||
{
|
||||
return $this->belongsToMany(Post::class);
|
||||
}
|
||||
}
|
||||
51
app/Models/User.php
Normal file
51
app/Models/User.php
Normal file
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
// use Illuminate\Contracts\Auth\MustVerifyEmail;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Foundation\Auth\User as Authenticatable;
|
||||
use Illuminate\Notifications\Notifiable;
|
||||
|
||||
class User extends Authenticatable
|
||||
{
|
||||
/** @use HasFactory<\Database\Factories\UserFactory> */
|
||||
use HasFactory;
|
||||
use Notifiable;
|
||||
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*
|
||||
* @var list<string>
|
||||
*/
|
||||
protected $fillable = [
|
||||
'name',
|
||||
'email',
|
||||
'password',
|
||||
];
|
||||
|
||||
/**
|
||||
* The attributes that should be hidden for serialization.
|
||||
*
|
||||
* @var list<string>
|
||||
*/
|
||||
protected $hidden = [
|
||||
'password',
|
||||
'remember_token',
|
||||
];
|
||||
|
||||
/**
|
||||
* Get the attributes that should be cast.
|
||||
*
|
||||
* @return array<string, string>
|
||||
*/
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'email_verified_at' => 'datetime',
|
||||
'password' => 'hashed',
|
||||
];
|
||||
}
|
||||
}
|
||||
26
app/Providers/AppServiceProvider.php
Normal file
26
app/Providers/AppServiceProvider.php
Normal file
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Providers;
|
||||
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
|
||||
class AppServiceProvider extends ServiceProvider
|
||||
{
|
||||
/**
|
||||
* Register any application services.
|
||||
*/
|
||||
public function register(): void
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Bootstrap any application services.
|
||||
*/
|
||||
public function boot(): void
|
||||
{
|
||||
//
|
||||
}
|
||||
}
|
||||
44
app/Providers/CategoryServiceProvider.php
Normal file
44
app/Providers/CategoryServiceProvider.php
Normal file
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Providers;
|
||||
|
||||
use App\Services\Category\CategoryService;
|
||||
use App\Services\Category\CategoryServiceInterface;
|
||||
use App\Services\QueryRequestModifiers\Category\CategoryFilter;
|
||||
use App\Services\QueryRequestModifiers\Category\CategoryOrder;
|
||||
use Illuminate\Foundation\Application;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
|
||||
class CategoryServiceProvider extends ServiceProvider
|
||||
{
|
||||
/**
|
||||
* Register services.
|
||||
*/
|
||||
public function register(): void
|
||||
{
|
||||
$this->app->singleton(CategoryFilter::class, function (Application $app) {
|
||||
return new CategoryFilter();
|
||||
});
|
||||
|
||||
$this->app->singleton(CategoryOrder::class, function (Application $app) {
|
||||
return new CategoryOrder();
|
||||
});
|
||||
|
||||
$this->app->singleton(CategoryServiceInterface::class, function (Application $app) {
|
||||
return new CategoryService(
|
||||
$this->app->get(CategoryFilter::class),
|
||||
$this->app->get(CategoryOrder::class),
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Bootstrap services.
|
||||
*/
|
||||
public function boot(): void
|
||||
{
|
||||
//
|
||||
}
|
||||
}
|
||||
44
app/Providers/CommentServiceProvider.php
Normal file
44
app/Providers/CommentServiceProvider.php
Normal file
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Providers;
|
||||
|
||||
use App\Services\Comment\PostCommentService;
|
||||
use App\Services\QueryRequestModifiers\Comment\CommentFilter;
|
||||
use App\Services\QueryRequestModifiers\Comment\CommentOrder;
|
||||
use App\Services\QueryRequestModifiers\Comment\CommentOrderDTO;
|
||||
use Illuminate\Foundation\Application;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
|
||||
class CommentServiceProvider extends ServiceProvider
|
||||
{
|
||||
/**
|
||||
* Register services.
|
||||
*/
|
||||
public function register(): void
|
||||
{
|
||||
$this->app->singleton(CommentFilter::class, function (Application $app) {
|
||||
return new CommentFilter();
|
||||
});
|
||||
|
||||
$this->app->singleton(CommentOrder::class, function (Application $app) {
|
||||
return new CommentOrder();
|
||||
});
|
||||
|
||||
$this->app->singleton(PostCommentService::class, function (Application $app) {
|
||||
return new PostCommentService(
|
||||
$this->app->get(CommentFilter::class),
|
||||
$this->app->get(CommentOrder::class),
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Bootstrap services.
|
||||
*/
|
||||
public function boot(): void
|
||||
{
|
||||
//
|
||||
}
|
||||
}
|
||||
46
app/Providers/PostServiceProvider.php
Normal file
46
app/Providers/PostServiceProvider.php
Normal file
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Providers;
|
||||
|
||||
use App\Services\Comment\PostCommentService;
|
||||
use App\Services\Post\CachedPostService;
|
||||
use App\Services\CacheKeyBuilder;
|
||||
use App\Services\Post\PostServiceInterface;
|
||||
use App\Services\QueryRequestModifiers\Post\PostFilter;
|
||||
use App\Services\QueryRequestModifiers\Post\PostOrder;
|
||||
use Illuminate\Foundation\Application;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
|
||||
class PostServiceProvider extends ServiceProvider
|
||||
{
|
||||
/**
|
||||
* Register services.
|
||||
*/
|
||||
public function register(): void
|
||||
{
|
||||
$this->app->singleton(PostFilter::class, function (Application $app) {
|
||||
return new PostFilter();
|
||||
});
|
||||
|
||||
$this->app->singleton(PostOrder::class, function (Application $app) {
|
||||
return new PostOrder();
|
||||
});
|
||||
|
||||
$this->app->singleton(PostServiceInterface::class, function (Application $app) {
|
||||
return new CachedPostService(
|
||||
$this->app->get(PostFilter::class),
|
||||
$this->app->get(PostOrder::class),
|
||||
$this->app->get(CacheKeyBuilder::class),
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Bootstrap services.
|
||||
*/
|
||||
public function boot(): void
|
||||
{
|
||||
}
|
||||
}
|
||||
31
app/Providers/SwaggerProvider.php
Normal file
31
app/Providers/SwaggerProvider.php
Normal file
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Providers;
|
||||
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
use L5Swagger\L5SwaggerServiceProvider;
|
||||
use OpenApi\Annotations as OA;
|
||||
|
||||
/**
|
||||
* @OA\Info(
|
||||
* title="Post API",
|
||||
* version="1.0.0",
|
||||
* )
|
||||
*/
|
||||
class SwaggerProvider extends ServiceProvider
|
||||
{
|
||||
public function register()
|
||||
{
|
||||
$this->app->register(L5SwaggerServiceProvider::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Bootstrap services.
|
||||
*/
|
||||
public function boot(): void
|
||||
{
|
||||
//
|
||||
}
|
||||
}
|
||||
32
app/Services/CacheKeyBuilder.php
Normal file
32
app/Services/CacheKeyBuilder.php
Normal file
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
class CacheKeyBuilder
|
||||
{
|
||||
/**
|
||||
* @param mixed ...$args
|
||||
* @return string
|
||||
*/
|
||||
public function buildCacheKeyFromArgs(...$args): string
|
||||
{
|
||||
$cacheName = '';
|
||||
foreach ($args as $key => $arg) {
|
||||
if (is_array($arg)) {
|
||||
$cacheName .= http_build_query($arg, '', '|');
|
||||
} elseif ($arg === null) {
|
||||
$cacheName .= '|N';
|
||||
} elseif (is_scalar($arg)) {
|
||||
$cacheName .= '|' . (string) $arg;
|
||||
} elseif ($arg instanceof CacheKeyInterface) {
|
||||
$cacheName .= $arg->toCacheKey();
|
||||
} else {
|
||||
throw new \InvalidArgumentException("Invalid argument $key type for key generator.");
|
||||
}
|
||||
}
|
||||
|
||||
return $cacheName;
|
||||
}
|
||||
}
|
||||
10
app/Services/CacheKeyInterface.php
Normal file
10
app/Services/CacheKeyInterface.php
Normal file
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
interface CacheKeyInterface
|
||||
{
|
||||
public function toCacheKey(): string;
|
||||
}
|
||||
70
app/Services/Category/CategoryService.php
Normal file
70
app/Services/Category/CategoryService.php
Normal file
@@ -0,0 +1,70 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Services\Category;
|
||||
|
||||
use App\Models\Category;
|
||||
use App\Services\PaginableResource;
|
||||
use App\Services\QueryRequestModifiers\Category\CategoryFilter;
|
||||
use App\Services\QueryRequestModifiers\Category\CategoryFilterDTO;
|
||||
use App\Services\QueryRequestModifiers\Category\CategoryOrder;
|
||||
use App\Services\QueryRequestModifiers\Category\CategoryOrderDTO;
|
||||
|
||||
class CategoryService implements CategoryServiceInterface
|
||||
{
|
||||
public function __construct(
|
||||
protected readonly CategoryFilter $categoryFilter,
|
||||
protected readonly CategoryOrder $categoryOrder,
|
||||
protected readonly int $paginate = 10,
|
||||
) {
|
||||
}
|
||||
|
||||
public function fetchCategories(int $page, ?CategoryFilterDTO $filters, ?CategoryOrderDTO $orderDef): PaginableResource
|
||||
{
|
||||
$categories = $this->categoryFilter->apply(
|
||||
$this->categoryOrder->apply(Category::query(), $orderDef),
|
||||
$filters
|
||||
)->paginate($this->paginate);
|
||||
return PaginableResource::createFromLengthAwarePaginator($categories);
|
||||
}
|
||||
|
||||
public function findCategory(int $id): ?Category
|
||||
{
|
||||
return Category::find($id);
|
||||
}
|
||||
|
||||
public function storeCategory(array $data): Category
|
||||
{
|
||||
$category = Category::create($data);
|
||||
return Category::findOrFail($category->id);
|
||||
}
|
||||
|
||||
public function updateCategory(array $data, int $id): ?Category
|
||||
{
|
||||
$category = Category::find($id);
|
||||
|
||||
if ($category === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$category->update($data);
|
||||
return Category::findOrFail($id);
|
||||
}
|
||||
|
||||
public function deleteCategory(int $id): bool
|
||||
{
|
||||
$category = Category::find($id);
|
||||
|
||||
if ($category === null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($category->posts()->count() > 0) {
|
||||
// TODO: return code
|
||||
return false;
|
||||
}
|
||||
|
||||
return $category->delete();
|
||||
}
|
||||
}
|
||||
33
app/Services/Category/CategoryServiceInterface.php
Normal file
33
app/Services/Category/CategoryServiceInterface.php
Normal file
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Services\Category;
|
||||
|
||||
use App\Models\Category;
|
||||
use App\Services\PaginableResource;
|
||||
use App\Services\QueryRequestModifiers\Category\CategoryFilterDTO;
|
||||
use App\Services\QueryRequestModifiers\Category\CategoryOrderDTO;
|
||||
|
||||
interface CategoryServiceInterface
|
||||
{
|
||||
/**
|
||||
* @return PaginableResource<Category>
|
||||
*/
|
||||
public function fetchCategories(int $page, ?CategoryFilterDTO $filters, ?CategoryOrderDTO $orderDef): PaginableResource;
|
||||
|
||||
public function findCategory(int $id): ?Category;
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $data
|
||||
* @return Category
|
||||
*/
|
||||
public function storeCategory(array $data): Category;
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $data
|
||||
*/
|
||||
public function updateCategory(array $data, int $id): ?Category;
|
||||
|
||||
public function deleteCategory(int $id): bool;
|
||||
}
|
||||
30
app/Services/Comment/CommentServiceInterface.php
Normal file
30
app/Services/Comment/CommentServiceInterface.php
Normal file
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Services\Comment;
|
||||
|
||||
use App\Models\Comment;
|
||||
use App\Services\PaginableResource;
|
||||
use App\Services\QueryRequestModifiers\Comment\CommentFilterDTO;
|
||||
use App\Services\QueryRequestModifiers\Comment\CommentOrderDTO;
|
||||
|
||||
interface CommentServiceInterface
|
||||
{
|
||||
/**
|
||||
* @return PaginableResource<Comment>
|
||||
*/
|
||||
public function fetchComments(int $remoteId, int $page, ?CommentFilterDTO $filters, ?CommentOrderDTO $orderDef): PaginableResource;
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $data
|
||||
*/
|
||||
public function storeComment(array $data, int $postId): ?Comment;
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $data
|
||||
*/
|
||||
public function updateComment(array $data, int $remoteId, int $id): ?Comment;
|
||||
|
||||
public function deleteComment(int $remoteId, int $id): bool;
|
||||
}
|
||||
82
app/Services/Comment/PostCommentService.php
Normal file
82
app/Services/Comment/PostCommentService.php
Normal file
@@ -0,0 +1,82 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Services\Comment;
|
||||
|
||||
use App\Models\Comment;
|
||||
use App\Models\Post;
|
||||
use App\Services\PaginableResource;
|
||||
use App\Services\QueryRequestModifiers\Comment\CommentFilter;
|
||||
use App\Services\QueryRequestModifiers\Comment\CommentFilterDTO;
|
||||
use App\Services\QueryRequestModifiers\Comment\CommentOrder;
|
||||
use App\Services\QueryRequestModifiers\Comment\CommentOrderDTO;
|
||||
|
||||
class PostCommentService implements CommentServiceInterface
|
||||
{
|
||||
public function __construct(
|
||||
protected readonly CommentFilter $commentFilter,
|
||||
protected readonly CommentOrder $commentOrder,
|
||||
protected readonly int $paginate = 10,
|
||||
) {
|
||||
}
|
||||
|
||||
public function fetchComments(int $remoteId, int $page, ?CommentFilterDTO $filters, ?CommentOrderDTO $orderDef): PaginableResource
|
||||
{
|
||||
$post = Post::findOrFail($remoteId);
|
||||
|
||||
$comments = $this->commentOrder->apply(
|
||||
$this->commentFilter->apply($post->comments()->getQuery(), $filters),
|
||||
$orderDef
|
||||
)->paginate($this->paginate);
|
||||
|
||||
return PaginableResource::createFromLengthAwarePaginator($comments);
|
||||
}
|
||||
|
||||
public function storeComment(array $data, int $postId): ?Comment
|
||||
{
|
||||
$post = Post::findOrFail($postId);
|
||||
|
||||
$comment = $post->comments()->create([
|
||||
'content' => $data['content'],
|
||||
]);
|
||||
|
||||
return Comment::findOrFail($comment->id);
|
||||
}
|
||||
|
||||
public function deleteComment(int $remoteId, int $id): bool
|
||||
{
|
||||
$post = Post::find($remoteId);
|
||||
|
||||
if ($post === null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$comment = $post->comments()->find($id);
|
||||
|
||||
if ($comment === null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $comment->delete();
|
||||
}
|
||||
|
||||
public function updateComment(array $data, int $remoteId, int $id): ?Comment
|
||||
{
|
||||
$post = Post::find($remoteId);
|
||||
|
||||
if ($post === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$comment = $post->comments()->find($id);
|
||||
|
||||
if ($comment === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$comment->update($data);
|
||||
|
||||
return Comment::findOrFail($comment->id);
|
||||
}
|
||||
}
|
||||
30
app/Services/PaginableResource.php
Normal file
30
app/Services/PaginableResource.php
Normal file
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
|
||||
|
||||
/**
|
||||
* @template T
|
||||
*/
|
||||
class PaginableResource
|
||||
{
|
||||
/**
|
||||
* @param array<int, T> $data
|
||||
* @param int $totalCount
|
||||
*/
|
||||
public function __construct(public array $data, public int $totalCount, public int $totalPages)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* @param LengthAwarePaginator<int, T> $paginator
|
||||
* @return PaginableResource<T>
|
||||
*/
|
||||
public static function createFromLengthAwarePaginator(LengthAwarePaginator $paginator): PaginableResource
|
||||
{
|
||||
return new PaginableResource($paginator->items(), $paginator->total(), (int)ceil($paginator->total() / $paginator->perPage()));
|
||||
}
|
||||
}
|
||||
75
app/Services/Post/CachedPostService.php
Normal file
75
app/Services/Post/CachedPostService.php
Normal file
@@ -0,0 +1,75 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Services\Post;
|
||||
|
||||
use App\Models\Post;
|
||||
use App\Services\CacheKeyBuilder;
|
||||
use App\Services\PaginableResource;
|
||||
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\Support\Facades\Cache;
|
||||
|
||||
class CachedPostService extends PostService
|
||||
{
|
||||
public function __construct(
|
||||
PostFilter $postFilter,
|
||||
PostOrder $postOrder,
|
||||
protected readonly CacheKeyBuilder $cacheKeyBuilder,
|
||||
protected readonly int $cacheTtl = 60,
|
||||
) {
|
||||
parent::__construct($postFilter, $postOrder);
|
||||
}
|
||||
|
||||
public function fetchPosts(int $page, ?PostFilterDTO $filters, ?PostOrderDTO $orderDef): PaginableResource
|
||||
{
|
||||
return Cache::tags(['posts', 'fetch-posts', 'post-page-' . $page])
|
||||
->remember(
|
||||
$this->cacheKeyBuilder->buildCacheKeyFromArgs('post-page', $page, $filters, $orderDef),
|
||||
$this->cacheTtl,
|
||||
function () use ($page, $filters, $orderDef) {
|
||||
return parent::fetchPosts($page, $filters, $orderDef);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
public function findPost(int $id): ?Post
|
||||
{
|
||||
return Cache::tags(['posts', 'find-post-' . $id])
|
||||
->remember('post-' . $id, $this->cacheTtl, function () use ($id) {
|
||||
return parent::findPost($id);
|
||||
});
|
||||
}
|
||||
|
||||
public function storePost(array $data): Post
|
||||
{
|
||||
$newPost = parent::storePost($data);
|
||||
|
||||
Cache::tags('fetch-posts')->flush();
|
||||
|
||||
return $newPost;
|
||||
}
|
||||
|
||||
public function updatePost(array $data, int $id): ?Post
|
||||
{
|
||||
$updatedPost = parent::updatePost($data, $id);
|
||||
|
||||
if ($updatedPost !== null) {
|
||||
Cache::tags('fetch-posts')->flush();
|
||||
Cache::tags('find-post-' . $id)->flush();
|
||||
}
|
||||
|
||||
return $updatedPost;
|
||||
}
|
||||
|
||||
public function deletePost(int $id): bool
|
||||
{
|
||||
Cache::tags('fetch-posts')->flush();
|
||||
Cache::tags('find-post-' . $id)->flush();
|
||||
|
||||
return parent::deletePost($id); // TODO: Change the autogenerated stub
|
||||
}
|
||||
}
|
||||
106
app/Services/Post/PostService.php
Normal file
106
app/Services/Post/PostService.php
Normal file
@@ -0,0 +1,106 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Services\Post;
|
||||
|
||||
use App\Models\Post;
|
||||
use App\Models\Tag;
|
||||
use App\Services\PaginableResource;
|
||||
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\Support\Facades\DB;
|
||||
|
||||
class PostService implements PostServiceInterface
|
||||
{
|
||||
public function __construct(
|
||||
protected readonly PostFilter $postFilter,
|
||||
protected readonly PostOrder $postOrder,
|
||||
protected readonly int $paginate = 10,
|
||||
) {
|
||||
}
|
||||
|
||||
public function fetchPosts(int $page, ?PostFilterDTO $filters, ?PostOrderDTO $orderDef): PaginableResource
|
||||
{
|
||||
$posts = $this->postOrder->apply(
|
||||
$this->postFilter->apply(Post::with(['category', 'tags', 'comments']), $filters),
|
||||
$orderDef
|
||||
)->paginate($this->paginate, page: $page);
|
||||
return PaginableResource::createFromLengthAwarePaginator($posts);
|
||||
}
|
||||
|
||||
public function findPost(int $id): ?Post
|
||||
{
|
||||
return Post::with(['category', 'tags', 'comments'])->find($id);
|
||||
}
|
||||
|
||||
public function storePost(array $data): Post
|
||||
{
|
||||
DB::beginTransaction();
|
||||
|
||||
$post = Post::create($data);
|
||||
|
||||
if (isset($data['tags'])) {
|
||||
$tags = [];
|
||||
|
||||
foreach ($data['tags'] as $tag) {
|
||||
$tag = Tag::firstOrCreate(['name' => $tag]);
|
||||
$tags[] = $tag;
|
||||
}
|
||||
|
||||
$post->tags()->sync($tags);
|
||||
}
|
||||
|
||||
$post = $this->findPost($post->id);
|
||||
|
||||
if ($post === null) {
|
||||
throw new \InvalidArgumentException('This should never happen - post is null');
|
||||
}
|
||||
|
||||
DB::commit();
|
||||
|
||||
return $post;
|
||||
}
|
||||
|
||||
public function updatePost(array $data, int $id): ?Post
|
||||
{
|
||||
DB::beginTransaction();
|
||||
|
||||
$post = Post::find($id);
|
||||
|
||||
if ($post === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (isset($data['tags'])) {
|
||||
$tags = [];
|
||||
|
||||
foreach ($data['tags'] as $tag) {
|
||||
$tag = Tag::firstOrCreate(['name' => $tag]);
|
||||
$tags[] = $tag;
|
||||
}
|
||||
|
||||
$post->tags()->sync($tags);
|
||||
}
|
||||
|
||||
$post->update($data);
|
||||
|
||||
DB::commit();
|
||||
|
||||
return $this->findPost($post->id);
|
||||
}
|
||||
|
||||
public function deletePost(int $id): bool
|
||||
{
|
||||
$post = Post::find($id);
|
||||
|
||||
if ($post === null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$post->delete();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
33
app/Services/Post/PostServiceInterface.php
Normal file
33
app/Services/Post/PostServiceInterface.php
Normal file
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Services\Post;
|
||||
|
||||
use App\Models\Post;
|
||||
use App\Services\PaginableResource;
|
||||
use App\Services\QueryRequestModifiers\Post\PostFilterDTO;
|
||||
use App\Services\QueryRequestModifiers\Post\PostOrderDTO;
|
||||
|
||||
interface PostServiceInterface
|
||||
{
|
||||
/**
|
||||
* @return PaginableResource<Post>
|
||||
*/
|
||||
public function fetchPosts(int $page, ?PostFilterDTO $filters, ?PostOrderDTO $orderDef): PaginableResource;
|
||||
|
||||
public function findPost(int $id): ?Post;
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $data
|
||||
* @return Post
|
||||
*/
|
||||
public function storePost(array $data): Post;
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $data
|
||||
*/
|
||||
public function updatePost(array $data, int $id): ?Post;
|
||||
|
||||
public function deletePost(int $id): bool;
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Services\QueryRequestModifiers\Category;
|
||||
|
||||
use App\Http\Requests\Category\ListCategoryRequest;
|
||||
use App\Models\Category;
|
||||
use App\Services\QueryRequestModifiers\Filterable;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
|
||||
class CategoryFilter
|
||||
{
|
||||
/**
|
||||
* @use Filterable<Category, CategoryFilterDTO>
|
||||
*/
|
||||
use Filterable;
|
||||
|
||||
/**
|
||||
* @param Builder<Category> $query
|
||||
* @return Builder<Category>
|
||||
*/
|
||||
public function apply(Builder $query, ?CategoryFilterDTO $filters): Builder
|
||||
{
|
||||
return $this->applyFilterable($query, $filters);
|
||||
}
|
||||
|
||||
public function makeFromRequest(ListCategoryRequest $request): ?CategoryFilterDTO
|
||||
{
|
||||
return $this->makeFilterableFromRequest($request, CategoryFilterDTO::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, string>
|
||||
*/
|
||||
public function validateRules(): array
|
||||
{
|
||||
return [
|
||||
'name' => 'sometimes|string|max:255',
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return array<int, string>
|
||||
*/
|
||||
protected static function keys(): array
|
||||
{
|
||||
return ['name'];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Services\QueryRequestModifiers\Category;
|
||||
|
||||
use App\Services\CacheKeyInterface;
|
||||
|
||||
class CategoryFilterDTO implements CacheKeyInterface
|
||||
{
|
||||
/**
|
||||
* @param array<string, string> $filters
|
||||
*/
|
||||
public function __construct(public array $filters)
|
||||
{
|
||||
}
|
||||
|
||||
public function toCacheKey(): string
|
||||
{
|
||||
return http_build_query($this->filters, '', '|');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Services\QueryRequestModifiers\Category;
|
||||
|
||||
use App\Http\Requests\Category\ListCategoryRequest;
|
||||
use App\Models\Category;
|
||||
use App\Services\QueryRequestModifiers\Orderable;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
|
||||
class CategoryOrder
|
||||
{
|
||||
/**
|
||||
* @use Orderable<Category, CategoryOrderDTO>
|
||||
*/
|
||||
use Orderable;
|
||||
|
||||
|
||||
/**
|
||||
* @param Builder<Category> $query
|
||||
* @return Builder<Category>
|
||||
*/
|
||||
public function apply(Builder $query, ?CategoryOrderDTO $filters): Builder
|
||||
{
|
||||
return $this->applyOrderable($query, $filters);
|
||||
}
|
||||
|
||||
public function makeFromRequest(ListCategoryRequest $request): ?CategoryOrderDTO
|
||||
{
|
||||
return $this->makeOrderableFromRequest($request, CategoryOrderDTO::class);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Services\QueryRequestModifiers\Category;
|
||||
|
||||
use App\Services\CacheKeyInterface;
|
||||
use App\Services\QueryRequestModifiers\OrderableDTO;
|
||||
use App\Services\QueryRequestModifiers\SortDirection;
|
||||
|
||||
/**
|
||||
* @implements OrderableDTO<CategoryOrderDTO>
|
||||
*/
|
||||
class CategoryOrderDTO implements CacheKeyInterface, OrderableDTO
|
||||
{
|
||||
public function __construct(public string $column, public SortDirection $direction)
|
||||
{
|
||||
}
|
||||
|
||||
public function toCacheKey(): string
|
||||
{
|
||||
return $this->column . '|' . $this->direction->value;
|
||||
}
|
||||
|
||||
public function getColumn(): string
|
||||
{
|
||||
return $this->column;
|
||||
}
|
||||
|
||||
public function getDirection(): SortDirection
|
||||
{
|
||||
return $this->direction;
|
||||
}
|
||||
|
||||
public static function createFromValues(string $column, SortDirection $sortDirection): OrderableDTO
|
||||
{
|
||||
return new self(
|
||||
$column,
|
||||
$sortDirection
|
||||
);
|
||||
}
|
||||
}
|
||||
51
app/Services/QueryRequestModifiers/Comment/CommentFilter.php
Normal file
51
app/Services/QueryRequestModifiers/Comment/CommentFilter.php
Normal file
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Services\QueryRequestModifiers\Comment;
|
||||
|
||||
use App\Http\Requests\Comment\ListCommentRequest;
|
||||
use App\Models\Comment;
|
||||
use App\Services\QueryRequestModifiers\Filterable;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
|
||||
class CommentFilter
|
||||
{
|
||||
/**
|
||||
* @use Filterable<Comment, CommentFilterDTO>
|
||||
*/
|
||||
use Filterable;
|
||||
|
||||
/**
|
||||
* @param Builder<Comment> $query
|
||||
* @return Builder<Comment>
|
||||
*/
|
||||
public function apply(Builder $query, ?CommentFilterDTO $filters): Builder
|
||||
{
|
||||
return $this->applyFilterable($query, $filters);
|
||||
}
|
||||
|
||||
public function makeFromRequest(ListCommentRequest $request): ?CommentFilterDTO
|
||||
{
|
||||
return $this->makeFilterableFromRequest($request, CommentFilterDTO::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, string>
|
||||
*/
|
||||
public function validateRules(): array
|
||||
{
|
||||
return [
|
||||
'content' => 'sometimes|string',
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return array<int, string>
|
||||
*/
|
||||
protected static function keys(): array
|
||||
{
|
||||
return ['content'];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Services\QueryRequestModifiers\Comment;
|
||||
|
||||
use App\Services\CacheKeyInterface;
|
||||
|
||||
readonly class CommentFilterDTO implements CacheKeyInterface
|
||||
{
|
||||
/**
|
||||
* @param array<string, string> $filters
|
||||
*/
|
||||
public function __construct(public array $filters)
|
||||
{
|
||||
}
|
||||
|
||||
public function toCacheKey(): string
|
||||
{
|
||||
return http_build_query($this->filters, '', '|');
|
||||
}
|
||||
}
|
||||
33
app/Services/QueryRequestModifiers/Comment/CommentOrder.php
Normal file
33
app/Services/QueryRequestModifiers/Comment/CommentOrder.php
Normal file
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Services\QueryRequestModifiers\Comment;
|
||||
|
||||
use App\Http\Requests\Comment\ListCommentRequest;
|
||||
use App\Models\Comment;
|
||||
use App\Services\QueryRequestModifiers\Orderable;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
|
||||
class CommentOrder
|
||||
{
|
||||
/**
|
||||
* @use Orderable<Comment, CommentOrderDTO>
|
||||
*/
|
||||
use Orderable;
|
||||
|
||||
|
||||
/**
|
||||
* @param Builder<Comment> $query
|
||||
* @return Builder<Comment>
|
||||
*/
|
||||
public function apply(Builder $query, ?CommentOrderDTO $filters): Builder
|
||||
{
|
||||
return $this->applyOrderable($query, $filters);
|
||||
}
|
||||
|
||||
public function makeFromRequest(ListCommentRequest $request): ?CommentOrderDTO
|
||||
{
|
||||
return $this->makeOrderableFromRequest($request, CommentOrderDTO::class);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Services\QueryRequestModifiers\Comment;
|
||||
|
||||
use App\Services\QueryRequestModifiers\OrderableDTO;
|
||||
use App\Services\QueryRequestModifiers\SortDirection;
|
||||
|
||||
/**
|
||||
* @implements OrderableDTO<CommentOrderDTO>
|
||||
*/
|
||||
readonly class CommentOrderDTO implements OrderableDTO
|
||||
{
|
||||
public function __construct(public string $column, public SortDirection $direction)
|
||||
{
|
||||
}
|
||||
|
||||
public function toCacheKey(): string
|
||||
{
|
||||
return $this->column . '|' . $this->direction->value;
|
||||
}
|
||||
|
||||
public function getColumn(): string
|
||||
{
|
||||
return $this->column;
|
||||
}
|
||||
|
||||
public function getDirection(): SortDirection
|
||||
{
|
||||
return $this->direction;
|
||||
}
|
||||
|
||||
public static function createFromValues(string $column, SortDirection $sortDirection): OrderableDTO
|
||||
{
|
||||
return new self(
|
||||
$column,
|
||||
$sortDirection
|
||||
);
|
||||
}
|
||||
}
|
||||
52
app/Services/QueryRequestModifiers/Filterable.php
Normal file
52
app/Services/QueryRequestModifiers/Filterable.php
Normal file
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Services\QueryRequestModifiers;
|
||||
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
/**
|
||||
* @template C of Model
|
||||
* @template D
|
||||
*/
|
||||
trait Filterable
|
||||
{
|
||||
abstract public static function keys();
|
||||
|
||||
/**
|
||||
* @param Builder<C> $query
|
||||
* @return Builder<C>
|
||||
*/
|
||||
protected function applyFilterable(Builder $query, ?object $filters): Builder
|
||||
{
|
||||
if ($filters === null) {
|
||||
return $query;
|
||||
}
|
||||
|
||||
foreach (self::keys() as $filterName) {
|
||||
if (isset($filters->filters[$filterName])) {
|
||||
$query->where($filterName, '=', $filters->filters[$filterName]);
|
||||
}
|
||||
}
|
||||
return $query;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param class-string<D> $className
|
||||
* @return D|null
|
||||
*/
|
||||
protected function makeFilterableFromRequest(FormRequest $request, string $className): ?object
|
||||
{
|
||||
$keys = $request->all(self::keys());
|
||||
|
||||
// no filtering
|
||||
if (count($keys) === 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return new $className($keys);
|
||||
}
|
||||
}
|
||||
75
app/Services/QueryRequestModifiers/Orderable.php
Normal file
75
app/Services/QueryRequestModifiers/Orderable.php
Normal file
@@ -0,0 +1,75 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Services\QueryRequestModifiers;
|
||||
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
|
||||
/**
|
||||
* @template C of Model
|
||||
* @template D of OrderableDTO
|
||||
*/
|
||||
trait Orderable
|
||||
{
|
||||
/**
|
||||
* @param Builder<C> $query
|
||||
* @param ?OrderableDTO<D> $orderDef
|
||||
* @return Builder<C>
|
||||
*/
|
||||
protected function applyOrderable(Builder $query, ?OrderableDTO $orderDef): Builder
|
||||
{
|
||||
if ($orderDef !== null) {
|
||||
$query->orderBy($orderDef->getColumn(), $orderDef->getDirection()->value);
|
||||
}
|
||||
|
||||
return $query;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param class-string<D> $className
|
||||
* @return D|null
|
||||
*/
|
||||
protected function makeOrderableFromRequest(FormRequest $request, string $className): ?object
|
||||
{
|
||||
$keys = $request->all(self::keys());
|
||||
|
||||
if (!isset($keys['order']) || !isset($keys['direction'])) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$validator = Validator::make($keys, $this->validateRules());
|
||||
|
||||
if ($validator->fails()) {
|
||||
// this should never happen... Invalid request
|
||||
throw new \InvalidArgumentException($validator->errors()->first());
|
||||
}
|
||||
|
||||
$column = $keys['order'];
|
||||
$direction = $keys['direction'];
|
||||
|
||||
return call_user_func([$className, 'createFromValues'], $column, SortDirection::from($direction));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, string>
|
||||
*/
|
||||
public function validateRules(): array
|
||||
{
|
||||
return [
|
||||
'order' => 'sometimes|string|in:title',
|
||||
'direction' => 'sometimes|string|in:asc,desc',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<int, string>
|
||||
*/
|
||||
protected static function keys(): array
|
||||
{
|
||||
return ['order', 'direction'];
|
||||
}
|
||||
}
|
||||
19
app/Services/QueryRequestModifiers/OrderableDTO.php
Normal file
19
app/Services/QueryRequestModifiers/OrderableDTO.php
Normal file
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Services\QueryRequestModifiers;
|
||||
|
||||
/**
|
||||
* @template T of OrderableDTO
|
||||
*/
|
||||
interface OrderableDTO
|
||||
{
|
||||
public function getColumn(): string;
|
||||
public function getDirection(): SortDirection;
|
||||
|
||||
/**
|
||||
* @return T
|
||||
*/
|
||||
public static function createFromValues(string $column, SortDirection $sortDirection): self;
|
||||
}
|
||||
52
app/Services/QueryRequestModifiers/Post/PostFilter.php
Normal file
52
app/Services/QueryRequestModifiers/Post/PostFilter.php
Normal file
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Services\QueryRequestModifiers\Post;
|
||||
|
||||
use App\Http\Requests\Post\ListPostRequest;
|
||||
use App\Models\Post;
|
||||
use App\Services\QueryRequestModifiers\Filterable;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
|
||||
class PostFilter
|
||||
{
|
||||
/**
|
||||
* @use Filterable<Post, PostFilterDTO>
|
||||
*/
|
||||
use Filterable;
|
||||
|
||||
/**
|
||||
* @param Builder<Post> $query
|
||||
* @return Builder<Post>
|
||||
*/
|
||||
public function apply(Builder $query, ?PostFilterDTO $filters): Builder
|
||||
{
|
||||
return $this->applyFilterable($query, $filters);
|
||||
}
|
||||
|
||||
public function makeFromRequest(ListPostRequest $request): ?PostFilterDTO
|
||||
{
|
||||
return $this->makeFilterableFromRequest($request, PostFilterDTO::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, string>
|
||||
*/
|
||||
public function validateRules(): array
|
||||
{
|
||||
return [
|
||||
'title' => 'sometimes|string',
|
||||
'content' => 'sometimes|string',
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return array<int, string>
|
||||
*/
|
||||
protected static function keys(): array
|
||||
{
|
||||
return ['title', 'content'];
|
||||
}
|
||||
}
|
||||
22
app/Services/QueryRequestModifiers/Post/PostFilterDTO.php
Normal file
22
app/Services/QueryRequestModifiers/Post/PostFilterDTO.php
Normal file
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Services\QueryRequestModifiers\Post;
|
||||
|
||||
use App\Services\CacheKeyInterface;
|
||||
|
||||
readonly class PostFilterDTO implements CacheKeyInterface
|
||||
{
|
||||
/**
|
||||
* @param array<string, string> $filters
|
||||
*/
|
||||
public function __construct(public array $filters)
|
||||
{
|
||||
}
|
||||
|
||||
public function toCacheKey(): string
|
||||
{
|
||||
return http_build_query($this->filters, '', '|');
|
||||
}
|
||||
}
|
||||
33
app/Services/QueryRequestModifiers/Post/PostOrder.php
Normal file
33
app/Services/QueryRequestModifiers/Post/PostOrder.php
Normal file
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Services\QueryRequestModifiers\Post;
|
||||
|
||||
use App\Http\Requests\Post\ListPostRequest;
|
||||
use App\Models\Post;
|
||||
use App\Services\QueryRequestModifiers\Orderable;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
|
||||
class PostOrder
|
||||
{
|
||||
/**
|
||||
* @use Orderable<Post, PostOrderDTO>
|
||||
*/
|
||||
use Orderable;
|
||||
|
||||
|
||||
/**
|
||||
* @param Builder<Post> $query
|
||||
* @return Builder<Post>
|
||||
*/
|
||||
public function apply(Builder $query, ?PostOrderDTO $filters): Builder
|
||||
{
|
||||
return $this->applyOrderable($query, $filters);
|
||||
}
|
||||
|
||||
public function makeFromRequest(ListPostRequest $request): ?PostOrderDTO
|
||||
{
|
||||
return $this->makeOrderableFromRequest($request, PostOrderDTO::class);
|
||||
}
|
||||
}
|
||||
42
app/Services/QueryRequestModifiers/Post/PostOrderDTO.php
Normal file
42
app/Services/QueryRequestModifiers/Post/PostOrderDTO.php
Normal file
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Services\QueryRequestModifiers\Post;
|
||||
|
||||
use App\Services\CacheKeyInterface;
|
||||
use App\Services\QueryRequestModifiers\SortDirection;
|
||||
use App\Services\QueryRequestModifiers\OrderableDTO;
|
||||
|
||||
/**
|
||||
* @implements OrderableDTO<PostOrderDTO>
|
||||
*/
|
||||
readonly class PostOrderDTO implements CacheKeyInterface, OrderableDTO
|
||||
{
|
||||
public function __construct(public string $column, public SortDirection $direction)
|
||||
{
|
||||
}
|
||||
|
||||
public function toCacheKey(): string
|
||||
{
|
||||
return $this->column . '|' . $this->direction->value;
|
||||
}
|
||||
|
||||
public function getColumn(): string
|
||||
{
|
||||
return $this->column;
|
||||
}
|
||||
|
||||
public function getDirection(): SortDirection
|
||||
{
|
||||
return $this->direction;
|
||||
}
|
||||
|
||||
public static function createFromValues(string $column, SortDirection $sortDirection): OrderableDTO
|
||||
{
|
||||
return new self(
|
||||
$column,
|
||||
$sortDirection
|
||||
);
|
||||
}
|
||||
}
|
||||
11
app/Services/QueryRequestModifiers/SortDirection.php
Normal file
11
app/Services/QueryRequestModifiers/SortDirection.php
Normal file
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Services\QueryRequestModifiers;
|
||||
|
||||
enum SortDirection: string
|
||||
{
|
||||
case ASC = "asc";
|
||||
case DESC = "desc";
|
||||
}
|
||||
Reference in New Issue
Block a user