el.xwx.moe/lib/api/controllers/public/getCollection.ts

46 lines
1.0 KiB
TypeScript
Raw Normal View History

2023-05-29 14:40:23 -05:00
import { prisma } from "@/lib/api/db";
import { PublicLinkRequestQuery } from "@/types/global";
2023-05-29 14:40:23 -05:00
2023-06-15 07:39:30 -05:00
export default async function getCollection(body: string) {
const query: PublicLinkRequestQuery = JSON.parse(decodeURIComponent(body));
console.log(query);
2023-05-29 14:40:23 -05:00
let data;
const collection = await prisma.collection.findFirst({
where: {
2023-06-15 07:48:47 -05:00
id: query.collectionId,
2023-05-29 14:40:23 -05:00
isPublic: true,
},
});
if (collection) {
const links = await prisma.link.findMany({
2023-07-21 09:17:26 -05:00
take: Number(process.env.PAGINATION_TAKE_COUNT) || 20,
2023-06-15 07:39:30 -05:00
skip: query.cursor ? 1 : undefined,
cursor: query.cursor
? {
2023-06-15 07:48:47 -05:00
id: query.cursor,
2023-06-15 07:39:30 -05:00
}
: undefined,
2023-05-29 14:40:23 -05:00
where: {
collection: {
2023-06-15 07:48:47 -05:00
id: query.collectionId,
},
},
include: {
tags: true,
},
orderBy: {
createdAt: "desc",
2023-05-29 14:40:23 -05:00
},
});
data = { ...collection, links: [...links] };
2023-05-29 14:40:23 -05:00
return { response: data, status: 200 };
} else {
return { response: "Collection not found...", status: 400 };
}
}