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

44 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
export default async function getCollection(query: PublicLinkRequestQuery) {
2023-05-29 14:40:23 -05:00
let data;
const collection = await prisma.collection.findFirst({
where: {
id: Number(query.collectionId),
2023-05-29 14:40:23 -05:00
isPublic: true,
},
});
if (collection) {
const links = await prisma.link.findMany({
take: Number(process.env.PAGINATION_TAKE_COUNT),
skip: query.cursor !== "undefined" ? 1 : undefined,
cursor:
query.cursor !== "undefined"
? {
id: Number(query.cursor),
}
: undefined,
2023-05-29 14:40:23 -05:00
where: {
collection: {
id: Number(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 };
}
}