init first

This commit is contained in:
2025-11-24 03:52:19 +08:00
parent 125e519989
commit 21b56d0948
26 changed files with 7266 additions and 125 deletions

View File

@@ -0,0 +1,157 @@
import { generateId } from '../../module/utils.ts';
import { app } from '../../app.ts';
import { getDb } from '../../module/db.ts';
import { questionLibrary } from '../../module/schema.ts';
import { eq } from 'drizzle-orm';
// 列出问题库
app.route({
description: '列出问题库',
path: 'library',
key: 'list'
}).define(async (ctx) => {
const query = ctx.query;
const page = query.page ?? 1;
const pageSize = query.pageSize ?? 10;
const db = getDb();
try {
const offset = (page - 1) * pageSize;
const allResults = await db.select().from(questionLibrary);
// Sort by createdAt in descending order (newest first)
const sortedResults = allResults.sort((a, b) =>
new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime()
);
const list = sortedResults.slice(offset, offset + pageSize);
const totalResult = await db.select().from(questionLibrary);
const total = totalResult.length;
ctx.body = {
list,
pagination: {
page,
pageSize,
total,
},
};
} catch (error) {
console.error('获取问题库列表失败:', error);
ctx.throw(500, '获取问题库列表失败');
}
}).addTo(app);
// 更新问题库
app.route({
description: '更新问题库',
path: 'library',
key: 'update'
}).define(async (ctx) => {
const query = ctx.query;
const id = query.id;
const db = getDb();
const body = query;
try {
if (!id) {
// 新增数据
const newQuestion = await db
.insert(questionLibrary)
.values({
id: generateId(),
title: body.title,
description: body.description,
tags: JSON.stringify(body.tags || []),
repeat: body.repeat ?? false,
isUse: body.isUse ?? false,
usedAt: body.usedAt,
createdAt: new Date().toISOString(),
updatedAt: new Date().toISOString(),
})
.returning();
ctx.body = newQuestion[0];
} else {
// 更新数据
const updated = await db
.update(questionLibrary)
.set({
title: body.title,
description: body.description,
tags: JSON.stringify(body.tags || []),
repeat: body.repeat,
isUse: body.isUse,
usedAt: body.usedAt,
updatedAt: new Date().toISOString(),
})
.where(eq(questionLibrary.id, id))
.returning();
if (updated.length === 0) {
ctx.throw(404, '数据未找到');
}
ctx.body = { success: true, data: updated[0] };
}
} catch (error) {
console.error('更新问题库数据失败:', error);
ctx.throw(500, '更新问题库数据失败');
}
}).addTo(app);
// 删除问题库
app.route({
description: '删除问题库',
path: 'library',
key: 'delete'
}).define(async (ctx) => {
const query = ctx.query;
const id = query.id;
const db = getDb();
try {
if (!id) {
ctx.throw(400, '缺少ID参数');
}
const deleted = await db
.delete(questionLibrary)
.where(eq(questionLibrary.id, id))
.returning();
ctx.body = deleted;
} catch (error) {
console.error('删除问题库数据失败:', error);
ctx.throw(500, '删除问题库数据失败');
}
}).addTo(app);
// 获取问题库详情
app.route({
description: '获取问题库详情',
path: 'library',
key: 'detail'
}).define(async (ctx) => {
const query = ctx.query;
const id = query.id;
const db = getDb();
try {
if (!id) {
ctx.throw(400, '缺少ID参数');
}
const result = await db
.select()
.from(questionLibrary)
.where(eq(questionLibrary.id, id));
if (result.length === 0) {
ctx.throw(404, '数据未找到');
}
ctx.body = result[0];
} catch (error) {
console.error('获取问题库详情失败:', error);
ctx.throw(500, '获取问题库详情失败');
}
}).addTo(app);