Skip to content

Commit 32bffc1

Browse files
committed
fix: update resource ID in GET comments API and streamline SQL query execution
1 parent 6c2a1b4 commit 32bffc1

File tree

3 files changed

+12
-77
lines changed

3 files changed

+12
-77
lines changed

src/app/api/play/route.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { NextResponse } from "next/server";
33

44
export async function GET() {
55
const comments = await getComments({
6-
resource_id: "05553726-5168-41f3-b2d1-5d27cf5c8beb",
6+
resource_id: "16196e73-275a-4af5-9186-39a5fec4244e",
77
resource_type: "ARTICLE",
88
});
99

src/backend/persistence/sql-functions/get_comments.sql

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
-- Thanks claude 🥰 for the help with this function
2+
-- Function to recursively fetch comments and their replies up to a certain level
3+
-- This function retrieves comments for a given resource and organizes them in a nested structure
4+
-- It uses recursion to fetch replies to comments, limiting the depth to 3 levels
5+
-- Function: public.get_comments(p_resource_id uuid, p_resource_type character varying, p_parent_id uuid DEFAULT NULL::uuid, p_current_level integer DEFAULT 0)
6+
7+
18
CREATE OR REPLACE FUNCTION public.get_comments(
29
p_resource_id uuid,
310
p_resource_type character varying,
@@ -52,7 +59,7 @@ BEGIN
5259
'id', u.id,
5360
'name', u.name,
5461
'email', u.email,
55-
'username', u.username -- Fixed: was 'useruser'
62+
'username', u.username
5663
),
5764
'replies', get_comments(p_resource_id, p_resource_type, c.id, p_current_level + 1)
5865
) ORDER BY c.created_at

src/backend/services/comment.action.ts

Lines changed: 3 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -13,86 +13,14 @@ export const getComments = async (
1313
const input = CommentActionInput.getComments.parse(_input);
1414

1515
const query = sql`
16-
WITH RECURSIVE comment_tree AS (
17-
-- Root comments
18-
SELECT
19-
id, body, user_id, created_at, resource_id, resource_type,
20-
0 as level,
21-
id as root_id
22-
FROM comments
23-
WHERE resource_id = $1
24-
AND resource_type = $2
25-
26-
UNION ALL
27-
28-
-- Nested replies (up to level 3)
29-
SELECT
30-
c.id, c.body, c.user_id, c.created_at, c.resource_id, c.resource_type,
31-
ct.level + 1,
32-
ct.root_id
33-
FROM comments c
34-
JOIN comment_tree ct ON c.resource_id = ct.id
35-
WHERE c.resource_type = 'COMMENT'
36-
AND ct.level < 3 -- This allows up to level 3 (0, 1, 2, 3)
37-
),
38-
-- Function to build replies recursively
39-
build_replies(parent_id, max_level) AS (
40-
SELECT
41-
ct.resource_id as parent_id,
42-
3 as max_level,
43-
json_agg(
44-
json_build_object(
45-
'id', ct.id,
46-
'body', ct.body,
47-
'level', ct.level,
48-
'created_at', ct.created_at,
49-
'parent_id', ct.resource_id,
50-
'author', json_build_object(
51-
'name', u.name,
52-
'email', u.email
53-
),
54-
'replies', CASE
55-
WHEN ct.level < 3 THEN
56-
COALESCE(
57-
(SELECT json_agg(child_reply ORDER BY (child_reply->>'created_at')::timestamp)
58-
FROM build_replies(ct.id, 3) br
59-
CROSS JOIN json_array_elements(br.replies) as child_reply),
60-
'[]'::json
61-
)
62-
ELSE '[]'::json
63-
END
64-
) ORDER BY ct.created_at
65-
) as replies
66-
FROM comment_tree ct
67-
JOIN users u ON ct.user_id = u.id
68-
WHERE ct.resource_id = parent_id AND ct.level > 0
69-
GROUP BY ct.resource_id
70-
)
71-
SELECT json_agg(
72-
json_build_object(
73-
'id', ct.id,
74-
'body', ct.body,
75-
'level', ct.level,
76-
'created_at', ct.created_at,
77-
'parent_id', null,
78-
'author', json_build_object(
79-
'name', u.name,
80-
'email', u.email
81-
),
82-
'replies', COALESCE(br.replies, '[]'::json)
83-
) ORDER BY ct.created_at
84-
) as comments
85-
FROM comment_tree ct
86-
JOIN users u ON ct.user_id = u.id
87-
LEFT JOIN build_replies(ct.id, 3) br ON true
88-
WHERE ct.level = 0;
16+
SELECT get_comments($1, $2);
8917
`;
9018

91-
const comments = await pgClient?.executeSQL(query, [
19+
const execution_response: any = await pgClient?.executeSQL(query, [
9220
input.resource_id,
9321
input.resource_type,
9422
]);
95-
return comments?.rows?.[0]; // Placeholder for actual database query
23+
return execution_response?.rows?.[0]?.get_comments;
9624
};
9725

9826
export const createMyComment = async (

0 commit comments

Comments
 (0)