1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
|
import { NextRequest } from "next/server";
import { db } from "@/server/db";
import { eq } from "drizzle-orm";
import {
chatThreads,
contentToSpace,
storedContent,
users,
} from "@repo/db/schema";
import { ensureAuth } from "../ensureAuth";
export const runtime = "edge";
export async function GET(req: NextRequest) {
const session = await ensureAuth(req);
if (!session) {
return new Response("Unauthorized", { status: 401 });
}
if (!process.env.BACKEND_SECURITY_KEY) {
return new Response("Missing BACKEND_SECURITY_KEY", { status: 500 });
}
const url = new URL(req.url);
const spaceId = url.searchParams.get("space");
try {
if (spaceId) {
const memories = await db
.select({
id: storedContent.id,
content: storedContent.content,
title: storedContent.title,
description: storedContent.description,
url: storedContent.url,
savedAt: storedContent.savedAt,
baseUrl: storedContent.baseUrl,
ogImage: storedContent.ogImage,
type: storedContent.type,
image: storedContent.image,
userId: storedContent.userId,
noteId: storedContent.noteId,
})
.from(storedContent)
.innerJoin(
contentToSpace,
eq(storedContent.id, contentToSpace.contentId),
)
.where(eq(contentToSpace.spaceId, parseInt(spaceId)));
return new Response(
JSON.stringify({
success: true,
data: { memories: memories },
}),
{ status: 200 },
);
}
const spaces = await db.query.space.findMany({
where: eq(users, session.user.id),
});
const memories = await db.query.storedContent.findMany({
where: eq(users, session.user.id),
});
return new Response(
JSON.stringify({
success: true,
data: { spaces: spaces, memories: memories },
}),
);
} catch (e) {
return new Response(
JSON.stringify({
success: false,
error: (e as Error).message,
}),
{ status: 400 },
);
}
}
|