|
| 1 | +"use server"; |
| 2 | + |
1 | 3 | import z from "zod"; |
2 | 4 | import { CommentActionInput } from "./inputs/comment.input"; |
| 5 | +import { authID } from "./session.actions"; |
| 6 | +import { ActionException } from "./RepositoryException"; |
| 7 | +import { persistenceRepository } from "../persistence/persistence-repositories"; |
| 8 | +import { eq } from "sqlkit"; |
| 9 | +import { CommentPresentation } from "../models/domain-models"; |
| 10 | + |
| 11 | +const sql = String.raw; |
3 | 12 |
|
4 | 13 | export const getComments = async ( |
5 | | - resourceId: string, |
6 | | - resourceType: "ARTICLE" | "COMMENT" |
7 | | -) => { |
8 | | - // Fetch comments from the database based on resourceId and resourceType |
9 | | - // const comments = await db |
10 | | - // .select() |
11 | | - // .from(commentsTable) |
12 | | - // .where(commentsTable.resource_id.eq(resourceId)) |
13 | | - // .and(commentsTable.resource_type.eq(resourceType)) |
14 | | - // .orderBy(commentsTable.created_at.desc()); |
15 | | - // return comments; |
16 | | - return []; // Placeholder for actual database query |
| 14 | + _input: z.infer<typeof CommentActionInput.getComments> |
| 15 | +): Promise<CommentPresentation[]> => { |
| 16 | + const input = CommentActionInput.getComments.parse(_input); |
| 17 | + |
| 18 | + const query = sql` |
| 19 | + SELECT get_comments($1, $2) as comments |
| 20 | + `; |
| 21 | + |
| 22 | + const execution_response: any = await pgClient?.executeSQL(query, [ |
| 23 | + input.resource_id, |
| 24 | + input.resource_type, |
| 25 | + ]); |
| 26 | + return execution_response?.rows?.[0]?.comments || []; |
17 | 27 | }; |
18 | 28 |
|
19 | | -export const createComment = async ( |
| 29 | +export const createMyComment = async ( |
20 | 30 | input: z.infer<typeof CommentActionInput.create> |
21 | 31 | ) => { |
| 32 | + const sessionId = await authID(); |
| 33 | + if (!sessionId) { |
| 34 | + throw new ActionException("Unauthorized: No session ID found"); |
| 35 | + } |
22 | 36 | const { resource_id, resource_type, body } = input; |
23 | 37 |
|
24 | | - // Create the comment in the database |
| 38 | + switch (resource_type) { |
| 39 | + case "ARTICLE": |
| 40 | + // Validate that the resource exists |
| 41 | + const [exists] = await persistenceRepository.article.find({ |
| 42 | + where: eq("id", resource_id), |
| 43 | + limit: 1, |
| 44 | + columns: ["id"], |
| 45 | + }); |
| 46 | + if (!exists) { |
| 47 | + throw new ActionException("Resource not found"); |
| 48 | + } |
| 49 | + break; |
| 50 | + case "COMMENT": |
| 51 | + // Validate that the parent comment exists |
| 52 | + const [parentExists] = await persistenceRepository.comment.find({ |
| 53 | + where: eq("id", resource_id), |
| 54 | + limit: 1, |
| 55 | + columns: ["id"], |
| 56 | + }); |
| 57 | + if (!parentExists) { |
| 58 | + throw new ActionException("Parent comment not found"); |
| 59 | + } |
| 60 | + break; |
| 61 | + default: |
| 62 | + throw new ActionException("Invalid resource type"); |
| 63 | + } |
| 64 | + |
| 65 | + const created = await persistenceRepository.comment.insert([ |
| 66 | + { |
| 67 | + body, |
| 68 | + resource_id, |
| 69 | + resource_type, |
| 70 | + user_id: sessionId, |
| 71 | + }, |
| 72 | + ]); |
25 | 73 |
|
26 | | - // return newComment[0]; |
| 74 | + return created?.rows?.[0]; |
27 | 75 | }; |
28 | 76 |
|
29 | 77 | export const deleteComment = async ( |
|
0 commit comments