Files
cwd/cwd-comments-api/src/api/admin/listComments.ts
anghunk 3d0e3a317a feat: 新增CWD评论系统前后端代码及文档
refactor: 移除旧版评论系统代码并重构为Vue3前端

docs: 更新后端配置文档说明

fix: 修复评论提交频率限制和邮件通知逻辑

style: 格式化代码并优化样式

test: 添加Vitest测试配置

build: 更新依赖项和构建配置

chore: 清理无用文件和缓存
2026-01-19 14:58:18 +08:00

41 lines
1.1 KiB
TypeScript

import { Context } from 'hono';
import { Bindings } from '../../bindings';
export const listComments = async (c: Context<{ Bindings: Bindings }>) => {
const page = parseInt(c.req.query('page') || '1');
const limit = 10;
const offset = (page - 1) * limit;
// 1. 获取总数
const totalCount = await c.env.CWD_DB.prepare(
"SELECT COUNT(*) as count FROM Comment"
).first<{ count: number }>();
// 2. 分页查询数据
const { results } = await c.env.CWD_DB.prepare(
`SELECT * FROM Comment ORDER BY pub_date DESC LIMIT ? OFFSET ?`
).bind(limit, offset).all();
// 3. 映射字段名以符合你的 API 规范
const data = results.map((row: any) => ({
id: row.id,
pubDate: row.pub_date,
author: row.author,
email: row.email,
postSlug: row.post_slug,
url: row.url,
ipAddress: row.ip_address,
contentText: row.content_text,
contentHtml: row.content_html,
status: row.status
}));
return c.json({
data,
pagination: {
page,
limit,
total: Math.ceil((totalCount?. count || 0) / limit)
}
});
};