|
| 1 | +"use server"; |
| 2 | + |
| 3 | +import { sql } from "@vercel/postgres"; |
| 4 | +import { unstable_noStore as noStore, revalidatePath } from "next/cache"; |
| 5 | +import { auth } from "@/auth"; |
| 6 | +import camelcaseKeys from "camelcase-keys"; |
| 7 | +import { ensureCommentsTable } from "@/utils/ensureCommentsTable"; |
| 8 | + |
| 9 | +/** |
| 10 | + * 댓글을 생성합니다. |
| 11 | + * @param postIndex 게시글의 index |
| 12 | + * @param content 댓글 내용 |
| 13 | + */ |
| 14 | +export async function postComment({ |
| 15 | + postIndex, |
| 16 | + content, |
| 17 | +}: { |
| 18 | + postIndex: number; |
| 19 | + content: string; |
| 20 | +}): Promise<IComment> { |
| 21 | + noStore(); |
| 22 | + |
| 23 | + // 인증 체크 |
| 24 | + const session = await auth(); |
| 25 | + console.log("session", session); |
| 26 | + if (!session?.user) { |
| 27 | + throw new Error("Unauthorized: 로그인이 필요합니다."); |
| 28 | + } |
| 29 | + |
| 30 | + // 이메일이 없으면 에러 |
| 31 | + const userEmail = session.user.email; |
| 32 | + if (!userEmail) { |
| 33 | + throw new Error("Unauthorized: 사용자 이메일 정보를 찾을 수 없습니다."); |
| 34 | + } |
| 35 | + |
| 36 | + // 입력 검증 |
| 37 | + if (!content || !content.trim()) { |
| 38 | + throw new Error("댓글 내용을 입력해주세요."); |
| 39 | + } |
| 40 | + |
| 41 | + try { |
| 42 | + // 테이블이 없으면 생성 |
| 43 | + await ensureCommentsTable(); |
| 44 | + |
| 45 | + // 게시글 ID 가져오기 |
| 46 | + const { rows: postRows } = await sql` |
| 47 | + SELECT id FROM posts WHERE index = ${postIndex} AND status = 'published' |
| 48 | + `; |
| 49 | + |
| 50 | + if (!postRows[0]) { |
| 51 | + throw new Error("게시글을 찾을 수 없습니다."); |
| 52 | + } |
| 53 | + |
| 54 | + const postId = postRows[0].id; |
| 55 | + |
| 56 | + // 사용자 ID 가져오기 |
| 57 | + const { rows: userRows } = await sql` |
| 58 | + SELECT id FROM users WHERE email = ${userEmail} |
| 59 | + `; |
| 60 | + |
| 61 | + if (!userRows[0]) { |
| 62 | + throw new Error("사용자 정보를 찾을 수 없습니다."); |
| 63 | + } |
| 64 | + |
| 65 | + const userId = userRows[0].id; |
| 66 | + |
| 67 | + // 댓글 생성 |
| 68 | + const { rows } = await sql` |
| 69 | + INSERT INTO comments (post_id, user_id, content) |
| 70 | + VALUES (${postId}, ${userId}, ${content.trim()}) |
| 71 | + RETURNING |
| 72 | + id, |
| 73 | + post_id as "postId", |
| 74 | + user_id as "userId", |
| 75 | + content, |
| 76 | + created_at as "createdAt", |
| 77 | + updated_at as "updatedAt", |
| 78 | + deleted_at as "deletedAt" |
| 79 | + `; |
| 80 | + |
| 81 | + // 사용자 정보 조인하여 반환 |
| 82 | + const { rows: commentWithUser } = await sql` |
| 83 | + SELECT |
| 84 | + c.id, |
| 85 | + c.post_id as "postId", |
| 86 | + p.index as "postIndex", |
| 87 | + c.user_id as "userId", |
| 88 | + u.email as "userEmail", |
| 89 | + u.name as "userName", |
| 90 | + u.image as "userImage", |
| 91 | + c.content, |
| 92 | + c.created_at as "createdAt", |
| 93 | + c.updated_at as "updatedAt", |
| 94 | + c.deleted_at as "deletedAt" |
| 95 | + FROM comments c |
| 96 | + INNER JOIN posts p ON c.post_id = p.id |
| 97 | + LEFT JOIN users u ON c.user_id = u.id |
| 98 | + WHERE c.id = ${rows[0].id} |
| 99 | + `; |
| 100 | + |
| 101 | + revalidatePath(`/posts/${postIndex}`); |
| 102 | + |
| 103 | + return camelcaseKeys(commentWithUser[0], { deep: true }) as IComment; |
| 104 | + } catch (error) { |
| 105 | + console.error("Database Error:", error); |
| 106 | + throw new Error( |
| 107 | + error instanceof Error |
| 108 | + ? error.message |
| 109 | + : "댓글 작성 중 오류가 발생했습니다." |
| 110 | + ); |
| 111 | + } |
| 112 | +} |
0 commit comments