This commit is contained in:
2025-11-24 18:58:32 +08:00
parent 8b9fdb706f
commit a2cee7c27a
28 changed files with 931 additions and 81 deletions

View File

@@ -2,12 +2,13 @@ import { generateId, getTodayDate } from '../module/utils.ts';
import { app } from '../app.ts';
import { getDb } from '../module/db.ts';
import { dailyQuestions, questionLibrary } from '../module/schema.ts';
import { eq } from 'drizzle-orm';
import { desc, eq } from 'drizzle-orm';
app.route({
path: 'daily',
key: 'random',
description: '随机获取一条未使用的问题'
description: '随机获取一条未使用的问题',
middleware: ['auth']
}).define(async (ctx) => {
const force = ctx.query.force ?? false;
const db = getDb();
@@ -37,14 +38,18 @@ app.route({
.from(questionLibrary)
.where(eq(questionLibrary.isUse, false));
let selectedQuestion;
if (unusedQuestions.length === 0) {
ctx.throw(404, '没有未使用的问题');
return;
selectedQuestion = {
title: '今天发生了什么有趣的事情?',
description: '请描述一下你今天经历的有趣事件,分享你的感受和想法。',
tags: JSON.stringify(['生活', '分享']),
}
} else {
const randomIndex = Math.floor(Math.random() * unusedQuestions.length);
selectedQuestion = unusedQuestions[randomIndex];
}
const randomIndex = Math.floor(Math.random() * unusedQuestions.length);
const selectedQuestion = unusedQuestions[randomIndex];
// 更新questionLibrary中的isUse和usedAt字段
await db
.update(questionLibrary)
@@ -60,8 +65,9 @@ app.route({
.insert(dailyQuestions)
.values({
id: generateId(),
qid: selectedQuestion.id,
qid: selectedQuestion.id || '',
title: selectedQuestion.title,
summary: selectedQuestion?.description,
description: '',
tags: selectedQuestion.tags,
date: day,
@@ -78,11 +84,12 @@ app.route({
app.route({
description: '获取今天的问题',
path: 'daily',
key: 'today'
key: 'today',
middleware: ['auth']
}).define(async (ctx) => {
const db = getDb();
const day = getTodayDate();
const token = ctx.query?.token;
try {
const todayQuestion = await db
.select()
@@ -95,11 +102,15 @@ app.route({
const res = await ctx.call({
path: 'daily',
key: 'random',
payload: {
token: token,
}
});
if (res.code === 200) {
ctx.body = res.body;
return;
}
console.error(res.message);
ctx.throw(500, '获取今天的问题失败');
}

View File

@@ -2,22 +2,41 @@ import { generateId } from '../../module/utils.ts';
import { app } from '../../app.ts';
import { getDb } from '../../module/db.ts';
import { dailyQuestions } from '../../module/schema.ts';
import { eq } from 'drizzle-orm';
import { eq, and, or, like } from 'drizzle-orm';
// 列出每日问题
app.route({
description: '列出每日问题',
path: 'daily',
key: 'list'
key: 'list',
middleware: ['auth']
}).define(async (ctx) => {
const query = ctx.query;
const page = query.page ?? 1;
const pageSize = query.pageSize ?? 99999;
const db = getDb();
const search = query.search ?? '';
const id = query.id ?? '';
try {
const offset = (page - 1) * pageSize;
const allResults = await db.select().from(dailyQuestions);
const allResults = await db.select().from(dailyQuestions).where(() => {
const conditions = [];
if (search) {
conditions.push(like(dailyQuestions.title, `%${search}%`));
}
if (id) {
conditions.push(eq(dailyQuestions.id, id));
}
// 如果需要 OR 逻辑(search 或 id 任一匹配)
return conditions.length > 0 ? or(...conditions) : undefined;
// 如果需要 AND 逻辑(search 和 id 都要匹配)
// return conditions.length > 0 ? and(...conditions) : undefined;
});
// Sort by createdAt in descending order (newest first)
const sortedResults = allResults.sort((a, b) =>
@@ -47,7 +66,8 @@ app.route({
app.route({
description: '更新每日问题',
path: 'daily',
key: 'update'
key: 'update',
middleware: ['auth']
}).define(async (ctx) => {
const query = ctx.query;
const id = query.id;
@@ -102,7 +122,8 @@ app.route({
app.route({
description: '删除每日问题',
path: 'daily',
key: 'delete'
key: 'delete',
middleware: ['auth']
}).define(async (ctx) => {
const query = ctx.query;
const id = query.id;
@@ -128,7 +149,8 @@ app.route({
app.route({
description: '获取每日问题详情',
path: 'daily',
key: 'detail'
key: 'detail',
middleware: ['auth']
}).define(async (ctx) => {
const query = ctx.query;
const id = query.id;

View File

@@ -1,4 +1,17 @@
import './daily/index.ts'
import './library/index.ts'
import './daily-task.ts'
import './library-task.ts'
import './library-task.ts'
import { app } from '../app.ts';
const hasAuth = app.router.routes.some(r => r.id === 'auth');
if (!hasAuth) {
console.log('添加认证中间件路由');
app.route({
path: 'auth',
key: 'auth',
description: '用户认证',
id: 'auth'
}).define(async (ctx) => {
// 这里可以添加实际的认证逻辑
}).addTo(app);
}

View File

@@ -7,7 +7,8 @@ import { eq } from 'drizzle-orm';
app.route({
path: 'library',
key: 'setAllUnused',
description: '将所有问题设置为未使用'
description: '将所有问题设置为未使用',
middleware: ['auth']
}).define(async (ctx) => {
const db = getDb();
try {

View File

@@ -8,7 +8,8 @@ import { eq } from 'drizzle-orm';
app.route({
description: '列出问题库',
path: 'library',
key: 'list'
key: 'list',
middleware: ['auth']
}).define(async (ctx) => {
const query = ctx.query;
const page = query.page ?? 1;
@@ -47,7 +48,8 @@ app.route({
app.route({
description: '更新问题库',
path: 'library',
key: 'update'
key: 'update',
middleware: ['auth']
}).define(async (ctx) => {
const query = ctx.query;
const id = query.id;
@@ -104,7 +106,8 @@ app.route({
app.route({
description: '删除问题库',
path: 'library',
key: 'delete'
key: 'delete',
middleware: ['auth']
}).define(async (ctx) => {
const query = ctx.query;
const id = query.id;
@@ -130,7 +133,8 @@ app.route({
app.route({
description: '获取问题库详情',
path: 'library',
key: 'detail'
key: 'detail',
middleware: ['auth']
}).define(async (ctx) => {
const query = ctx.query;
const id = query.id;