feat(admin): add posts CRUD sandbox and shared CRUD foundation
This commit is contained in:
@@ -1,2 +1,9 @@
|
||||
export { db } from "./client"
|
||||
export { createPost, listPosts } from "./posts"
|
||||
export {
|
||||
createPost,
|
||||
deletePost,
|
||||
getPostById,
|
||||
listPosts,
|
||||
registerPostCrudAuditHook,
|
||||
updatePost,
|
||||
} from "./posts"
|
||||
|
||||
@@ -1,19 +1,80 @@
|
||||
import { upsertPostSchema } from "@cms/content"
|
||||
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<CrudAuditHook<Post>> = []
|
||||
|
||||
const postCrudService = createCrudService({
|
||||
resource: "post",
|
||||
repository: postRepository,
|
||||
schemas: {
|
||||
create: createPostInputSchema,
|
||||
update: updatePostInputSchema,
|
||||
},
|
||||
auditHooks: postAuditHooks,
|
||||
})
|
||||
|
||||
export function registerPostCrudAuditHook(hook: CrudAuditHook<Post>): () => void {
|
||||
postAuditHooks.push(hook)
|
||||
|
||||
return () => {
|
||||
const index = postAuditHooks.indexOf(hook)
|
||||
|
||||
if (index >= 0) {
|
||||
postAuditHooks.splice(index, 1)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export async function listPosts() {
|
||||
return db.post.findMany({
|
||||
orderBy: {
|
||||
updatedAt: "desc",
|
||||
},
|
||||
})
|
||||
return postCrudService.list()
|
||||
}
|
||||
|
||||
export async function createPost(input: unknown) {
|
||||
const payload = upsertPostSchema.parse(input)
|
||||
|
||||
return db.post.create({
|
||||
data: payload,
|
||||
})
|
||||
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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user