Skip to content

Commit

Permalink
feat(be): create post router api
Browse files Browse the repository at this point in the history
  • Loading branch information
acatzk committed Jan 26, 2024
1 parent b59b761 commit 0bcf337
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
4 changes: 3 additions & 1 deletion server/api/root.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { createTRPCRouter } from './trpc'
import { userRouter } from './routers/user'
import { postRouter } from './routers/post'

export const appRouter = createTRPCRouter({
user: userRouter
user: userRouter,
post: postRouter
})

export type AppRouter = typeof appRouter
30 changes: 30 additions & 0 deletions server/api/routers/post.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { PostSchema } from '~/zod/schema'

import { protectedProcedure, createTRPCRouter } from './../trpc'

export const postRouter = createTRPCRouter({
create: protectedProcedure.input(PostSchema).mutation(async ({ input, ctx }) => {
return await ctx.db.post.create({
data: {
mediaFiles: {
createMany: {
data: input.mediaFiles
}
},
isHideLikeAndCount: input.isHideLikeAndCount,
isTurnOffComment: input.isTurnOffComment,
title: input.captions,
user: {
connect: {
id: parseInt(ctx.auth.userId)
}
}
},
include: {
user: true,
postHashtags: true,
mediaFiles: true
}
})
})
})
4 changes: 3 additions & 1 deletion zod/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ export const PostSchema = z.object({
})
),
captions: z.string().max(200).optional(),
location: z.string().optional()
location: z.string().optional(),
isHideLikeAndCount: z.boolean().default(false),
isTurnOffComment: z.boolean().default(false)
})

export type PostSchemaType = z.infer<typeof PostSchema>

0 comments on commit 0bcf337

Please sign in to comment.