2023-08-14 05:12:02 +00:00
|
|
|
import { error, redirect } from '@sveltejs/kit';
|
2023-06-16 06:28:49 +00:00
|
|
|
import { superValidate } from 'sveltekit-superforms/server';
|
|
|
|
|
import prisma from '$lib/prisma.js';
|
|
|
|
|
import { list_game_request_schema } from '$lib/zodValidation';
|
2023-08-14 05:12:02 +00:00
|
|
|
import { modifyListGameSchema } from '$lib/config/zod-schemas.js';
|
2023-06-16 06:28:49 +00:00
|
|
|
|
|
|
|
|
export async function load({ params, locals }) {
|
|
|
|
|
const session = await locals.auth.validate();
|
|
|
|
|
if (!session) {
|
|
|
|
|
throw redirect(302, '/auth/signin');
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-14 05:12:02 +00:00
|
|
|
console.log('Wishlist load User id', session.user);
|
|
|
|
|
|
2023-06-16 06:28:49 +00:00
|
|
|
try {
|
2023-08-14 05:12:02 +00:00
|
|
|
let wishlist = await prisma.wishlist.findUnique({
|
2023-06-16 06:28:49 +00:00
|
|
|
where: {
|
2023-08-14 05:12:02 +00:00
|
|
|
user_id: session?.user?.userId
|
2023-06-16 06:28:49 +00:00
|
|
|
},
|
|
|
|
|
include: {
|
2023-08-14 05:12:02 +00:00
|
|
|
items: {
|
|
|
|
|
include: {
|
|
|
|
|
game: {
|
|
|
|
|
select: {
|
|
|
|
|
id: true,
|
|
|
|
|
name: true,
|
|
|
|
|
thumb_url: true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-06-16 06:28:49 +00:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2023-08-14 05:12:02 +00:00
|
|
|
if (!wishlist) {
|
|
|
|
|
throw redirect(302, '/404');
|
2023-06-16 06:28:49 +00:00
|
|
|
}
|
|
|
|
|
|
2023-08-14 05:12:02 +00:00
|
|
|
console.log('wishlist', wishlist);
|
|
|
|
|
|
2023-06-16 06:28:49 +00:00
|
|
|
return {
|
2023-08-14 05:12:02 +00:00
|
|
|
games: wishlist?.items
|
2023-06-16 06:28:49 +00:00
|
|
|
};
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.error(e);
|
|
|
|
|
}
|
2023-08-14 05:12:02 +00:00
|
|
|
redirect(302, '/404');
|
2023-06-16 06:28:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const actions = {
|
|
|
|
|
// Add game to a wishlist
|
|
|
|
|
add: async (event) => {
|
|
|
|
|
const { params, locals, request } = event;
|
2023-08-14 05:12:02 +00:00
|
|
|
const form = await superValidate(event, modifyListGameSchema);
|
2023-06-16 06:28:49 +00:00
|
|
|
|
2023-08-14 05:12:02 +00:00
|
|
|
try {
|
|
|
|
|
const session = await locals.auth.validate();
|
|
|
|
|
if (!session) {
|
|
|
|
|
throw redirect(302, '/auth/signin');
|
2023-06-16 06:28:49 +00:00
|
|
|
}
|
|
|
|
|
|
2023-08-14 05:12:02 +00:00
|
|
|
let game = await prisma.game.findUnique({
|
|
|
|
|
where: {
|
|
|
|
|
id: form.data.id
|
2023-06-26 06:14:26 +00:00
|
|
|
}
|
|
|
|
|
});
|
2023-06-16 06:28:49 +00:00
|
|
|
|
2023-08-14 05:12:02 +00:00
|
|
|
if (!game) {
|
|
|
|
|
// game = await prisma.game.create({
|
|
|
|
|
// data: {
|
|
|
|
|
// name: form.name
|
|
|
|
|
// }
|
|
|
|
|
// });
|
|
|
|
|
console.log('game not found');
|
|
|
|
|
throw redirect(302, '/404');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (game) {
|
|
|
|
|
const wishlist = await prisma.wishlist.findUnique({
|
|
|
|
|
where: {
|
|
|
|
|
user_id: session.user.userId
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!wishlist) {
|
|
|
|
|
console.log('Wishlist not found');
|
|
|
|
|
return error(404, 'Wishlist not found');
|
2023-06-16 06:28:49 +00:00
|
|
|
}
|
|
|
|
|
|
2023-08-14 05:12:02 +00:00
|
|
|
await prisma.wishlistItem.create({
|
|
|
|
|
data: {
|
|
|
|
|
game_id: game.id,
|
|
|
|
|
wishlist_id: wishlist.id
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
form
|
|
|
|
|
};
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.error(e);
|
|
|
|
|
return error(500, 'Something went wrong');
|
|
|
|
|
}
|
2023-06-16 06:28:49 +00:00
|
|
|
},
|
|
|
|
|
// Create new wishlist
|
|
|
|
|
create: async ({ params, locals, request }) => {
|
|
|
|
|
const session = await locals.auth.validate();
|
|
|
|
|
if (!session) {
|
|
|
|
|
throw redirect(302, '/auth/signin');
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
// Delete a wishlist
|
|
|
|
|
delete: async ({ params, locals, request }) => {
|
|
|
|
|
const session = await locals.auth.validate();
|
|
|
|
|
if (!session) {
|
|
|
|
|
throw redirect(302, '/auth/signin');
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
// Remove game from a wishlist
|
2023-08-14 05:12:02 +00:00
|
|
|
remove: async (event) => {
|
|
|
|
|
const { params, locals, request } = event;
|
|
|
|
|
const form = await superValidate(event, modifyListGameSchema);
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const session = await locals.auth.validate();
|
|
|
|
|
if (!session) {
|
|
|
|
|
throw redirect(302, '/auth/signin');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let game = await prisma.game.findUnique({
|
|
|
|
|
where: {
|
|
|
|
|
id: form.data.id
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!game) {
|
|
|
|
|
// game = await prisma.game.create({
|
|
|
|
|
// data: {
|
|
|
|
|
// name: form.name
|
|
|
|
|
// }
|
|
|
|
|
// });
|
|
|
|
|
console.log('game not found');
|
|
|
|
|
throw redirect(302, '/404');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (game) {
|
|
|
|
|
const wishlist = await prisma.wishlist.findUnique({
|
|
|
|
|
where: {
|
|
|
|
|
user_id: session.user.userId
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!wishlist) {
|
|
|
|
|
console.log('Wishlist not found');
|
|
|
|
|
return error(404, 'Wishlist not found');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await prisma.wishlistItem.delete({
|
|
|
|
|
where: {
|
|
|
|
|
wishlist_id: wishlist.id,
|
|
|
|
|
game_id: game.id
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
form
|
|
|
|
|
};
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.error(e);
|
|
|
|
|
return error(500, 'Something went wrong');
|
2023-06-16 06:28:49 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|