51 lines
1.0 KiB
TypeScript
51 lines
1.0 KiB
TypeScript
![]() |
import { db, VisitType } from '@nice/common';
|
||
|
export async function updatePostViewCount(id: string, type: VisitType) {
|
||
|
const totalViews = await db.visit.aggregate({
|
||
|
_sum: {
|
||
|
views: true,
|
||
|
},
|
||
|
where: {
|
||
|
postId: id,
|
||
|
type: type,
|
||
|
},
|
||
|
});
|
||
|
if (type === VisitType.READED) {
|
||
|
await db.post.update({
|
||
|
where: {
|
||
|
id,
|
||
|
},
|
||
|
data: {
|
||
|
views: totalViews._sum.views || 0, // Use 0 if no visits exist
|
||
|
},
|
||
|
});
|
||
|
} else if (type === VisitType.LIKE) {
|
||
|
await db.post.update({
|
||
|
where: {
|
||
|
id,
|
||
|
},
|
||
|
data: {
|
||
|
likes: totalViews._sum.views || 0, // Use 0 if no visits exist
|
||
|
},
|
||
|
});
|
||
|
}
|
||
|
}
|
||
|
export async function updatePostLikeCount(id: string) {
|
||
|
const totalViews = await db.visit.aggregate({
|
||
|
_sum: {
|
||
|
views: true,
|
||
|
},
|
||
|
where: {
|
||
|
postId: id,
|
||
|
type: VisitType.LIKE,
|
||
|
},
|
||
|
});
|
||
|
await db.post.update({
|
||
|
where: {
|
||
|
id,
|
||
|
},
|
||
|
data: {
|
||
|
views: totalViews._sum.views || 0, // Use 0 if no visits exist
|
||
|
},
|
||
|
});
|
||
|
}
|