Deploy Backend / deploy (push) Successful in 2m41s
- blog_comments table + Comment model/factory; comments attach to the post,
public list shows approved rows only, public submission starts unapproved
- Admin moderation API: list/filter, approve, delete comments
- Public POST posts/{slug}/view and posts/{slug}/like toggle counters
- Route groups: comments throttled like the contact form, interactions at
60/min; comments resource exposed to the generic admin list
60 lines
1.8 KiB
PHP
60 lines
1.8 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api\Blog;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\Blog\Comment;
|
|
use App\Models\Blog\Post;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
|
|
/**
|
|
* @OA\Tag(name="Post comments", description="Reader comments on blog posts")
|
|
*/
|
|
class CommentController extends Controller
|
|
{
|
|
/**
|
|
* Public list of approved comments for a post, newest first.
|
|
*/
|
|
public function index(string $slug): JsonResponse
|
|
{
|
|
$post = Post::where('slug', $slug)->firstOrFail();
|
|
|
|
$comments = $post->comments()
|
|
->approved()
|
|
->orderByDesc('created_at')
|
|
->get(['id', 'name', 'body', 'created_at']);
|
|
|
|
return response()->json([
|
|
'post_id' => $post->id,
|
|
'comments' => $comments,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Public comment submission. Every comment starts unapproved and appears
|
|
* only after an admin approves it, mirroring the contact form's moderation.
|
|
*/
|
|
public function store(Request $request, string $slug): JsonResponse
|
|
{
|
|
$post = Post::where('slug', $slug)->firstOrFail();
|
|
|
|
$validated = $request->validate([
|
|
'name' => ['required', 'string', 'max:120'],
|
|
'email' => ['nullable', 'email', 'max:255'],
|
|
'body' => ['required', 'string', 'min:3', 'max:2000'],
|
|
]);
|
|
|
|
$comment = $post->comments()->create([
|
|
'name' => $validated['name'],
|
|
'email' => $validated['email'] ?? null,
|
|
'body' => $validated['body'],
|
|
'is_approved' => false,
|
|
]);
|
|
|
|
return response()->json([
|
|
'message' => 'دیدگاه شما ثبت شد و پس از تأیید نمایش داده میشود.',
|
|
'comment' => $comment,
|
|
], 201);
|
|
}
|
|
} |