로그인 상태를 헤더에서 즉시 인지하고 계정 관리를 이어갈 수 있도록 사용자 설정과 관리자 멤버 관측 기능을 연결했다. Co-authored-by: Cursor <cursoragent@cursor.com>
45 lines
1.2 KiB
JavaScript
45 lines
1.2 KiB
JavaScript
import { createError, getRequestIP, readBody } from 'h3'
|
|
import { z } from 'zod'
|
|
import { createComment } from '../../../repositories/comment-repository'
|
|
import { touchUserActivity } from '../../../repositories/member-repository'
|
|
import { requireMemberSession } from '../../../utils/member-auth'
|
|
|
|
const createCommentSchema = z.object({
|
|
body: z.string().trim().min(1).max(5000),
|
|
parentId: z.string().uuid().optional().nullable()
|
|
})
|
|
|
|
/**
|
|
* 게시물 댓글 생성 API
|
|
* @param {import('h3').H3Event} event - 요청 이벤트
|
|
* @returns {Promise<{ comment: Object }>} 생성 댓글
|
|
*/
|
|
export default defineEventHandler(async (event) => {
|
|
const session = requireMemberSession(event)
|
|
const slug = String(getRouterParam(event, 'slug') || '')
|
|
const parsedBody = createCommentSchema.safeParse(await readBody(event))
|
|
|
|
if (!parsedBody.success) {
|
|
throw createError({
|
|
statusCode: 400,
|
|
message: '댓글 요청 형식이 올바르지 않습니다.'
|
|
})
|
|
}
|
|
|
|
const comment = await createComment({
|
|
slug,
|
|
userId: session.userId,
|
|
body: parsedBody.data.body,
|
|
parentId: parsedBody.data.parentId || null
|
|
})
|
|
await touchUserActivity({
|
|
userId: session.userId,
|
|
ip: String(getRequestIP(event) || '')
|
|
})
|
|
|
|
return {
|
|
comment
|
|
}
|
|
})
|
|
|