Files
d8d-admin-mobile-starter-pu…/server/routes_know_info.ts

228 lines
6.0 KiB
TypeScript

import { Hono } from "hono";
import debug from "debug";
import type {
KnowInfo,
} from "../client/share/types.ts";
import {
EnableStatus,
DeleteStatus,
} from "../client/share/types.ts";
import type { Variables, WithAuth } from "./middlewares.ts";
const log = {
api: debug("api:sys"),
};
// 创建知识库管理路由
export function createKnowInfoRoutes(withAuth: WithAuth) {
const knowInfoRoutes = new Hono<{ Variables: Variables }>();
// 获取知识库文章列表
knowInfoRoutes.get("/", withAuth, async (c) => {
try {
const apiClient = c.get('apiClient');
// 获取分页参数
const page = Number(c.req.query("page")) || 1;
const limit = Number(c.req.query("limit")) || 10;
const offset = (page - 1) * limit;
// 获取筛选参数
const title = c.req.query("title");
const category = c.req.query("category");
const tags = c.req.query("tags");
// 构建查询
let query = apiClient.database
.table("know_info")
.where("is_deleted", 0)
.orderBy("id", "desc");
// 应用筛选条件
if (title) {
query = query.where("title", "like", `%${title}%`);
}
if (category) {
query = query.where("category", category);
}
if (tags) {
query = query.where("tags", "like", `%${tags}%`);
}
// 克隆查询以获取总数
const countQuery = query.clone();
// 执行分页查询
const articles = await query.limit(limit).offset(offset);
// 获取总数
const count = await countQuery.count();
return c.json({
data: articles,
pagination: {
total: Number(count),
current: page,
pageSize: limit,
totalPages: Math.ceil(Number(count) / limit),
},
});
} catch (error) {
log.api("获取知识库文章列表失败:", error);
return c.json({ error: "获取知识库文章列表失败" }, 500);
}
});
// 获取单个知识库文章
knowInfoRoutes.get("/:id", withAuth, async (c) => {
try {
const id = Number(c.req.param("id"));
if (!id || isNaN(id)) {
return c.json({ error: "无效的文章ID" }, 400);
}
const apiClient = c.get('apiClient');
const [article] = await apiClient.database
.table("know_info")
.where({ id, is_deleted: 0 });
if (!article) {
return c.json({ error: "文章不存在" }, 404);
}
return c.json({message: '获取知识库文章详情成功', data: article});
} catch (error) {
log.api("获取知识库文章详情失败:", error);
return c.json({ error: "获取知识库文章详情失败" }, 500);
}
});
// 创建知识库文章
knowInfoRoutes.post("/", withAuth, async (c) => {
try {
const articleData = (await c.req.json()) as Partial<KnowInfo>;
// 验证必填字段
if (!articleData.title) {
return c.json({ error: "文章标题不能为空" }, 400);
}
// 如果作者为空,则使用当前用户的用户名
if (!articleData.author) {
const user = c.get("user");
articleData.author = user ? user.username : "unknown";
}
const apiClient = c.get('apiClient');
const [id] = await apiClient.database
.table("know_info")
.insert(articleData);
// 获取创建的文章
const [createdArticle] = await apiClient.database
.table("know_info")
.where("id", id);
return c.json({
message: "知识库文章创建成功",
data: createdArticle,
});
} catch (error) {
log.api("创建知识库文章失败:", error);
return c.json({ error: "创建知识库文章失败" }, 500);
}
});
// 更新知识库文章
knowInfoRoutes.put("/:id", withAuth, async (c) => {
try {
const id = Number(c.req.param("id"));
if (!id || isNaN(id)) {
return c.json({ error: "无效的文章ID" }, 400);
}
const articleData = (await c.req.json()) as Partial<KnowInfo>;
// 验证必填字段
if (!articleData.title) {
return c.json({ error: "文章标题不能为空" }, 400);
}
const apiClient = c.get('apiClient');
// 检查文章是否存在
const [existingArticle] = await apiClient.database
.table("know_info")
.where({ id, is_deleted: 0 });
if (!existingArticle) {
return c.json({ error: "文章不存在" }, 404);
}
// 更新文章
await apiClient.database
.table("know_info")
.where("id", id)
.update({
...articleData,
updated_at: apiClient.database.fn.now(),
});
// 获取更新后的文章
const [updatedArticle] = await apiClient.database
.table("know_info")
.where("id", id);
return c.json({
message: "知识库文章更新成功",
data: updatedArticle,
});
} catch (error) {
log.api("更新知识库文章失败:", error);
return c.json({ error: "更新知识库文章失败" }, 500);
}
});
// 删除知识库文章(软删除)
knowInfoRoutes.delete("/:id", withAuth, async (c) => {
try {
const id = Number(c.req.param("id"));
if (!id || isNaN(id)) {
return c.json({ error: "无效的文章ID" }, 400);
}
const apiClient = c.get('apiClient');
// 检查文章是否存在
const [existingArticle] = await apiClient.database
.table("know_info")
.where({ id, is_deleted: 0 });
if (!existingArticle) {
return c.json({ error: "文章不存在" }, 404);
}
// 软删除文章
await apiClient.database.table("know_info").where("id", id).update({
is_deleted: 1,
updated_at: apiClient.database.fn.now(),
});
return c.json({
message: "知识库文章删除成功",
});
} catch (error) {
log.api("删除知识库文章失败:", error);
return c.json({ error: "删除知识库文章失败" }, 500);
}
});
return knowInfoRoutes;
}