book_manage/apps/server/src/models/post/post.router.ts

130 lines
4.7 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-02-06 16:32:52 +08:00
import { CourseMethodSchema, Prisma } from '@nice/common';
2024-12-30 08:26:40 +08:00
import { PostService } from './post.service';
import { z, ZodType } from 'zod';
2025-02-24 19:33:18 +08:00
import { UpdateOrderArgs } from '../base/base.type';
2024-12-30 08:26:40 +08:00
const PostCreateArgsSchema: ZodType<Prisma.PostCreateArgs> = z.any();
const PostUpdateArgsSchema: ZodType<Prisma.PostUpdateArgs> = z.any();
2025-02-24 19:33:18 +08:00
const PostUpdateOrderArgsSchema: ZodType<UpdateOrderArgs> = z.any();
2024-12-30 08:26:40 +08:00
const PostFindFirstArgsSchema: ZodType<Prisma.PostFindFirstArgs> = z.any();
2025-02-06 16:32:52 +08:00
const PostFindManyArgsSchema: ZodType<Prisma.PostFindManyArgs> = z.any();
2024-12-30 08:26:40 +08:00
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-02-06 16:32:52 +08:00
) {}
2024-12-30 08:26:40 +08:00
router = this.trpc.router({
create: this.trpc.protectProcedure
.input(PostCreateArgsSchema)
.mutation(async ({ ctx, input }) => {
const { staff } = ctx;
return await this.postService.create(input, { staff });
}),
2025-02-06 16:32:52 +08:00
createCourse: this.trpc.protectProcedure
.input(CourseMethodSchema.createCourse)
.mutation(async ({ ctx, input }) => {
const { staff } = ctx;
return await this.postService.createCourse(input, { staff });
}),
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);
}),
2025-02-24 21:23:48 +08:00
findById: this.trpc.procedure
2024-12-30 08:26:40 +08:00
.input(z.object({ id: z.string(), args: PostFindFirstArgsSchema }))
.query(async ({ ctx, input }) => {
const { staff } = ctx;
return await this.postService.findById(input.id, input.args);
}),
2025-02-24 21:23:48 +08:00
findMany: this.trpc.procedure
2025-02-06 16:32:52 +08:00
.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);
}),
2024-12-30 08:26:40 +08:00
deleteMany: this.trpc.protectProcedure
.input(PostDeleteManyArgsSchema)
.mutation(async ({ input }) => {
return await this.postService.deleteMany(input);
}),
2025-02-24 21:23:48 +08:00
findManyWithCursor: this.trpc.procedure
2024-12-30 08:26:40 +08:00
.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);
}),
2025-02-06 16:32:52 +08:00
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);
}),
2025-02-24 19:33:18 +08:00
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);
}),
2024-12-30 08:26:40 +08:00
});
}