|
1 | 1 | "use server"; |
2 | 2 |
|
| 3 | +import { and, eq } from "sqlkit"; |
3 | 4 | import z from "zod"; |
| 5 | +import { persistenceRepository } from "../persistence/persistence-repositories"; |
| 6 | +import { pgClient } from "../persistence/clients"; |
4 | 7 | import { BookmarkActionInput } from "./inputs/bookmark.input"; |
5 | | -import { authID } from "./session.actions"; |
6 | 8 | import { ActionException, handleActionException } from "./RepositoryException"; |
7 | | -import { persistenceRepository } from "../persistence/persistence-repositories"; |
8 | | -import { and, eq } from "sqlkit"; |
| 9 | +import { authID } from "./session.actions"; |
| 10 | +import { BookmarkArticlePresentation } from "../models/domain-models"; |
| 11 | + |
| 12 | +const sql = String.raw; |
9 | 13 |
|
10 | 14 | export async function toggleResourceBookmark( |
11 | 15 | _input: z.infer<typeof BookmarkActionInput.toggleBookmarkInput> |
@@ -56,47 +60,71 @@ export async function toggleResourceBookmark( |
56 | 60 | } |
57 | 61 |
|
58 | 62 | export async function myBookmarks( |
59 | | - _input: z.infer<typeof BookmarkActionInput.toggleBookmarkInput> |
| 63 | + _input: z.infer<typeof BookmarkActionInput.myBookmarks> |
60 | 64 | ) { |
61 | 65 | try { |
62 | 66 | const sessionUserId = await authID(); |
63 | 67 | if (!sessionUserId) { |
64 | 68 | throw new ActionException("Unauthorized"); |
65 | 69 | } |
66 | | - const input = |
67 | | - await BookmarkActionInput.toggleBookmarkInput.parseAsync(_input); |
68 | 70 |
|
69 | | - // ----------- |
70 | | - const [existingBookmark] = await persistenceRepository.bookmark.find({ |
71 | | - limit: 1, |
72 | | - where: and( |
73 | | - eq("resource_id", input.resource_id), |
74 | | - eq("resource_type", input.resource_type), |
75 | | - eq("user_id", sessionUserId) |
76 | | - ), |
77 | | - }); |
| 71 | + const input = await BookmarkActionInput.myBookmarks.parseAsync(_input); |
| 72 | + const resourceType = "ARTICLE"; |
| 73 | + const offset = input.page > 1 ? (input.page - 1) * input.limit : 0; |
78 | 74 |
|
79 | | - if (existingBookmark) { |
80 | | - // If bookmark exists, delete it |
81 | | - await persistenceRepository.bookmark.delete({ |
82 | | - where: and( |
83 | | - eq("resource_id", input.resource_id), |
84 | | - eq("resource_type", input.resource_type), |
85 | | - eq("user_id", sessionUserId) |
86 | | - ), |
87 | | - }); |
88 | | - return { bookmarked: false }; |
89 | | - } |
| 75 | + const countQuery = sql` |
| 76 | + SELECT COUNT(*) AS totalCount |
| 77 | + FROM bookmarks |
| 78 | + WHERE user_id = $1 AND resource_type = $2 |
| 79 | + `; |
90 | 80 |
|
91 | | - // If bookmark does not exist, create it |
92 | | - await persistenceRepository.bookmark.insert([ |
93 | | - { |
94 | | - resource_id: input.resource_id, |
95 | | - resource_type: input.resource_type, |
96 | | - user_id: sessionUserId, |
97 | | - }, |
| 81 | + const countResult: any = await pgClient?.executeSQL(countQuery, [ |
| 82 | + sessionUserId, |
| 83 | + resourceType, |
98 | 84 | ]); |
99 | | - return { bookmarked: true }; |
| 85 | + const totalCount = countResult?.rows[0]?.totalcount; |
| 86 | + const totalPages = Math.ceil(totalCount / input.limit); |
| 87 | + |
| 88 | + const bookmarksQuery = sql` |
| 89 | + SELECT |
| 90 | + bookmarks.*, |
| 91 | + json_build_object( |
| 92 | + 'id', articles.id, |
| 93 | + 'title', articles.title, |
| 94 | + 'cover_image', articles.cover_image, |
| 95 | + 'path', concat(users.username, '/', articles.handle), |
| 96 | + 'author', json_build_object( |
| 97 | + 'id', users.id, |
| 98 | + 'name', users.name, |
| 99 | + 'username', users.username, |
| 100 | + 'email', users.email, |
| 101 | + 'profile_photo', users.profile_photo |
| 102 | + ) |
| 103 | + ) AS article |
| 104 | + FROM bookmarks |
| 105 | + LEFT JOIN articles ON articles.id = bookmarks.resource_id |
| 106 | + LEFT JOIN users ON users.id = articles.author_id |
| 107 | + WHERE bookmarks.user_id = $1 AND bookmarks.resource_type = $2 |
| 108 | + ORDER BY bookmarks.created_at DESC |
| 109 | + LIMIT $3 OFFSET $4 |
| 110 | + `; |
| 111 | + |
| 112 | + const bookmarks = await pgClient?.executeSQL(bookmarksQuery, [ |
| 113 | + sessionUserId, |
| 114 | + resourceType, |
| 115 | + input.limit, |
| 116 | + offset, |
| 117 | + ]); |
| 118 | + |
| 119 | + return { |
| 120 | + nodes: bookmarks?.rows as BookmarkArticlePresentation[], |
| 121 | + meta: { |
| 122 | + totalCount: Number(totalCount), |
| 123 | + currentPage: input.page, |
| 124 | + hasNextPage: input.page < totalPages, |
| 125 | + totalPages, |
| 126 | + }, |
| 127 | + }; |
100 | 128 | } catch (error) { |
101 | 129 | handleActionException(error); |
102 | 130 | } |
|
0 commit comments