2023-05-29 14:40:23 -05:00
|
|
|
import { prisma } from "@/lib/api/db";
|
2023-06-14 17:34:54 -05:00
|
|
|
import { PublicLinkRequestQuery } from "@/types/global";
|
2023-05-29 14:40:23 -05:00
|
|
|
|
2023-06-14 17:34:54 -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: {
|
2023-06-14 17:34:54 -05:00
|
|
|
id: Number(query.collectionId),
|
2023-05-29 14:40:23 -05:00
|
|
|
isPublic: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
if (collection) {
|
2023-06-14 17:34:54 -05:00
|
|
|
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: {
|
2023-06-14 17:34:54 -05:00
|
|
|
collection: {
|
|
|
|
id: Number(query.collectionId),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
include: {
|
|
|
|
tags: true,
|
|
|
|
},
|
|
|
|
orderBy: {
|
|
|
|
createdAt: "desc",
|
2023-05-29 14:40:23 -05:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2023-06-14 17:34:54 -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 };
|
|
|
|
}
|
|
|
|
}
|