fix: remove likes property in mii model
This commit is contained in:
parent
2fdf120280
commit
8f731dd358
5 changed files with 28 additions and 30 deletions
|
|
@ -47,7 +47,6 @@ CREATE TABLE "miis" (
|
||||||
"name" VARCHAR(64) NOT NULL,
|
"name" VARCHAR(64) NOT NULL,
|
||||||
"pictures" TEXT[],
|
"pictures" TEXT[],
|
||||||
"tags" TEXT[],
|
"tags" TEXT[],
|
||||||
"likes" INTEGER NOT NULL DEFAULT 0,
|
|
||||||
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
|
||||||
CONSTRAINT "miis_pkey" PRIMARY KEY ("id")
|
CONSTRAINT "miis_pkey" PRIMARY KEY ("id")
|
||||||
|
|
@ -66,7 +66,6 @@ model Mii {
|
||||||
name String @db.VarChar(64)
|
name String @db.VarChar(64)
|
||||||
pictures String[]
|
pictures String[]
|
||||||
tags String[]
|
tags String[]
|
||||||
likes Int @default(0)
|
|
||||||
|
|
||||||
createdAt DateTime @default(now())
|
createdAt DateTime @default(now())
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -21,7 +21,7 @@ export async function PATCH(request: Request) {
|
||||||
});
|
});
|
||||||
|
|
||||||
if (existingLike) {
|
if (existingLike) {
|
||||||
// Delete the like if it exists
|
// Remove the like if it exists
|
||||||
await tx.like.delete({
|
await tx.like.delete({
|
||||||
where: {
|
where: {
|
||||||
userId_miiId: {
|
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 {
|
} else {
|
||||||
// Create a new like if it doesn't exist
|
// Add a like if it doesn't exist
|
||||||
await tx.like.create({
|
await tx.like.create({
|
||||||
data: {
|
data: {
|
||||||
userId: Number(session.user.id),
|
userId: Number(session.user.id),
|
||||||
miiId,
|
miiId,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
const updatedMii = await tx.mii.update({
|
const likeCount = await tx.like.count({
|
||||||
where: { id: miiId },
|
where: { miiId },
|
||||||
data: { likes: { increment: 1 } },
|
|
||||||
select: { likes: true },
|
|
||||||
});
|
});
|
||||||
|
|
||||||
return { liked: true, count: updatedMii.likes };
|
return { liked: !existingLike, count: likeCount };
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
return Response.json({ success: true, liked: result.liked, count: result.count });
|
return Response.json({ success: true, liked: result.liked, count: result.count });
|
||||||
|
|
|
||||||
|
|
@ -75,11 +75,15 @@ export default async function MiiList({ searchParams, userId }: Props) {
|
||||||
userId: true,
|
userId: true,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
_count: {
|
||||||
|
select: { likedBy: true },
|
||||||
|
},
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
const formattedMiis = miis.map((mii) => ({
|
const formattedMiis = miis.map((mii) => ({
|
||||||
...mii,
|
...mii,
|
||||||
|
likes: mii._count.likedBy,
|
||||||
isLikedByUser: mii.likedBy.length > 0, // True if the user has liked the Mii
|
isLikedByUser: mii.likedBy.length > 0, // True if the user has liked the Mii
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,7 @@ interface Props {
|
||||||
params: Promise<{ slug: string }>;
|
params: Promise<{ slug: string }>;
|
||||||
}
|
}
|
||||||
|
|
||||||
export default async function ProfilePage({ params }: Props) {
|
export default async function MiiPage({ params }: Props) {
|
||||||
const { slug } = await params;
|
const { slug } = await params;
|
||||||
const session = await auth();
|
const session = await auth();
|
||||||
|
|
||||||
|
|
@ -25,14 +25,14 @@ export default async function ProfilePage({ params }: Props) {
|
||||||
username: true,
|
username: true,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
likedBy: {
|
||||||
});
|
|
||||||
|
|
||||||
const isLiked = await prisma.like.findUnique({
|
|
||||||
where: {
|
where: {
|
||||||
userId_miiId: {
|
|
||||||
userId: Number(session?.user.id),
|
userId: Number(session?.user.id),
|
||||||
miiId: Number(slug),
|
},
|
||||||
|
select: { userId: true },
|
||||||
|
},
|
||||||
|
_count: {
|
||||||
|
select: { likedBy: true }, // Get total like count
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
@ -61,7 +61,13 @@ export default async function ProfilePage({ params }: Props) {
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div className="mt-auto">
|
<div className="mt-auto">
|
||||||
<LikeButton likes={mii?.likes ?? 0} miiId={mii?.id} isLiked={isLiked != null} isLoggedIn={session?.user != null} big />
|
<LikeButton
|
||||||
|
likes={mii?._count.likedBy ?? 0}
|
||||||
|
miiId={mii?.id}
|
||||||
|
isLiked={(mii?.likedBy ?? []).length > 0}
|
||||||
|
isLoggedIn={session?.user != null}
|
||||||
|
big
|
||||||
|
/>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue