import { type CreatePostInput, createPostInputSchema, type UpdatePostInput, updatePostInputSchema, } from "@cms/content" import { type CrudAuditHook, type CrudMutationContext, createCrudService } from "@cms/crud" import type { Post } from "../prisma/generated/client/client" import { db } from "./client" const postRepository = { list: () => db.post.findMany({ orderBy: { updatedAt: "desc", }, }), findById: (id: string) => db.post.findUnique({ where: { id }, }), create: (input: CreatePostInput) => db.post.create({ data: input, }), update: (id: string, input: UpdatePostInput) => db.post.update({ where: { id }, data: input, }), delete: (id: string) => db.post.delete({ where: { id }, }), } const postAuditHooks: Array> = [] const postCrudService = createCrudService({ resource: "post", repository: postRepository, schemas: { create: createPostInputSchema, update: updatePostInputSchema, }, auditHooks: postAuditHooks, }) export function registerPostCrudAuditHook(hook: CrudAuditHook): () => void { postAuditHooks.push(hook) return () => { const index = postAuditHooks.indexOf(hook) if (index >= 0) { postAuditHooks.splice(index, 1) } } } export async function listPosts() { return postCrudService.list() } export async function getPostById(id: string) { return postCrudService.getById(id) } export async function createPost(input: unknown, context?: CrudMutationContext) { return postCrudService.create(input, context) } export async function updatePost(id: string, input: unknown, context?: CrudMutationContext) { return postCrudService.update(id, input, context) } export async function deletePost(id: string, context?: CrudMutationContext) { return postCrudService.delete(id, context) }