You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
36 lines
1.2 KiB
36 lines
1.2 KiB
import { z } from "zod";
|
|
import { validate } from "#server/utils/validation";
|
|
import { createArticle, ArticleStatuses } from "../../service/article";
|
|
import { requireUser, delCache } from "#server/utils/context";
|
|
|
|
const createSchema = z.object({
|
|
title: z.string().min(1, "标题不能为空").max(255, "标题不能超过 255 字"),
|
|
content: z.string().min(1, "内容不能为空"),
|
|
summary: z.string().max(500, "摘要不能超过 500 字").nullable().optional(),
|
|
cover: z.string().max(500).nullable().optional(),
|
|
status: z.enum(ArticleStatuses).optional(),
|
|
cardIds: z
|
|
.array(
|
|
z.object({
|
|
cardId: z.number().int().positive("无效的卡片 ID"),
|
|
sortOrder: z.number().int().optional(),
|
|
}),
|
|
)
|
|
.nullable()
|
|
.optional(),
|
|
});
|
|
|
|
export default defineWrappedResponseHandler(async (event) => {
|
|
await requireUser(event);
|
|
const body = await readBody(event);
|
|
const data = validate(createSchema, body);
|
|
|
|
const article = await createArticle(data);
|
|
|
|
// Clear card caches so detail views reflect new article association
|
|
if (article.cards.length > 0) {
|
|
await Promise.all(article.cards.map((c) => delCache(`card:${c.id}`)));
|
|
}
|
|
|
|
return R.success(article);
|
|
});
|
|
|