태그를 관리용/일반용으로 분리하고 관리자 드래그 정렬을 추가.
댓글/회원/관리자 인증·프로필 흐름 보완과 관련 마이그레이션 및 문서를 함께 반영해 운영 동선을 안정화. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -10,7 +10,9 @@ import { getPostgresClient } from './postgres-client'
|
||||
* @property {string} status - 댓글 상태
|
||||
* @property {string} createdAt - 생성 시각
|
||||
* @property {string} updatedAt - 수정 시각
|
||||
* @property {{ id: string, username: string }} user - 작성자 정보
|
||||
* @property {number} likeCount - 좋아요 수
|
||||
* @property {boolean} likedByMe - 현재 회원 좋아요 여부
|
||||
* @property {{ id: string, username: string, avatarUrl: string }} user - 작성자 정보
|
||||
*/
|
||||
|
||||
/**
|
||||
@@ -19,6 +21,13 @@ import { getPostgresClient } from './postgres-client'
|
||||
*/
|
||||
const getSql = () => getPostgresClient()
|
||||
|
||||
/**
|
||||
* Postgres undefined table 에러 여부 확인
|
||||
* @param {unknown} error - 에러 객체
|
||||
* @returns {boolean} undefined table 여부
|
||||
*/
|
||||
const isUndefinedTableError = (error) => String(error?.code || '') === '42P01'
|
||||
|
||||
/**
|
||||
* 게시물 ID 조회
|
||||
* @param {ReturnType<typeof import('postgres').default>} sql - postgres 클라이언트
|
||||
@@ -46,7 +55,7 @@ const findPublishedPostIdBySlug = async (sql, slug) => {
|
||||
* @param {string} slug - 게시물 슬러그
|
||||
* @returns {Promise<Array<PostComment>>} 댓글 목록
|
||||
*/
|
||||
export const listPostCommentsBySlug = async (slug) => {
|
||||
export const listPostCommentsBySlug = async (slug, viewerUserId = null) => {
|
||||
const sql = getSql()
|
||||
|
||||
if (!sql) {
|
||||
@@ -61,23 +70,64 @@ export const listPostCommentsBySlug = async (slug) => {
|
||||
})
|
||||
}
|
||||
|
||||
const rows = await sql`
|
||||
SELECT
|
||||
comments.id,
|
||||
comments.post_id AS "postId",
|
||||
comments.parent_id AS "parentId",
|
||||
comments.body,
|
||||
comments.status,
|
||||
comments.created_at AS "createdAt",
|
||||
comments.updated_at AS "updatedAt",
|
||||
users.id AS "userId",
|
||||
users.username AS "username"
|
||||
FROM comments
|
||||
INNER JOIN users ON users.id = comments.user_id
|
||||
WHERE comments.post_id = ${postId}
|
||||
AND comments.status = 'published'
|
||||
ORDER BY comments.created_at ASC
|
||||
`
|
||||
let rows = []
|
||||
|
||||
try {
|
||||
rows = await sql`
|
||||
SELECT
|
||||
comments.id,
|
||||
comments.post_id AS "postId",
|
||||
comments.parent_id AS "parentId",
|
||||
comments.body,
|
||||
comments.status,
|
||||
comments.created_at AS "createdAt",
|
||||
comments.updated_at AS "updatedAt",
|
||||
users.id AS "userId",
|
||||
users.username AS "username",
|
||||
users.avatar_url AS "avatarUrl",
|
||||
COALESCE(comment_like_counts.like_count, 0) AS "likeCount",
|
||||
CASE
|
||||
WHEN viewer_comment_likes.user_id IS NULL THEN false
|
||||
ELSE true
|
||||
END AS "likedByMe"
|
||||
FROM comments
|
||||
INNER JOIN users ON users.id = comments.user_id
|
||||
LEFT JOIN (
|
||||
SELECT comment_id, COUNT(*)::INT AS like_count
|
||||
FROM comment_likes
|
||||
GROUP BY comment_id
|
||||
) AS comment_like_counts ON comment_like_counts.comment_id = comments.id
|
||||
LEFT JOIN comment_likes AS viewer_comment_likes
|
||||
ON viewer_comment_likes.comment_id = comments.id
|
||||
AND viewer_comment_likes.user_id = ${viewerUserId}
|
||||
WHERE comments.post_id = ${postId}
|
||||
AND comments.status = 'published'
|
||||
ORDER BY comments.created_at ASC
|
||||
`
|
||||
} catch (error) {
|
||||
if (!isUndefinedTableError(error)) {
|
||||
throw error
|
||||
}
|
||||
|
||||
rows = await sql`
|
||||
SELECT
|
||||
comments.id,
|
||||
comments.post_id AS "postId",
|
||||
comments.parent_id AS "parentId",
|
||||
comments.body,
|
||||
comments.status,
|
||||
comments.created_at AS "createdAt",
|
||||
comments.updated_at AS "updatedAt",
|
||||
users.id AS "userId",
|
||||
users.username AS "username",
|
||||
users.avatar_url AS "avatarUrl"
|
||||
FROM comments
|
||||
INNER JOIN users ON users.id = comments.user_id
|
||||
WHERE comments.post_id = ${postId}
|
||||
AND comments.status = 'published'
|
||||
ORDER BY comments.created_at ASC
|
||||
`
|
||||
}
|
||||
|
||||
return rows.map((row) => ({
|
||||
id: row.id,
|
||||
@@ -87,9 +137,12 @@ export const listPostCommentsBySlug = async (slug) => {
|
||||
status: row.status,
|
||||
createdAt: row.createdAt.toISOString(),
|
||||
updatedAt: row.updatedAt.toISOString(),
|
||||
likeCount: Number(row.likeCount || 0),
|
||||
likedByMe: Boolean(row.likedByMe),
|
||||
user: {
|
||||
id: row.userId,
|
||||
username: row.username
|
||||
username: row.username,
|
||||
avatarUrl: row.avatarUrl || ''
|
||||
}
|
||||
}))
|
||||
}
|
||||
@@ -165,7 +218,7 @@ export const createComment = async (input) => {
|
||||
}
|
||||
|
||||
const userRows = await sql`
|
||||
SELECT id, username
|
||||
SELECT id, username, avatar_url
|
||||
FROM users
|
||||
WHERE id = ${input.userId}
|
||||
LIMIT 1
|
||||
@@ -189,8 +242,84 @@ export const createComment = async (input) => {
|
||||
updatedAt: created.updatedAt.toISOString(),
|
||||
user: {
|
||||
id: user.id,
|
||||
username: user.username
|
||||
username: user.username,
|
||||
avatarUrl: user.avatar_url || ''
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 댓글 좋아요를 토글한다.
|
||||
* @param {{ slug: string, commentId: string, userId: string }} input - 좋아요 입력값
|
||||
* @returns {Promise<{ liked: boolean, likeCount: number }>} 좋아요 결과
|
||||
*/
|
||||
export const toggleCommentLike = async (input) => {
|
||||
const sql = getSql()
|
||||
|
||||
if (!sql) {
|
||||
throw createError({
|
||||
statusCode: 500,
|
||||
message: '데이터베이스 설정이 필요합니다.'
|
||||
})
|
||||
}
|
||||
|
||||
const postId = await findPublishedPostIdBySlug(sql, input.slug)
|
||||
if (!postId) {
|
||||
throw createError({
|
||||
statusCode: 404,
|
||||
statusMessage: '게시물을 찾을 수 없습니다'
|
||||
})
|
||||
}
|
||||
|
||||
const commentRows = await sql`
|
||||
SELECT id
|
||||
FROM comments
|
||||
WHERE id = ${input.commentId}
|
||||
AND post_id = ${postId}
|
||||
AND status = 'published'
|
||||
LIMIT 1
|
||||
`
|
||||
const comment = commentRows?.[0]
|
||||
|
||||
if (!comment) {
|
||||
throw createError({
|
||||
statusCode: 404,
|
||||
message: '댓글을 찾을 수 없습니다.'
|
||||
})
|
||||
}
|
||||
|
||||
const likedRows = await sql`
|
||||
SELECT 1
|
||||
FROM comment_likes
|
||||
WHERE comment_id = ${input.commentId}
|
||||
AND user_id = ${input.userId}
|
||||
LIMIT 1
|
||||
`
|
||||
const alreadyLiked = Boolean(likedRows?.[0])
|
||||
|
||||
if (alreadyLiked) {
|
||||
await sql`
|
||||
DELETE FROM comment_likes
|
||||
WHERE comment_id = ${input.commentId}
|
||||
AND user_id = ${input.userId}
|
||||
`
|
||||
} else {
|
||||
await sql`
|
||||
INSERT INTO comment_likes (comment_id, user_id)
|
||||
VALUES (${input.commentId}, ${input.userId})
|
||||
ON CONFLICT (comment_id, user_id) DO NOTHING
|
||||
`
|
||||
}
|
||||
|
||||
const countRows = await sql`
|
||||
SELECT COUNT(*)::INT AS like_count
|
||||
FROM comment_likes
|
||||
WHERE comment_id = ${input.commentId}
|
||||
`
|
||||
|
||||
return {
|
||||
liked: !alreadyLiked,
|
||||
likeCount: Number(countRows?.[0]?.like_count || 0)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user