From 0bcf33785420351b3c8fb05d5aff9de610902ac2 Mon Sep 17 00:00:00 2001 From: acatzk Date: Fri, 26 Jan 2024 17:14:25 +0800 Subject: [PATCH] feat(be): create post router api --- server/api/root.ts | 4 +++- server/api/routers/post.ts | 30 ++++++++++++++++++++++++++++++ zod/schema.ts | 4 +++- 3 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 server/api/routers/post.ts diff --git a/server/api/root.ts b/server/api/root.ts index c032568..4965237 100644 --- a/server/api/root.ts +++ b/server/api/root.ts @@ -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 diff --git a/server/api/routers/post.ts b/server/api/routers/post.ts new file mode 100644 index 0000000..11cd521 --- /dev/null +++ b/server/api/routers/post.ts @@ -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 + } + }) + }) +}) diff --git a/zod/schema.ts b/zod/schema.ts index 8c63ac1..90c6e66 100644 --- a/zod/schema.ts +++ b/zod/schema.ts @@ -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