Skip to content

Commit 8d121e4

Browse files
Merge pull request #15 from Shitanshukumar607/collections
add collections
2 parents fde203e + 9286449 commit 8d121e4

File tree

18 files changed

+2192
-13
lines changed

18 files changed

+2192
-13
lines changed

app/api/works/route.ts

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import { createClient } from "@/lib/supabase/server";
2+
import { NextRequest, NextResponse } from "next/server";
3+
4+
export async function GET(request: NextRequest) {
5+
try {
6+
const { searchParams } = new URL(request.url);
7+
const page = parseInt(searchParams.get("page") || "1");
8+
const limit = parseInt(searchParams.get("limit") || "12");
9+
const offset = (page - 1) * limit;
10+
11+
const supabase = await createClient();
12+
13+
// Get the current user
14+
const {
15+
data: { user },
16+
error: authError,
17+
} = await supabase.auth.getUser();
18+
19+
if (authError || !user) {
20+
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
21+
}
22+
23+
// Fetch works for the current user
24+
const { data: works, error: worksError } = await supabase
25+
.from("works")
26+
.select("*")
27+
.eq("user_id", user.id)
28+
.order("created_at", { ascending: false })
29+
.range(offset, offset + limit - 1);
30+
31+
if (worksError) {
32+
console.error("Error fetching works:", worksError);
33+
return NextResponse.json(
34+
{ error: "Failed to fetch works" },
35+
{ status: 500 },
36+
);
37+
}
38+
39+
// Get total count for pagination
40+
const { count, error: countError } = await supabase
41+
.from("works")
42+
.select("*", { count: "exact", head: true })
43+
.eq("user_id", user.id);
44+
45+
if (countError) {
46+
console.error("Error counting works:", countError);
47+
return NextResponse.json(
48+
{ error: "Failed to count works" },
49+
{ status: 500 },
50+
);
51+
}
52+
53+
return NextResponse.json({
54+
works: works || [],
55+
total: count || 0,
56+
page,
57+
limit,
58+
});
59+
} catch (error) {
60+
console.error("Unexpected error:", error);
61+
return NextResponse.json(
62+
{ error: "Internal server error" },
63+
{ status: 500 },
64+
);
65+
}
66+
}

app/collections/layout.tsx

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { Metadata } from "next";
2+
3+
export const metadata: Metadata = {
4+
title: "My Collections - ImgToSVG",
5+
description:
6+
"View and manage your converted SVG works. Download, search, and organize your image to SVG conversions.",
7+
keywords: [
8+
"SVG",
9+
"collections",
10+
"image converter",
11+
"download",
12+
"vector graphics",
13+
],
14+
};
15+
16+
export default function CollectionsLayout({
17+
children,
18+
}: {
19+
children: React.ReactNode;
20+
}) {
21+
return <>{children}</>;
22+
}

0 commit comments

Comments
 (0)