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

91 lines
3.2 KiB
TypeScript
Raw Normal View History

2024-12-30 08:26:40 +08:00
import { Injectable } from '@nestjs/common';
import { TrpcService } from '@server/trpc/trpc.service';
2025-01-06 08:45:23 +08:00
import { Prisma } from '@nice/common';
2024-12-30 08:26:40 +08:00
import { PostService } from './post.service';
import { z, ZodType } from 'zod';
2025-01-22 18:56:27 +08:00
import { PostMeta } from '@nice/common';
import { getClientIp } from './utils';
2024-12-30 08:26:40 +08:00
const PostCreateArgsSchema: ZodType<Prisma.PostCreateArgs> = z.any();
const PostUpdateArgsSchema: ZodType<Prisma.PostUpdateArgs> = z.any();
const PostFindFirstArgsSchema: ZodType<Prisma.PostFindFirstArgs> = z.any();
const PostDeleteManyArgsSchema: ZodType<Prisma.PostDeleteManyArgs> = z.any();
const PostWhereInputSchema: ZodType<Prisma.PostWhereInput> = z.any();
const PostSelectSchema: ZodType<Prisma.PostSelect> = z.any();
const PostUpdateInputSchema: ZodType<Prisma.PostUpdateInput> = z.any();
@Injectable()
export class PostRouter {
constructor(
private readonly trpc: TrpcService,
private readonly postService: PostService,
2025-01-22 18:56:27 +08:00
) {}
2024-12-30 08:26:40 +08:00
router = this.trpc.router({
create: this.trpc.protectProcedure
.input(PostCreateArgsSchema)
.mutation(async ({ ctx, input }) => {
2025-01-22 18:56:27 +08:00
const { staff, req } = ctx;
// 从请求中获取 IP
const ip = getClientIp(req);
const currentMeta =
typeof input.data.meta === 'object' && input.data.meta !== null
? input.data.meta
: {};
input.data.meta = {
...currentMeta,
ip: ip || '',
} as Prisma.InputJsonObject; // 明确指定类型
2024-12-30 08:26:40 +08:00
return await this.postService.create(input, { staff });
}),
2025-01-22 19:25:55 +08:00
2024-12-30 08:26:40 +08:00
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.protectProcedure
.input(z.object({ id: z.string(), args: PostFindFirstArgsSchema }))
.query(async ({ ctx, input }) => {
const { staff } = ctx;
return await this.postService.findById(input.id, input.args);
}),
deleteMany: this.trpc.protectProcedure
.input(PostDeleteManyArgsSchema)
.mutation(async ({ input }) => {
return await this.postService.deleteMany(input);
}),
findManyWithCursor: this.trpc.protectProcedure
.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);
}),
});
}