From 8f731dd3587d3e0a80338c4e3e7d57576780bc4c Mon Sep 17 00:00:00 2001 From: trafficlunar Date: Mon, 31 Mar 2025 20:20:16 +0100 Subject: [PATCH] fix: remove likes property in mii model --- .../migration.sql | 1 - prisma/schema.prisma | 1 - src/app/api/like/route.ts | 26 ++++++------------- src/app/components/mii-list.tsx | 4 +++ src/app/mii/[slug]/page.tsx | 26 ++++++++++++------- 5 files changed, 28 insertions(+), 30 deletions(-) rename prisma/migrations/{20250330163530_init => 20250331194845_init}/migration.sql (98%) diff --git a/prisma/migrations/20250330163530_init/migration.sql b/prisma/migrations/20250331194845_init/migration.sql similarity index 98% rename from prisma/migrations/20250330163530_init/migration.sql rename to prisma/migrations/20250331194845_init/migration.sql index 8fee47a..e53b21c 100644 --- a/prisma/migrations/20250330163530_init/migration.sql +++ b/prisma/migrations/20250331194845_init/migration.sql @@ -47,7 +47,6 @@ CREATE TABLE "miis" ( "name" VARCHAR(64) NOT NULL, "pictures" TEXT[], "tags" TEXT[], - "likes" INTEGER NOT NULL DEFAULT 0, "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, CONSTRAINT "miis_pkey" PRIMARY KEY ("id") diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 617e6c0..4dd66bf 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -66,7 +66,6 @@ model Mii { name String @db.VarChar(64) pictures String[] tags String[] - likes Int @default(0) createdAt DateTime @default(now()) diff --git a/src/app/api/like/route.ts b/src/app/api/like/route.ts index 47dbf95..88d2510 100644 --- a/src/app/api/like/route.ts +++ b/src/app/api/like/route.ts @@ -21,7 +21,7 @@ export async function PATCH(request: Request) { }); if (existingLike) { - // Delete the like if it exists + // Remove the like if it exists await tx.like.delete({ where: { userId_miiId: { @@ -30,31 +30,21 @@ export async function PATCH(request: Request) { }, }, }); - - const updatedMii = await tx.mii.update({ - where: { id: miiId }, - data: { likes: { decrement: 1 } }, - select: { likes: true }, - }); - - return { liked: false, count: updatedMii.likes }; } else { - // Create a new like if it doesn't exist + // Add a like if it doesn't exist await tx.like.create({ data: { userId: Number(session.user.id), miiId, }, }); - - const updatedMii = await tx.mii.update({ - where: { id: miiId }, - data: { likes: { increment: 1 } }, - select: { likes: true }, - }); - - return { liked: true, count: updatedMii.likes }; } + + const likeCount = await tx.like.count({ + where: { miiId }, + }); + + return { liked: !existingLike, count: likeCount }; }); return Response.json({ success: true, liked: result.liked, count: result.count }); diff --git a/src/app/components/mii-list.tsx b/src/app/components/mii-list.tsx index 634d2c2..e8a40c9 100644 --- a/src/app/components/mii-list.tsx +++ b/src/app/components/mii-list.tsx @@ -75,11 +75,15 @@ export default async function MiiList({ searchParams, userId }: Props) { userId: true, }, }, + _count: { + select: { likedBy: true }, + }, }, }); const formattedMiis = miis.map((mii) => ({ ...mii, + likes: mii._count.likedBy, isLikedByUser: mii.likedBy.length > 0, // True if the user has liked the Mii })); diff --git a/src/app/mii/[slug]/page.tsx b/src/app/mii/[slug]/page.tsx index 8515485..07b6e47 100644 --- a/src/app/mii/[slug]/page.tsx +++ b/src/app/mii/[slug]/page.tsx @@ -10,7 +10,7 @@ interface Props { params: Promise<{ slug: string }>; } -export default async function ProfilePage({ params }: Props) { +export default async function MiiPage({ params }: Props) { const { slug } = await params; const session = await auth(); @@ -25,14 +25,14 @@ export default async function ProfilePage({ params }: Props) { username: true, }, }, - }, - }); - - const isLiked = await prisma.like.findUnique({ - where: { - userId_miiId: { - userId: Number(session?.user.id), - miiId: Number(slug), + likedBy: { + where: { + userId: Number(session?.user.id), + }, + select: { userId: true }, + }, + _count: { + select: { likedBy: true }, // Get total like count }, }, }); @@ -61,7 +61,13 @@ export default async function ProfilePage({ params }: Props) {
- + 0} + isLoggedIn={session?.user != null} + big + />