generated from template/router-template
update
This commit is contained in:
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user