|
1 | | -import { supabaseAdmin } from "@changes-page/supabase/admin"; |
2 | | -import { Database } from "@changes-page/supabase/types"; |
| 1 | +import { supabaseAdmin } from "@changespage/supabase/admin"; |
| 2 | +import { Database } from "@changespage/supabase/types"; |
3 | 3 | import { |
4 | 4 | IPage, |
5 | 5 | IPageSettings, |
6 | 6 | IPost, |
7 | 7 | IRoadmapBoard, |
8 | 8 | IRoadmapColumn, |
9 | 9 | IRoadmapItem, |
10 | | -} from "@changes-page/supabase/types/page"; |
| 10 | +} from "@changespage/supabase/types/page"; |
11 | 11 | import { sanitizeCss } from "./css"; |
12 | 12 |
|
13 | 13 | const PAGINATION_LIMIT = 50; |
@@ -441,9 +441,37 @@ async function getRoadmapBySlug( |
441 | 441 | return { board, columns, items }; |
442 | 442 | } |
443 | 443 |
|
| 444 | +async function fetchPostsWithPagination( |
| 445 | + pageId: string, |
| 446 | + { limit, offset }: { limit?: number; offset?: number } |
| 447 | +): Promise<{ posts: IPost[]; postsCount: number }> { |
| 448 | + const effectiveLimit = Math.min(limit ?? PAGINATION_LIMIT, PAGINATION_LIMIT); |
| 449 | + const effectiveOffset = offset ?? 0; |
| 450 | + |
| 451 | + const { |
| 452 | + data: posts, |
| 453 | + count: postsCount, |
| 454 | + error: postsError, |
| 455 | + } = await supabaseAdmin |
| 456 | + .from("posts") |
| 457 | + .select(postSelectParams, { count: "exact" }) |
| 458 | + .eq("page_id", String(pageId)) |
| 459 | + .eq("status", "published") |
| 460 | + .range(effectiveOffset, effectiveOffset + effectiveLimit - 1) |
| 461 | + .order("publication_date", { ascending: false }); |
| 462 | + |
| 463 | + if (postsError) { |
| 464 | + console.error("Fetch post error", postsError); |
| 465 | + throw new Error("Failed to fetch posts"); |
| 466 | + } |
| 467 | + |
| 468 | + return { posts: (posts ?? []) as Array<IPost>, postsCount: postsCount ?? 0 }; |
| 469 | +} |
| 470 | + |
444 | 471 | export { |
445 | 472 | fetchPostById, |
446 | 473 | fetchPosts, |
| 474 | + fetchPostsWithPagination, |
447 | 475 | fetchRenderData, |
448 | 476 | getRoadmapBySlug, |
449 | 477 | PAGINATION_LIMIT, |
|
0 commit comments