doctor-mail/apps/server/src/models/resource/resource.router.ts

119 lines
4.0 KiB
TypeScript
Raw Normal View History

2024-12-31 15:57:32 +08:00
import { Injectable } from '@nestjs/common';
import { TrpcService } from '@server/trpc/trpc.service';
2025-01-06 08:45:23 +08:00
import { Prisma, UpdateOrderSchema } from '@nice/common';
2024-12-31 15:57:32 +08:00
import { ResourceService } from './resource.service';
import { z, ZodType } from 'zod';
2025-01-26 18:24:16 +08:00
const ResourceCreateArgsSchema: ZodType<Prisma.ResourceCreateArgs> = z.any();
const ResourceCreateManyInputSchema: ZodType<Prisma.ResourceCreateManyInput> =
z.any();
const ResourceDeleteManyArgsSchema: ZodType<Prisma.ResourceDeleteManyArgs> =
z.any();
const ResourceFindManyArgsSchema: ZodType<Prisma.ResourceFindManyArgs> =
z.any();
const ResourceFindFirstArgsSchema: ZodType<Prisma.ResourceFindFirstArgs> =
z.any();
const ResourceWhereInputSchema: ZodType<Prisma.ResourceWhereInput> = z.any();
const ResourceSelectSchema: ZodType<Prisma.ResourceSelect> = z.any();
2024-12-31 15:57:32 +08:00
@Injectable()
export class ResourceRouter {
2025-01-26 18:24:16 +08:00
constructor(
private readonly trpc: TrpcService,
private readonly resourceService: ResourceService,
) {}
router = this.trpc.router({
create: this.trpc.protectProcedure
2025-05-08 11:44:48 +08:00
.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(),
}),
}),
)
2025-01-26 18:24:16 +08:00
.mutation(async ({ ctx, input }) => {
const { staff } = ctx;
2025-05-08 11:44:48 +08:00
console.log('资源创建数据:', input.data);
2025-01-26 18:24:16 +08:00
return await this.resourceService.create(input, { staff });
}),
2025-05-08 11:44:48 +08:00
2025-01-26 18:24:16 +08:00
createMany: this.trpc.protectProcedure
.input(z.array(ResourceCreateManyInputSchema))
.mutation(async ({ ctx, input }) => {
const { staff } = ctx;
2024-12-31 15:57:32 +08:00
2025-01-26 18:24:16 +08:00
return await this.resourceService.createMany({ data: input }, staff);
}),
2025-05-01 19:11:49 +08:00
deleteMany: this.trpc.protectProcedure
2025-01-26 18:24:16 +08:00
.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
2025-05-08 11:44:48 +08:00
.input(
z.object({
ids: z.array(z.string()),
}),
)
2025-05-01 19:11:49 +08:00
.mutation(async ({ input, ctx }) => {
const result = await this.resourceService.softDeleteByIds(input.ids);
return result;
2025-01-26 18:24:16 +08:00
}),
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);
}),
2025-05-01 19:11:49 +08:00
findManyWithCursor: this.trpc.procedure
2025-01-26 18:24:16 +08:00
.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);
}),
2025-05-08 11:44:48 +08:00
count: this.trpc.procedure
2025-05-01 19:11:49 +08:00
.input(
z.object({
2025-05-08 11:44:48 +08:00
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(),
2025-05-01 19:11:49 +08:00
}),
)
.query(async ({ input }) => {
return await this.resourceService.count(input);
}),
2025-01-26 18:24:16 +08:00
});
2024-12-31 15:57:32 +08:00
}