56 lines
1.9 KiB
PHP
56 lines
1.9 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api\Posts;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\Post;
|
|
use App\Models\PostComment;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
/**
|
|
* POST /api/posts/{post_id}/comments/{comment_id}/highlight
|
|
* DELETE /api/posts/{post_id}/comments/{comment_id}/highlight
|
|
*
|
|
* Only the post owner may highlight/un-highlight.
|
|
* Only 1 highlighted comment per post is allowed at a time.
|
|
*/
|
|
class PostCommentHighlightController extends Controller
|
|
{
|
|
public function highlight(Request $request, int $postId, int $commentId): JsonResponse
|
|
{
|
|
$post = Post::findOrFail($postId);
|
|
$comment = PostComment::where('post_id', $postId)->findOrFail($commentId);
|
|
|
|
if ($request->user()->id !== $post->user_id) {
|
|
abort(403, 'Only the post owner can highlight comments.');
|
|
}
|
|
|
|
DB::transaction(function () use ($post, $comment) {
|
|
// Remove any existing highlight on this post
|
|
PostComment::where('post_id', $post->id)
|
|
->where('is_highlighted', true)
|
|
->update(['is_highlighted' => false]);
|
|
|
|
$comment->update(['is_highlighted' => true]);
|
|
});
|
|
|
|
return response()->json(['message' => 'Comment highlighted.', 'comment_id' => $comment->id]);
|
|
}
|
|
|
|
public function unhighlight(Request $request, int $postId, int $commentId): JsonResponse
|
|
{
|
|
$post = Post::findOrFail($postId);
|
|
$comment = PostComment::where('post_id', $postId)->findOrFail($commentId);
|
|
|
|
if ($request->user()->id !== $post->user_id) {
|
|
abort(403, 'Only the post owner can remove comment highlights.');
|
|
}
|
|
|
|
$comment->update(['is_highlighted' => false]);
|
|
|
|
return response()->json(['message' => 'Highlight removed.', 'comment_id' => $comment->id]);
|
|
}
|
|
}
|