import { desc, eq, count } from 'drizzle-orm'; import { app, db, schema } from '@/app.ts'; // 列表 app.route({ path: 'n5-make', key: 'list', middleware: ['auth'], description: '获取 Make 列表, 参数: page, pageSize, sort', }).define(async (ctx) => { const tokenUser = ctx.state.tokenUser; const uid = tokenUser.id; const { page = 1, pageSize = 20, sort = 'DESC' } = ctx.query || {}; const offset = (page - 1) * pageSize; const orderByField = sort === 'ASC' ? schema.n5Make.updatedAt : desc(schema.n5Make.updatedAt); const whereCondition = eq(schema.n5Make.userId, uid); const [list, totalCount] = await Promise.all([ db.select().from(schema.n5Make).where(whereCondition).limit(pageSize).offset(offset).orderBy(orderByField), db.select({ count: count() }).from(schema.n5Make).where(whereCondition), ]); ctx.body = { list, pagination: { page, current: page, pageSize, total: totalCount[0]?.count || 0 }, }; return ctx; }).addTo(app); // 获取单个 app.route({ path: 'n5-make', key: 'get', description: '获取单个 Make, 参数: id 或 slug', }).define(async (ctx) => { const { id, slug } = ctx.query || {}; if (!id && !slug) { ctx.throw(400, 'id 或 slug 参数缺失'); } const condition = id ? eq(schema.n5Make.id, id) : eq(schema.n5Make.slug, slug); const existing = await db.select().from(schema.n5Make).where(condition).limit(1); if (existing.length === 0) { ctx.throw(404, 'Make 不存在'); } ctx.body = existing[0]; return ctx; }).addTo(app);