feat: display all posts on home page

This commit is contained in:
Stevan Freeborn
2023-09-15 17:52:01 -05:00
parent 31dd29a9c2
commit 638ba3ce5c
6 changed files with 109 additions and 3 deletions
+43
View File
@@ -160,3 +160,46 @@ export const getUserPostCount = query({
return posts.length;
},
});
export const getAllPostsWithUser = query({
args: { paginationOpts: paginationOptsValidator },
handler: async (ctx, args) => {
const posts = await ctx.db
.query('posts')
.filter(q => q.eq(q.field('parentPostId'), undefined))
.order('desc')
.paginate(args.paginationOpts);
const postsWithUser = await Promise.all(
posts.page.map(async post => {
const user = await ctx.db.get(post.userId);
if (user === null) {
return {
...post,
user: {
_id: '' as Id<'users'>,
_creationTime: 0,
clerkUsername: null,
clerkImageUrl: '',
clerkUserId: '',
},
};
}
return {
...post,
user: {
_id: user._id,
_creationTime: user._creationTime,
clerkUsername: user.clerkUser.username,
clerkImageUrl: user.clerkUser.image_url,
clerkUserId: user.clerkUser.id,
},
};
})
);
return { ...posts, page: postsWithUser };
},
});