import { userIdentity } from '$lib/AniList/identity'; import { removeUserBadge, updateUserBadge } from '$lib/Database/badges'; import { getUserBadges } from '$lib/Database/badges'; import { addUserBadge } from '$lib/Database/badges'; export const GET = async ({ url }) => { return Response.json(await getUserBadges(Number(url.searchParams.get('id') || 0))); }; export const DELETE = async ({ url, cookies }) => { const userCookie = cookies.get('user'); if (!userCookie) { return new Response('Unauthenticated', { status: 401 }); } const user = JSON.parse(userCookie); const identity = await userIdentity({ tokenType: user['token_type'], expiresIn: user['expires_in'], accessToken: user['access_token'], refreshToken: user['refresh_token'] }); await removeUserBadge(identity.id, Number(url.searchParams.get('id'))); return Response.json(await getUserBadges(identity.id)); }; export const PUT = async ({ cookies, url }) => { const userCookie = cookies.get('user'); if (!userCookie) { return new Response('Unauthenticated', { status: 401 }); } const user = JSON.parse(userCookie); const identity = await userIdentity({ tokenType: user['token_type'], expiresIn: user['expires_in'], accessToken: user['access_token'], refreshToken: user['refresh_token'] }); if ( (await getUserBadges(identity.id)).find( (badge) => Number(badge.id) === Number(url.searchParams.get('update')) ) ) { await updateUserBadge(identity.id, Number(url.searchParams.get('update')), { post: url.searchParams.get('post') || undefined, image: url.searchParams.get('image') || undefined, description: url.searchParams.get('description') || undefined, time: url.searchParams.get('time') || undefined, category: url.searchParams.get('category') || undefined }); } else { await addUserBadge(identity.id, { post: url.searchParams.get('post') || undefined, image: url.searchParams.get('image') || undefined, description: url.searchParams.get('description') || undefined, time: url.searchParams.get('time') || undefined, category: url.searchParams.get('category') || undefined }); } return Response.json({}); };