feat: add individual post page

This commit is contained in:
Stevan Freeborn
2023-09-11 22:05:12 -05:00
parent 41401cd3c5
commit 5389a86ed1
5 changed files with 85 additions and 10 deletions
+22 -2
View File
@@ -1,5 +1,6 @@
import { paginationOptsValidator } from 'convex/server';
import { v } from 'convex/values';
import { PostWithUserDto } from '../src/app/types';
import { mutation, query } from './_generated/server';
import { userQuery } from './users';
@@ -59,13 +60,32 @@ export const deletePostById = mutation({
export const getPostById = query({
args: { id: v.id('posts') },
handler: async (ctx, args) => {
handler: async (
ctx,
args
): Promise<
PostWithUserDto | 'POST_NOT_FOUND' | 'USER_FOR_POST_NOT_FOUND'
> => {
const post = await ctx.db.get(args.id);
if (post === null) {
return 'POST_NOT_FOUND';
}
return post;
const user = await ctx.db.get(post.userId);
if (user === null) {
return 'USER_FOR_POST_NOT_FOUND';
}
return {
...post,
user: {
_id: user._id,
_creationTime: user._creationTime,
clerkUsername: user.clerkUser.username,
clerkImageUrl: user.clerkUser.image_url,
},
};
},
});
+18
View File
@@ -32,6 +32,24 @@ export const getUserByClerkId = query({
},
});
export const getUserById = query({
args: { id: v.id('users') },
async handler(ctx, args): Promise<UserDto | 'USER_NOT_FOUND'> {
const user = await ctx.db.get(args.id);
if (user === null) {
return 'USER_NOT_FOUND';
}
return {
_id: user._id,
_creationTime: user._creationTime,
clerkUsername: user.clerkUser.username,
clerkImageUrl: user.clerkUser.image_url,
};
},
});
export const getUser = internalQuery({
args: { subject: v.string() },
async handler(ctx, args) {