import { Injectable } from '@nestjs/common'; import { TrpcService } from '@server/trpc/trpc.service'; import { Prisma, UpdateOrderSchema } from '@nice/common'; import { ResourceService } from './resource.service'; import { z, ZodType } from 'zod'; const ResourceCreateArgsSchema: ZodType = z.any(); const ResourceCreateManyInputSchema: ZodType = z.any(); const ResourceDeleteManyArgsSchema: ZodType = z.any(); const ResourceFindManyArgsSchema: ZodType = z.any(); const ResourceFindFirstArgsSchema: ZodType = z.any(); const ResourceWhereInputSchema: ZodType = z.any(); const ResourceSelectSchema: ZodType = z.any(); @Injectable() export class ResourceRouter { constructor( private readonly trpc: TrpcService, private readonly resourceService: ResourceService, ) {} router = this.trpc.router({ create: this.trpc.protectProcedure .input( z.object({ data: z.object({ fileId: z.string(), isPublic: z.boolean().optional(), title: z.string().optional(), description: z.string().optional(), type: z.string().optional(), }), }), ) .mutation(async ({ ctx, input }) => { const { staff } = ctx; console.log('资源创建数据:', input.data); return await this.resourceService.create(input, { staff }); }), createMany: this.trpc.protectProcedure .input(z.array(ResourceCreateManyInputSchema)) .mutation(async ({ ctx, input }) => { const { staff } = ctx; return await this.resourceService.createMany({ data: input }, staff); }), deleteMany: this.trpc.protectProcedure .input(ResourceDeleteManyArgsSchema) .mutation(async ({ input }) => { return await this.resourceService.deleteMany(input); }), findFirst: this.trpc.procedure .input(ResourceFindFirstArgsSchema) // Assuming StaffMethodSchema.findMany is the Zod schema for finding staffs by keyword .query(async ({ input }) => { return await this.resourceService.findFirst(input); }), softDeleteByIds: this.trpc.protectProcedure .input( z.object({ ids: z.array(z.string()), }), ) .mutation(async ({ input, ctx }) => { const result = await this.resourceService.softDeleteByIds(input.ids); return result; }), updateOrder: this.trpc.protectProcedure .input(UpdateOrderSchema) .mutation(async ({ input }) => { return this.resourceService.updateOrder(input); }), findMany: this.trpc.procedure .input(ResourceFindManyArgsSchema) // Assuming StaffMethodSchema.findMany is the Zod schema for finding staffs by keyword .query(async ({ input }) => { return await this.resourceService.findMany(input); }), findManyWithCursor: this.trpc.procedure .input( z.object({ cursor: z.any().nullish(), take: z.number().nullish(), where: ResourceWhereInputSchema.nullish(), select: ResourceSelectSchema.nullish(), }), ) .query(async ({ ctx, input }) => { const { staff } = ctx; return await this.resourceService.findManyWithCursor(input); }), count: this.trpc.procedure .input( z.object({ where: z .object({ AND: z .object({ title: z.object({ not: z.null(), }), description: z.object({ not: z.null(), }), }) .optional(), deletedAt: z.date().nullable().optional(), }) .optional(), }), ) .query(async ({ input }) => { return await this.resourceService.count(input); }), }); }