2024-09-01 19:22:00 +00:00
|
|
|
import { notSignedInMessage } from '$lib/flashMessages.js'
|
|
|
|
|
import { db } from '$lib/server/api/packages/drizzle'
|
|
|
|
|
import { userNotAuthenticated } from '$lib/server/auth-utils'
|
|
|
|
|
import { modifyListGameSchema } from '$lib/validations/zod-schemas'
|
|
|
|
|
import { type Actions, error, fail } from '@sveltejs/kit'
|
|
|
|
|
import { and, eq } from 'drizzle-orm'
|
|
|
|
|
import { redirect } from 'sveltekit-flash-message/server'
|
|
|
|
|
import { zod } from 'sveltekit-superforms/adapters'
|
|
|
|
|
import { superValidate } from 'sveltekit-superforms/server'
|
|
|
|
|
import { collection_items, collections, games } from '../../../../../lib/server/api/databases/tables'
|
2023-06-16 06:28:49 +00:00
|
|
|
|
2024-03-01 01:11:07 +00:00
|
|
|
export async function load(event) {
|
2024-09-01 19:22:00 +00:00
|
|
|
const { params, locals } = event
|
|
|
|
|
const { cuid } = params
|
2024-06-12 02:12:12 +00:00
|
|
|
|
2024-09-01 19:22:00 +00:00
|
|
|
const authedUser = await locals.getAuthedUser()
|
2024-08-26 00:53:32 +00:00
|
|
|
if (!authedUser) {
|
2024-09-01 19:22:00 +00:00
|
|
|
throw redirect(302, '/login', notSignedInMessage, event)
|
2023-06-16 06:28:49 +00:00
|
|
|
}
|
2024-04-17 19:02:51 +00:00
|
|
|
|
2024-08-26 00:53:32 +00:00
|
|
|
try {
|
2024-09-01 19:22:00 +00:00
|
|
|
const { data, errors } = await locals.api.collections[':cuid']
|
|
|
|
|
.$get({
|
|
|
|
|
param: { cuid },
|
|
|
|
|
})
|
|
|
|
|
.then(locals.parseApiResponse)
|
2024-04-17 19:02:51 +00:00
|
|
|
|
2024-08-26 00:53:32 +00:00
|
|
|
if (errors) {
|
2024-09-01 19:22:00 +00:00
|
|
|
return error(500, 'Failed to fetch collection')
|
2024-08-26 00:53:32 +00:00
|
|
|
}
|
2024-04-17 19:02:51 +00:00
|
|
|
|
2024-09-01 19:22:00 +00:00
|
|
|
const { collection } = data
|
2023-06-16 06:28:49 +00:00
|
|
|
|
2024-08-26 00:53:32 +00:00
|
|
|
if (!collection) {
|
2024-09-01 19:22:00 +00:00
|
|
|
redirect(302, '/404')
|
2024-08-26 00:53:32 +00:00
|
|
|
}
|
2024-02-18 08:03:08 +00:00
|
|
|
|
2024-09-01 19:22:00 +00:00
|
|
|
console.log('collection', collection)
|
2024-04-17 19:02:51 +00:00
|
|
|
|
2024-08-26 00:53:32 +00:00
|
|
|
return {
|
|
|
|
|
collection,
|
2024-09-01 19:22:00 +00:00
|
|
|
}
|
2024-08-26 00:53:32 +00:00
|
|
|
} catch (e) {
|
2024-09-01 19:22:00 +00:00
|
|
|
console.error(e)
|
2023-06-16 06:28:49 +00:00
|
|
|
}
|
2024-04-17 19:02:51 +00:00
|
|
|
|
2024-09-01 19:22:00 +00:00
|
|
|
redirect(302, '/404')
|
2024-08-26 00:53:32 +00:00
|
|
|
|
|
|
|
|
// const searchParams = Object.fromEntries(url?.searchParams);
|
|
|
|
|
// console.log('searchParams', searchParams);
|
|
|
|
|
// const q = searchParams?.q;
|
|
|
|
|
// const limit = parseInt(searchParams?.limit) || 10;
|
|
|
|
|
// const skip = parseInt(searchParams?.skip) || 0;
|
|
|
|
|
//
|
|
|
|
|
// const searchData = {
|
|
|
|
|
// q,
|
|
|
|
|
// limit,
|
|
|
|
|
// skip,
|
|
|
|
|
// };
|
|
|
|
|
//
|
|
|
|
|
// const searchForm = await superValidate(searchData, zod(search_schema));
|
|
|
|
|
// const listManageForm = await superValidate(zod(modifyListGameSchema));
|
|
|
|
|
//
|
|
|
|
|
// const collection = await db.query.collections.findFirst({
|
|
|
|
|
// columns: {
|
|
|
|
|
// id: true,
|
|
|
|
|
// cuid: true,
|
|
|
|
|
// name: true,
|
|
|
|
|
// },
|
|
|
|
|
// where: and(eq(collections.user_id, user!.id!), eq(collections.cuid, id)),
|
|
|
|
|
// });
|
|
|
|
|
// console.log('collection', collection);
|
|
|
|
|
|
|
|
|
|
// if (!collection) {
|
|
|
|
|
// console.log('Collection was not found');
|
|
|
|
|
// error(404, 'Collection was not found');
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// const collectionItems = await db.query.collection_items.findMany({
|
|
|
|
|
// columns: {
|
|
|
|
|
// collection_id: true,
|
|
|
|
|
// times_played: true,
|
|
|
|
|
// },
|
|
|
|
|
// where: eq(collection_items.collection_id, collection.id),
|
|
|
|
|
// with: {
|
|
|
|
|
// game: {
|
|
|
|
|
// columns: {
|
|
|
|
|
// id: true,
|
|
|
|
|
// name: true,
|
|
|
|
|
// thumb_url: true,
|
|
|
|
|
// },
|
|
|
|
|
// },
|
|
|
|
|
// },
|
|
|
|
|
// offset: skip,
|
|
|
|
|
// limit,
|
|
|
|
|
// });
|
|
|
|
|
//
|
|
|
|
|
// console.log('collection_items', collectionItems);
|
|
|
|
|
//
|
|
|
|
|
// const items: ListGame[] = [];
|
|
|
|
|
// for (const item of collectionItems) {
|
|
|
|
|
// console.log('item', item);
|
|
|
|
|
// const game = item.game;
|
|
|
|
|
// if (game) {
|
|
|
|
|
// items.push({
|
|
|
|
|
// game_id: '',
|
|
|
|
|
// in_wishlist: false,
|
|
|
|
|
// wishlist_id: '',
|
|
|
|
|
// id: game.id,
|
|
|
|
|
// collection_id: item.collection_id,
|
|
|
|
|
// game_name: game.name ?? "Game doesn't have a name",
|
|
|
|
|
// thumb_url: game.thumb_url,
|
|
|
|
|
// times_played: item.times_played ?? 0,
|
|
|
|
|
// in_collection: true,
|
|
|
|
|
// });
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// return {
|
|
|
|
|
// searchForm,
|
|
|
|
|
// listManageForm,
|
|
|
|
|
// collection: {
|
|
|
|
|
// name: collection.name,
|
|
|
|
|
// cuid: collection.cuid ?? '',
|
|
|
|
|
// },
|
|
|
|
|
// items,
|
|
|
|
|
// };
|
2023-06-16 06:28:49 +00:00
|
|
|
}
|
|
|
|
|
|
2023-11-05 06:20:34 +00:00
|
|
|
export const actions: Actions = {
|
2023-06-16 06:28:49 +00:00
|
|
|
// Add game to a wishlist
|
|
|
|
|
add: async (event) => {
|
2024-09-01 19:22:00 +00:00
|
|
|
const { locals } = event
|
|
|
|
|
const { user, session } = locals
|
2024-06-17 20:06:45 +00:00
|
|
|
if (userNotAuthenticated(user, session)) {
|
2024-09-01 19:22:00 +00:00
|
|
|
return fail(401)
|
2024-04-17 19:02:51 +00:00
|
|
|
}
|
|
|
|
|
|
2024-09-01 19:22:00 +00:00
|
|
|
const form = await superValidate(event, zod(modifyListGameSchema))
|
2024-06-12 02:12:12 +00:00
|
|
|
|
2024-04-17 19:02:51 +00:00
|
|
|
const game = await db.query.games.findFirst({
|
|
|
|
|
where: eq(games.id, form.data.id),
|
2024-09-01 19:22:00 +00:00
|
|
|
})
|
2024-04-17 19:02:51 +00:00
|
|
|
|
|
|
|
|
if (!game) {
|
|
|
|
|
// game = await prisma.game.create({
|
|
|
|
|
// data: {
|
|
|
|
|
// name: form.name
|
|
|
|
|
// }
|
|
|
|
|
// });
|
2024-09-01 19:22:00 +00:00
|
|
|
console.log('game not found')
|
|
|
|
|
redirect(302, '/404')
|
2024-04-17 19:02:51 +00:00
|
|
|
}
|
2023-06-16 06:28:49 +00:00
|
|
|
|
2024-04-17 19:02:51 +00:00
|
|
|
try {
|
|
|
|
|
const collection = await db.query.collections.findFirst({
|
2024-06-18 00:37:47 +00:00
|
|
|
where: eq(collections.user_id, user!.id!),
|
2024-09-01 19:22:00 +00:00
|
|
|
})
|
2023-06-16 06:28:49 +00:00
|
|
|
|
2024-04-17 19:02:51 +00:00
|
|
|
if (!collection) {
|
2024-09-01 19:22:00 +00:00
|
|
|
console.log('Wishlist not found')
|
|
|
|
|
return error(404, 'Wishlist not found')
|
2023-08-14 05:12:02 +00:00
|
|
|
}
|
|
|
|
|
|
2024-04-17 19:02:51 +00:00
|
|
|
await db.insert(collection_items).values({
|
|
|
|
|
game_id: game.id,
|
|
|
|
|
collection_id: collection.id,
|
|
|
|
|
times_played: 0,
|
2024-09-01 19:22:00 +00:00
|
|
|
})
|
2023-08-14 05:12:02 +00:00
|
|
|
|
|
|
|
|
return {
|
2024-04-17 17:56:33 +00:00
|
|
|
form,
|
2024-09-01 19:22:00 +00:00
|
|
|
}
|
2023-08-14 05:12:02 +00:00
|
|
|
} catch (e) {
|
2024-09-01 19:22:00 +00:00
|
|
|
console.error(e)
|
|
|
|
|
return error(500, 'Something went wrong')
|
2023-08-14 05:12:02 +00:00
|
|
|
}
|
2023-06-16 06:28:49 +00:00
|
|
|
},
|
|
|
|
|
// Create new wishlist
|
2024-06-12 02:12:12 +00:00
|
|
|
create: async (event) => {
|
2024-09-01 19:22:00 +00:00
|
|
|
const { locals } = event
|
|
|
|
|
const { user, session } = locals
|
2024-06-17 20:06:45 +00:00
|
|
|
if (userNotAuthenticated(user, session)) {
|
2024-09-01 19:22:00 +00:00
|
|
|
return fail(401)
|
2023-06-16 06:28:49 +00:00
|
|
|
}
|
2024-09-01 19:22:00 +00:00
|
|
|
return error(405, 'Method not allowed')
|
2023-06-16 06:28:49 +00:00
|
|
|
},
|
|
|
|
|
// Delete a wishlist
|
2024-06-12 02:12:12 +00:00
|
|
|
delete: async (event) => {
|
2024-09-01 19:22:00 +00:00
|
|
|
const { locals } = event
|
|
|
|
|
const { user, session } = locals
|
2024-06-17 20:06:45 +00:00
|
|
|
if (userNotAuthenticated(user, session)) {
|
2024-09-01 19:22:00 +00:00
|
|
|
return fail(401)
|
2023-06-16 06:28:49 +00:00
|
|
|
}
|
2024-09-01 19:22:00 +00:00
|
|
|
return error(405, 'Method not allowed')
|
2023-06-16 06:28:49 +00:00
|
|
|
},
|
|
|
|
|
// Remove game from a wishlist
|
2023-08-14 05:12:02 +00:00
|
|
|
remove: async (event) => {
|
2024-09-01 19:22:00 +00:00
|
|
|
const { locals } = event
|
|
|
|
|
const { user, session } = locals
|
2024-06-17 20:06:45 +00:00
|
|
|
if (userNotAuthenticated(user, session)) {
|
2024-09-01 19:22:00 +00:00
|
|
|
return fail(401)
|
2024-04-17 19:02:51 +00:00
|
|
|
}
|
2024-09-01 19:22:00 +00:00
|
|
|
const form = await superValidate(event, zod(modifyListGameSchema))
|
2023-08-14 05:12:02 +00:00
|
|
|
|
2024-04-17 19:02:51 +00:00
|
|
|
const game = await db.query.games.findFirst({
|
|
|
|
|
where: eq(games.id, form.data.id),
|
2024-09-01 19:22:00 +00:00
|
|
|
})
|
2024-04-17 19:02:51 +00:00
|
|
|
|
|
|
|
|
if (!game) {
|
2024-09-01 19:22:00 +00:00
|
|
|
console.log('game not found')
|
|
|
|
|
redirect(302, '/404')
|
2024-04-17 19:02:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const collection = await db.query.collections.findFirst({
|
2024-06-18 00:37:47 +00:00
|
|
|
where: eq(collections.user_id, user!.id!),
|
2024-09-01 19:22:00 +00:00
|
|
|
})
|
2023-08-14 05:12:02 +00:00
|
|
|
|
2024-04-17 19:02:51 +00:00
|
|
|
if (!collection) {
|
2024-09-01 19:22:00 +00:00
|
|
|
console.log('Collection not found')
|
|
|
|
|
return error(404, 'Collection not found')
|
2023-08-14 05:12:02 +00:00
|
|
|
}
|
|
|
|
|
|
2024-09-01 19:22:00 +00:00
|
|
|
await db.delete(collection_items).where(and(eq(collection_items.collection_id, collection.id), eq(collection_items.game_id, game.id)))
|
2023-08-14 05:12:02 +00:00
|
|
|
|
|
|
|
|
return {
|
2024-04-17 17:56:33 +00:00
|
|
|
form,
|
2024-09-01 19:22:00 +00:00
|
|
|
}
|
2023-08-14 05:12:02 +00:00
|
|
|
} catch (e) {
|
2024-09-01 19:22:00 +00:00
|
|
|
console.error(e)
|
|
|
|
|
return error(500, 'Something went wrong')
|
2023-06-16 06:28:49 +00:00
|
|
|
}
|
2024-04-17 17:56:33 +00:00
|
|
|
},
|
2024-09-01 19:22:00 +00:00
|
|
|
}
|