Add post comments with moderation plus view/like interaction endpoints
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
This commit is contained in:
Parsa Aghayi committed 2026-10-02 11:06:06 -04:00
1 parent fbacb0c9a1
commit e98807da05
11 files changed
+497

No files matched your search

@@ -4,6 +4,7 @@ namespace App\Http\Controllers\Api\Admin;
use App\Http\Controllers\Controller;
use App\Models\Blog\Category;
use App\Models\Blog\Comment;
use App\Models\Blog\Post;
use App\Models\Blog\Tag;
use App\Models\Website\AboutMe;
@@ -26,6 +27,7 @@ class AdminResourceController extends Controller
'projects' => Project::class,
'project-categories' => ProjectCategory::class,
'about-me' => AboutMe::class,
'comments' => Comment::class,
];
/**
@@ -39,6 +41,7 @@ class AdminResourceController extends Controller
'projects' => ['category'],
'skills' => ['aboutMe'],
'testimonials' => ['translations'],
'comments' => ['post'],
];
/**
@@ -0,0 +1,55 @@
<?php
namespace App\Http\Controllers\Api\Admin;
use App\Http\Controllers\Controller;
use App\Models\Blog\Comment;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
/**
* Moderation endpoints for reader comments. Public comments are never shown
* until an admin approves them here; pending rows are the inbox.
*/
class CommentAdminController extends Controller
{
/**
* List comments, newest first. `?approved=1|0` filters by moderation state.
*/
public function index(Request $request): JsonResponse
{
$query = Comment::query()->with('post:id,slug,title')->orderByDesc('id');
if ($request->has('approved')) {
$query->where('is_approved', $request->boolean('approved'));
}
return response()->json(['data' => $query->get()]);
}
/**
* Approve or reject a comment.
*/
public function update(Request $request, int $id): JsonResponse
{
$validated = $request->validate([
'is_approved' => ['required', 'boolean'],
]);
$comment = Comment::findOrFail($id);
$comment->is_approved = $validated['is_approved'];
$comment->save();
return response()->json(['data' => $comment]);
}
/**
* Delete a comment.
*/
public function destroy(int $id): JsonResponse
{
Comment::findOrFail($id)->delete();
return response()->json(null, 200);
}
}
@@ -0,0 +1,60 @@
<?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);
}
}
@@ -0,0 +1,56 @@
<?php
namespace App\Http\Controllers\Api\Blog;
use App\Http\Controllers\Controller;
use App\Models\Blog\Post;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
/**
* @OA\Tag(name="Post interactions", description="Anonymous view and like counters")
*/
class PostInteractionController extends Controller
{
/**
* Count a view. The caller records one hit per session on its side, so this
* simply bumps the running total and returns the fresh numbers.
*/
public function view(string $slug): JsonResponse
{
$post = Post::where('slug', $slug)->firstOrFail();
$post->increment('views');
$post->refresh();
return response()->json([
'views' => $post->views,
'likes' => $post->likes,
]);
}
/**
* Toggle a like. The frontend sends whether the visitor now likes the post;
* the counter grows on like and shrinks (never below zero) on unlike.
*/
public function like(Request $request, string $slug): JsonResponse
{
$validated = $request->validate([
'liked' => ['required', 'boolean'],
]);
$post = Post::where('slug', $slug)->firstOrFail();
if ($validated['liked']) {
$post->increment('likes');
} elseif ($post->likes > 0) {
$post->decrement('likes');
}
$post->refresh();
return response()->json([
'views' => $post->views,
'likes' => $post->likes,
]);
}
}