feat: 添加短链管理功能,包括创建、更新、删除和列表接口
This commit is contained in:
48
src/routes/n5-link/n5-make/list.ts
Normal file
48
src/routes/n5-link/n5-make/list.ts
Normal file
@@ -0,0 +1,48 @@
|
||||
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);
|
||||
Reference in New Issue
Block a user