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
|
import { authOptions } from "@/lib/auth";
import { LinkCrawlerQueue } from "@remember/shared/queues";
import prisma from "@remember/db";
import {
zNewBookmarkedLinkRequestSchema,
ZGetLinksResponse,
ZBookmarkedLink,
} from "@/lib/types/api/links";
import { getServerSession } from "next-auth";
import { NextRequest, NextResponse } from "next/server";
export async function POST(request: NextRequest) {
// TODO: We probably should be using an API key here instead of the session;
const session = await getServerSession(authOptions);
if (!session) {
return new Response(null, { status: 401 });
}
const linkRequest = zNewBookmarkedLinkRequestSchema.safeParse(
await request.json(),
);
if (!linkRequest.success) {
return NextResponse.json(
{
error: linkRequest.error.toString(),
},
{ status: 400 },
);
}
const link = await prisma.bookmarkedLink.create({
data: {
url: linkRequest.data.url,
userId: session.user.id,
},
});
// Enqueue crawling request
await LinkCrawlerQueue.add("crawl", {
linkId: link.id,
url: link.url,
});
let response: ZBookmarkedLink = { ...link };
return NextResponse.json(response, { status: 201 });
}
export async function GET() {
// TODO: We probably should be using an API key here instead of the session;
const session = await getServerSession(authOptions);
if (!session) {
return new Response(null, { status: 401 });
}
const links = await prisma.bookmarkedLink.findMany({
where: {
userId: session.user.id,
},
select: {
id: true,
url: true,
createdAt: true,
details: {
select: {
title: true,
description: true,
imageUrl: true,
favicon: true,
},
},
},
});
let response: ZGetLinksResponse = { links };
return NextResponse.json(response);
}
|