2023-07-18 21:23:45 +00:00
|
|
|
import { error, json } from '@sveltejs/kit';
|
2023-11-05 00:03:28 +00:00
|
|
|
import prisma from '$lib/prisma';
|
2023-07-18 21:23:45 +00:00
|
|
|
|
|
|
|
|
// Search a user's collection
|
|
|
|
|
export async function GET({ url, locals, params }) {
|
|
|
|
|
const searchParams = Object.fromEntries(url.searchParams);
|
|
|
|
|
const q = searchParams?.q || '';
|
|
|
|
|
const limit = parseInt(searchParams?.limit) || 10;
|
|
|
|
|
const skip = parseInt(searchParams?.skip) || 0;
|
|
|
|
|
const order = searchParams?.order || 'asc';
|
|
|
|
|
const sort = searchParams?.sort || 'name';
|
|
|
|
|
const collection_id = params.id;
|
|
|
|
|
const session = await locals.auth.validate();
|
|
|
|
|
console.log('url', url);
|
|
|
|
|
console.log('username', locals?.user?.id);
|
|
|
|
|
|
|
|
|
|
if (!session) {
|
|
|
|
|
throw error(401, { message: 'Unauthorized' });
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-05 00:03:28 +00:00
|
|
|
let collection = await prisma.collection.findUnique({
|
2023-07-18 21:23:45 +00:00
|
|
|
where: {
|
2023-07-30 05:00:51 +00:00
|
|
|
user_id: locals.user.userId
|
2023-07-18 21:23:45 +00:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
console.log('collection', collection);
|
|
|
|
|
|
|
|
|
|
if (!collection) {
|
|
|
|
|
console.log('Collection was not found');
|
|
|
|
|
throw error(404, { message: 'Collection was not found' });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const orderBy = { [sort]: order };
|
2023-11-05 00:03:28 +00:00
|
|
|
let collection_items = await prisma.collectionItem.findMany({
|
2023-07-18 21:23:45 +00:00
|
|
|
where: {
|
|
|
|
|
collection_id,
|
|
|
|
|
AND: [
|
|
|
|
|
{
|
|
|
|
|
game: {
|
|
|
|
|
name: {
|
|
|
|
|
contains: q
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
},
|
|
|
|
|
orderBy: [
|
|
|
|
|
{
|
|
|
|
|
game: {
|
|
|
|
|
...orderBy
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
],
|
|
|
|
|
include: {
|
|
|
|
|
game: {
|
|
|
|
|
select: {
|
|
|
|
|
id: true,
|
|
|
|
|
name: true,
|
|
|
|
|
thumb_url: true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
skip,
|
|
|
|
|
take: limit
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return json(collection_items);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.error(e);
|
|
|
|
|
throw error(500, { message: 'Something went wrong' });
|
|
|
|
|
}
|
|
|
|
|
}
|