Skip to content

Commit

Permalink
fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
SupertigerDev committed Oct 27, 2024
1 parent 9a9dc43 commit 36c88ff
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
26 changes: 20 additions & 6 deletions src/routes/posts/postsDiscover.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ interface Query {
afterId?: string;
beforeId?: string;
sort?: string;
skip?: string;
}

const SortOptions = ['mostLiked7Days', 'mostLiked30days', 'mostLikedAllTime'];
Expand All @@ -36,6 +37,7 @@ async function route(req: Request, res: Response) {
limit = undefined;
}

const skip = query.skip ? parseInt(query.skip) : 0;
const sort = SortOptions.includes(query.sort!) ? query.sort : undefined;

let afterDate: Date | undefined = undefined;
Expand All @@ -55,14 +57,26 @@ async function route(req: Request, res: Response) {
bypassBlocked: isAdmin,
requesterUserId: req.userCache.id,
limit,
afterId: query.afterId,
beforeId: query.beforeId,

...(!sort
? {
afterId: query.afterId,
beforeId: query.beforeId,
}
: {}),

...(sort
? {
orderBy: {
estimateLikes: query.afterId || query.beforeId ? 'asc' : 'desc',
},
...(query.afterId
? {
skip: 1,
cursor: {
id: query.afterId,
},
}
: {}),

orderBy: [{ estimateLikes: 'desc' }, { createdAt: 'asc' }],
...(afterDate
? {
where: {
Expand All @@ -75,5 +89,5 @@ async function route(req: Request, res: Response) {
}
: {}),
});
res.json(posts);
res.json(posts.map((e) => ({ content: e.content, id: e.id, likes: e.estimateLikes })));
}
10 changes: 5 additions & 5 deletions src/services/Post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,9 @@ interface FetchPostsOpts {

where?: Prisma.PostWhereInput;
additionalInclude?: Prisma.PostInclude;
orderBy?: Prisma.PostOrderByWithRelationInput;
doNotReverse?: boolean;
orderBy?: Prisma.PostOrderByWithRelationInput | Prisma.PostOrderByWithRelationInput[];
skip?: number;
cursor?: Prisma.PostWhereUniqueInput;
}

export async function fetchPosts(opts: FetchPostsOpts) {
Expand Down Expand Up @@ -203,15 +204,14 @@ export async function fetchPosts(opts: FetchPostsOpts) {

const posts = await prisma.post.findMany({
where,
skip: opts.skip,
orderBy: opts.orderBy || { createdAt: 'desc' },
take: opts.limit ? (opts.limit > 30 ? 30 : opts.limit) : 30,
cursor: opts.cursor,
include: { ...constructInclude(opts.requesterUserId), ...opts.additionalInclude },
});
updateViews(posts);

if (opts.doNotReverse) {
return posts;
}
return posts.reverse();
}
async function updateViews(posts: Post[]) {
Expand Down

0 comments on commit 36c88ff

Please sign in to comment.