refactor: add slug to mii like route

This commit is contained in:
trafficlunar 2025-04-14 18:10:51 +01:00
parent 4fec5f65f5
commit f00c998c30
2 changed files with 25 additions and 19 deletions

View file

@ -4,21 +4,20 @@ 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 slugSchema = z.coerce
.number({ message: "Mii ID must be a number" })
.int({ message: "Mii ID must be an integer" })
.positive({ message: "Mii ID must be valid" });
export async function PATCH(request: NextRequest, { params }: { params: Promise<{ id: string }> }) {
const session = await auth();
if (!session) return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
const body = await request.json();
const parsed = likeSchema.safeParse(body);
const { id: slugId } = await params;
const parsed = slugSchema.safeParse(slugId);
if (!parsed.success) return NextResponse.json({ error: parsed.error.errors[0].message }, { status: 400 });
const { miiId } = parsed.data;
const miiId = parsed.data;
const result = await prisma.$transaction(async (tx) => {
const existingLike = await tx.like.findUnique({