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),
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user