Add news article comments and reactions

This commit is contained in:
2026-05-01 11:43:49 +02:00
parent 874f8feb9c
commit 28e7e46e13
22 changed files with 20083 additions and 26 deletions

View File

@@ -7,6 +7,8 @@ use App\Http\Controllers\Controller;
use App\Models\ArtworkComment;
use App\Models\ArtworkReaction;
use App\Models\CommentReaction;
use App\Models\NewsArticleComment;
use App\Models\NewsArticleCommentReaction;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
@@ -77,6 +79,33 @@ class ReactionController extends Controller
);
}
public function newsCommentReactions(Request $request, int $commentId): JsonResponse
{
$this->resolveVisibleNewsComment($commentId);
return response()->json([
'entity_type' => 'news-comment',
'entity_id' => $commentId,
'totals' => $this->getTotals('news_article_comment_reactions', ['comment_id' => $commentId], $request->user()?->id),
]);
}
public function toggleNewsCommentReaction(Request $request, int $commentId): JsonResponse
{
$this->resolveVisibleNewsComment($commentId);
$slug = $this->validateReactionSlug($request);
return $this->toggle(
model: new NewsArticleCommentReaction(),
where: ['comment_id' => $commentId, 'user_id' => $request->user()->id, 'reaction' => $slug],
countWhere: ['comment_id' => $commentId],
entityId: $commentId,
entityType: 'news-comment',
userId: $request->user()->id,
slug: $slug,
);
}
// ─────────────────────────────────────────────────────────────────────────
// Shared internals
// ─────────────────────────────────────────────────────────────────────────
@@ -189,4 +218,13 @@ class ReactionController extends Controller
throw new ModelNotFoundException("No [{$table}] record found with id [{$id}].");
}
}
private function resolveVisibleNewsComment(int $commentId): NewsArticleComment
{
return NewsArticleComment::query()
->where('id', $commentId)
->where('status', 'visible')
->whereHas('article', fn ($query) => $query->published())
->firstOrFail();
}
}