mirror of
https://github.com/trafficlunar/tomodachi-share.git
synced 2026-06-28 06:34:15 +00:00
fix: better like count
This commit is contained in:
parent
c951c7d755
commit
583d223ed9
6 changed files with 25 additions and 19 deletions
|
|
@ -29,7 +29,6 @@ export async function POST(request: NextRequest, { params }: { params: Promise<{
|
|||
});
|
||||
|
||||
if (existingLike) {
|
||||
// Remove the like if it exists
|
||||
await tx.like.delete({
|
||||
where: {
|
||||
userId_miiId: {
|
||||
|
|
@ -38,22 +37,25 @@ export async function POST(request: NextRequest, { params }: { params: Promise<{
|
|||
},
|
||||
},
|
||||
});
|
||||
await tx.mii.update({
|
||||
where: { id: miiId },
|
||||
data: { likeCount: { decrement: 1 } },
|
||||
});
|
||||
} else {
|
||||
// Add a like if it doesn't exist
|
||||
await tx.like.create({
|
||||
data: {
|
||||
userId: Number(session.user?.id),
|
||||
miiId,
|
||||
},
|
||||
});
|
||||
await tx.mii.update({
|
||||
where: { id: miiId },
|
||||
data: { likeCount: { increment: 1 } },
|
||||
});
|
||||
}
|
||||
|
||||
const likeCount = await tx.like.count({
|
||||
where: { miiId },
|
||||
});
|
||||
|
||||
return { liked: !existingLike, count: likeCount };
|
||||
return { liked: !existingLike };
|
||||
});
|
||||
|
||||
return rateLimit.sendResponse({ success: true, liked: result.liked, count: result.count });
|
||||
return rateLimit.sendResponse({ success: true, liked: result.liked });
|
||||
}
|
||||
|
|
|
|||
|
|
@ -78,6 +78,7 @@ export async function GET(request: NextRequest) {
|
|||
allowedCopying: true,
|
||||
quarantined: true,
|
||||
in_queue: true,
|
||||
likeCount: true,
|
||||
// Mii liked check
|
||||
...(session?.user?.id && {
|
||||
likedBy: {
|
||||
|
|
@ -85,10 +86,6 @@ export async function GET(request: NextRequest) {
|
|||
select: { userId: true },
|
||||
},
|
||||
}),
|
||||
// Like count
|
||||
_count: {
|
||||
select: { likedBy: true },
|
||||
},
|
||||
// Admin
|
||||
...(parentPage === "admin" && {
|
||||
description: true,
|
||||
|
|
@ -102,7 +99,7 @@ export async function GET(request: NextRequest) {
|
|||
let orderBy: Prisma.MiiOrderByWithRelationInput[];
|
||||
|
||||
if (sort === "likes") {
|
||||
orderBy = [{ likedBy: { _count: "desc" } }, { name: "asc" }];
|
||||
orderBy = [{ likeCount: "desc" }, { name: "asc" }];
|
||||
} else if (sort === "oldest") {
|
||||
orderBy = [{ createdAt: "asc" }, { name: "asc" }];
|
||||
} else {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue