61 lines
1.5 KiB
TypeScript
61 lines
1.5 KiB
TypeScript
import { NextRequest, NextResponse } from "next/server";
|
|
import { z } from "zod";
|
|
|
|
import { auth } from "@/lib/auth";
|
|
import { prisma } from "@/lib/prisma";
|
|
|
|
const likeSchema = z.object({
|
|
miiId: z.coerce.number().int({ message: "Mii ID must be an integer" }).positive({ message: "Mii ID must be valid" }),
|
|
});
|
|
|
|
export async function PATCH(request: NextRequest) {
|
|
// todo: rate limit
|
|
|
|
const session = await auth();
|
|
if (!session) return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
|
|
|
const body = await request.json();
|
|
const parsed = likeSchema.safeParse(body);
|
|
|
|
if (!parsed.success) return NextResponse.json({ error: parsed.error.errors[0].message }, { status: 400 });
|
|
const { miiId } = parsed.data;
|
|
|
|
const result = await prisma.$transaction(async (tx) => {
|
|
const existingLike = await tx.like.findUnique({
|
|
where: {
|
|
userId_miiId: {
|
|
userId: Number(session.user.id),
|
|
miiId,
|
|
},
|
|
},
|
|
});
|
|
|
|
if (existingLike) {
|
|
// Remove the like if it exists
|
|
await tx.like.delete({
|
|
where: {
|
|
userId_miiId: {
|
|
userId: Number(session.user.id),
|
|
miiId,
|
|
},
|
|
},
|
|
});
|
|
} else {
|
|
// Add a like if it doesn't exist
|
|
await tx.like.create({
|
|
data: {
|
|
userId: Number(session.user.id),
|
|
miiId,
|
|
},
|
|
});
|
|
}
|
|
|
|
const likeCount = await tx.like.count({
|
|
where: { miiId },
|
|
});
|
|
|
|
return { liked: !existingLike, count: likeCount };
|
|
});
|
|
|
|
return NextResponse.json({ success: true, liked: result.liked, count: result.count });
|
|
}
|