38 lines
934 B
TypeScript

import {
getCommentLike,
createCommentLike,
deleteCommentLike,
getCommentLikeCount,
syncCommentLikeCount,
} from "../../db/community";
/**
* POST /api/community/comment-like
* Body: { commentId }
* Toggled Like auf einem Kommentar.
*/
export default defineEventHandler(async (event) => {
const user = await requireUser(event);
const { commentId } = (await readBody(event)) as { commentId: string };
if (!commentId) {
throw createError({ statusCode: 400, message: "commentId erforderlich" });
}
const existing = await getCommentLike(user.id, commentId);
let userLike: boolean;
if (existing) {
await deleteCommentLike(user.id, commentId);
userLike = false;
} else {
await createCommentLike(user.id, commentId);
userLike = true;
}
const count = await getCommentLikeCount(commentId);
await syncCommentLikeCount(commentId, count);
return { likesCount: count, userLike };
});