- PostService.getMyPosts() 메서드 추가 - PostController.myPosts() 액션 추가 - GET /v1/posts/my 라우트 추가 - PostApi.php Swagger 문서 추가
166 lines
4.6 KiB
PHP
166 lines
4.6 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api\V1;
|
|
|
|
use App\Helpers\ApiResponse;
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\Boards\CommentStoreRequest;
|
|
use App\Http\Requests\Boards\PostStoreRequest;
|
|
use App\Http\Requests\Boards\PostUpdateRequest;
|
|
use App\Services\Boards\PostService;
|
|
|
|
/**
|
|
* 게시글 API 컨트롤러
|
|
*
|
|
* URL: /v1/boards/{code}/posts
|
|
* - {code}: 게시판 코드 (board_code)
|
|
*/
|
|
class PostController extends Controller
|
|
{
|
|
public function __construct(
|
|
protected PostService $postService
|
|
) {}
|
|
|
|
/**
|
|
* 나의 게시글 목록 조회
|
|
*/
|
|
public function myPosts()
|
|
{
|
|
return ApiResponse::handle(function () {
|
|
$filters = request()->only(['board_code', 'search', 'status']);
|
|
$perPage = (int) request()->get('per_page', 15);
|
|
|
|
return $this->postService->getMyPosts($filters, $perPage);
|
|
}, __('message.fetched'));
|
|
}
|
|
|
|
/**
|
|
* 게시글 목록 조회
|
|
*/
|
|
public function index(string $code)
|
|
{
|
|
return ApiResponse::handle(function () use ($code) {
|
|
$filters = request()->only(['search', 'is_notice', 'status']);
|
|
$perPage = (int) request()->get('per_page', 15);
|
|
|
|
return $this->postService->getPostsByBoardCode($code, $filters, $perPage);
|
|
}, __('message.fetched'));
|
|
}
|
|
|
|
/**
|
|
* 게시글 상세 조회
|
|
*/
|
|
public function show(string $code, int $id)
|
|
{
|
|
return ApiResponse::handle(function () use ($code, $id) {
|
|
$post = $this->postService->getPostByCodeAndId($code, $id);
|
|
|
|
if (! $post) {
|
|
abort(404, __('error.post.not_found'));
|
|
}
|
|
|
|
// 조회수 증가
|
|
$post->increment('views');
|
|
|
|
// 커스텀 필드 값 추가
|
|
$post->custom_field_values = $this->postService->getCustomFieldValues($id);
|
|
|
|
return $post;
|
|
}, __('message.fetched'));
|
|
}
|
|
|
|
/**
|
|
* 게시글 작성
|
|
*/
|
|
public function store(PostStoreRequest $request, string $code)
|
|
{
|
|
return ApiResponse::handle(function () use ($request, $code) {
|
|
return $this->postService->createPostByBoardCode($code, $request->validated());
|
|
}, __('message.created'));
|
|
}
|
|
|
|
/**
|
|
* 게시글 수정
|
|
*/
|
|
public function update(PostUpdateRequest $request, string $code, int $id)
|
|
{
|
|
return ApiResponse::handle(function () use ($request, $code, $id) {
|
|
$post = $this->postService->updatePostByBoardCode($code, $id, $request->validated());
|
|
|
|
if (! $post) {
|
|
abort(404, __('error.post.not_found'));
|
|
}
|
|
|
|
return $post;
|
|
}, __('message.updated'));
|
|
}
|
|
|
|
/**
|
|
* 게시글 삭제
|
|
*/
|
|
public function destroy(string $code, int $id)
|
|
{
|
|
return ApiResponse::handle(function () use ($code, $id) {
|
|
$deleted = $this->postService->deletePostByBoardCode($code, $id);
|
|
|
|
if (! $deleted) {
|
|
abort(404, __('error.post.not_found'));
|
|
}
|
|
|
|
return ['deleted' => true];
|
|
}, __('message.deleted'));
|
|
}
|
|
|
|
/**
|
|
* 댓글 목록 조회
|
|
*/
|
|
public function comments(string $code, int $postId)
|
|
{
|
|
return ApiResponse::handle(function () use ($postId) {
|
|
return $this->postService->getComments($postId);
|
|
}, __('message.fetched'));
|
|
}
|
|
|
|
/**
|
|
* 댓글 작성
|
|
*/
|
|
public function storeComment(CommentStoreRequest $request, string $code, int $postId)
|
|
{
|
|
return ApiResponse::handle(function () use ($request, $postId) {
|
|
return $this->postService->createComment($postId, $request->validated());
|
|
}, __('message.created'));
|
|
}
|
|
|
|
/**
|
|
* 댓글 수정
|
|
*/
|
|
public function updateComment(CommentStoreRequest $request, string $code, int $postId, int $commentId)
|
|
{
|
|
return ApiResponse::handle(function () use ($request, $commentId) {
|
|
$comment = $this->postService->updateComment($commentId, $request->validated());
|
|
|
|
if (! $comment) {
|
|
abort(404, __('error.comment.not_found'));
|
|
}
|
|
|
|
return $comment;
|
|
}, __('message.updated'));
|
|
}
|
|
|
|
/**
|
|
* 댓글 삭제
|
|
*/
|
|
public function destroyComment(string $code, int $postId, int $commentId)
|
|
{
|
|
return ApiResponse::handle(function () use ($commentId) {
|
|
$deleted = $this->postService->deleteComment($commentId);
|
|
|
|
if (! $deleted) {
|
|
abort(404, __('error.comment.not_found'));
|
|
}
|
|
|
|
return ['deleted' => true];
|
|
}, __('message.deleted'));
|
|
}
|
|
}
|