Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
import { json, type RequestEvent } from '@sveltejs/kit'; | |
import { tokenIsAvailable } from '$lib/utils'; | |
import { REACTION_EMOJIS } from "$lib/utils"; | |
import prisma from '$lib/prisma'; | |
/** @type {import('./$types').RequestHandler} */ | |
export async function POST({ cookies, request, params } : RequestEvent) { | |
const id = params.id | |
const token = cookies.get('hf_access_token') | |
if (!token) { | |
return json({ | |
error: "You must be logged" | |
}, { status: 401 }) | |
} | |
const is_token_available = await tokenIsAvailable(token) | |
if (!is_token_available) { | |
return json({ | |
error: "Invalid token" | |
}, { status: 401 }) | |
} | |
const { emoji } = await request.json() | |
if (!REACTION_EMOJIS.includes(emoji)) { | |
return json({ | |
error: "Invalid emoji" | |
}, { status: 400 }) | |
} | |
const gallery = await prisma.gallery.findUnique({ | |
where: { | |
id | |
} | |
}) | |
if (!gallery) { | |
return json({ | |
error: "Gallery not found" | |
}, { status: 404 }) | |
} | |
const reaction_exist = await prisma.reaction.findFirst({ | |
where: { | |
galleryId: id, | |
userId: is_token_available.sub, | |
emoji | |
} | |
}) | |
if (reaction_exist) { | |
await prisma.reaction.delete({ | |
where: { | |
id: reaction_exist.id | |
} | |
}) | |
return json({ | |
success: true, | |
delete: true, | |
id: reaction_exist.id | |
}) | |
} | |
const new_reaction = await prisma.reaction.create({ | |
data: { | |
emoji, | |
gallery: { | |
connect: { | |
id: id | |
} | |
}, | |
user: { | |
connect: { | |
sub: is_token_available.sub | |
} | |
} | |
} | |
}) | |
return json({ | |
success: true, | |
delete: false, | |
id: new_reaction.id | |
}) | |
} | |