import { Injectable } from '@nestjs/common'; import { TrpcService } from '@server/trpc/trpc.service'; import { CourseMethodSchema, Prisma } from '@nice/common'; import { PostService } from './post.service'; import { z, ZodType } from 'zod'; import { UpdateOrderArgs } from '../base/base.type'; const PostCreateArgsSchema: ZodType = z.any(); const PostUpdateArgsSchema: ZodType = z.any(); const PostUpdateOrderArgsSchema: ZodType = z.any(); const PostFindFirstArgsSchema: ZodType = z.any(); const PostFindManyArgsSchema: ZodType = z.any(); const PostDeleteManyArgsSchema: ZodType = z.any(); const PostWhereInputSchema: ZodType = z.any(); const PostSelectSchema: ZodType = z.any(); const PostUpdateInputSchema: ZodType = z.any(); @Injectable() export class PostRouter { constructor( private readonly trpc: TrpcService, private readonly postService: PostService, ) {} router = this.trpc.router({ create: this.trpc.protectProcedure .input(PostCreateArgsSchema) .mutation(async ({ ctx, input }) => { const { staff } = ctx; return await this.postService.create(input, { staff }); }), createCourse: this.trpc.protectProcedure .input(CourseMethodSchema.createCourse) .mutation(async ({ ctx, input }) => { const { staff } = ctx; return await this.postService.createCourse(input, { staff }); }), softDeleteByIds: this.trpc.protectProcedure .input( z.object({ ids: z.array(z.string()), data: PostUpdateInputSchema.nullish(), }), ) .mutation(async ({ input }) => { return await this.postService.softDeleteByIds(input.ids, input.data); }), restoreByIds: this.trpc.protectProcedure .input( z.object({ ids: z.array(z.string()), args: PostUpdateInputSchema.nullish(), }), ) .mutation(async ({ input }) => { return await this.postService.restoreByIds(input.ids, input.args); }), update: this.trpc.protectProcedure .input(PostUpdateArgsSchema) .mutation(async ({ ctx, input }) => { const { staff } = ctx; return await this.postService.update(input, staff); }), findById: this.trpc.procedure .input(z.object({ id: z.string(), args: PostFindFirstArgsSchema })) .query(async ({ ctx, input }) => { const { staff } = ctx; return await this.postService.findById(input.id, input.args); }), findMany: this.trpc.procedure .input(PostFindManyArgsSchema) .query(async ({ ctx, input }) => { const { staff } = ctx; return await this.postService.findMany(input); }), findFirst: this.trpc.procedure .input(PostFindFirstArgsSchema) // Assuming StaffMethodSchema.findMany is the Zod schema for finding staffs by keyword .query(async ({ input, ctx }) => { const { staff, ip } = ctx; // 从请求中获取 IP return await this.postService.findFirst(input, staff, ip); }), deleteMany: this.trpc.protectProcedure .input(PostDeleteManyArgsSchema) .mutation(async ({ input }) => { return await this.postService.deleteMany(input); }), findManyWithCursor: this.trpc.procedure .input( z.object({ cursor: z.any().nullish(), take: z.number().nullish(), where: PostWhereInputSchema.nullish(), select: PostSelectSchema.nullish(), }), ) .query(async ({ ctx, input }) => { const { staff } = ctx; return await this.postService.findManyWithCursor(input, staff); }), findManyWithPagination: this.trpc.procedure .input( z.object({ page: z.number().optional(), pageSize: z.number().optional(), where: PostWhereInputSchema.optional(), select: PostSelectSchema.optional(), }), ) // Assuming StaffMethodSchema.findMany is the Zod schema for finding staffs by keyword .query(async ({ input }) => { return await this.postService.findManyWithPagination(input); }), updateOrder: this.trpc.protectProcedure .input(PostUpdateOrderArgsSchema) .mutation(async ({ ctx, input }) => { const { staff } = ctx; return await this.postService.updateOrder(input); }), updateOrderByIds: this.trpc.protectProcedure .input( z.object({ ids: z.array(z.string()), }), ) .mutation(async ({ ctx, input }) => { const { staff } = ctx; return await this.postService.updateOrderByIds(input.ids); }), }); }