feat: 添加短链管理功能,包括创建、更新、删除和列表接口
This commit is contained in:
24
src/routes/n5-link/n5-make/create.ts
Normal file
24
src/routes/n5-link/n5-make/create.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
import { eq } from 'drizzle-orm';
|
||||
import { app, db, schema } from '@/app.ts';
|
||||
|
||||
app.route({
|
||||
path: 'n5-make',
|
||||
key: 'create',
|
||||
middleware: ['auth'],
|
||||
description: `创建 Make, 参数:
|
||||
slug: 唯一业务ID, 必填
|
||||
resources: 资源列表(数组), 选填
|
||||
`,
|
||||
}).define(async (ctx) => {
|
||||
const tokenUser = ctx.state.tokenUser;
|
||||
const { userId, createdAt, updatedAt, id: _id, ...rest } = ctx.query.data || {};
|
||||
if (!rest.slug) {
|
||||
ctx.throw(400, 'slug 参数缺失');
|
||||
}
|
||||
const result = await db.insert(schema.n5Make).values({
|
||||
...rest,
|
||||
userId: tokenUser.id,
|
||||
}).returning();
|
||||
ctx.body = result[0];
|
||||
return ctx;
|
||||
}).addTo(app);
|
||||
25
src/routes/n5-link/n5-make/delete.ts
Normal file
25
src/routes/n5-link/n5-make/delete.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
import { eq } from 'drizzle-orm';
|
||||
import { app, db, schema } from '@/app.ts';
|
||||
|
||||
app.route({
|
||||
path: 'n5-make',
|
||||
key: 'delete',
|
||||
middleware: ['auth'],
|
||||
description: '删除 Make, 参数: id Make ID',
|
||||
}).define(async (ctx) => {
|
||||
const tokenUser = ctx.state.tokenUser;
|
||||
const { id } = ctx.query.data || {};
|
||||
if (!id) {
|
||||
ctx.throw(400, 'id 参数缺失');
|
||||
}
|
||||
const existing = await db.select().from(schema.n5Make).where(eq(schema.n5Make.id, id)).limit(1);
|
||||
if (existing.length === 0) {
|
||||
ctx.throw(404, 'Make 不存在');
|
||||
}
|
||||
if (existing[0].userId !== tokenUser.id) {
|
||||
ctx.throw(403, '没有权限删除该 Make');
|
||||
}
|
||||
await db.delete(schema.n5Make).where(eq(schema.n5Make.id, id));
|
||||
ctx.body = { success: true };
|
||||
return ctx;
|
||||
}).addTo(app);
|
||||
4
src/routes/n5-link/n5-make/index.ts
Normal file
4
src/routes/n5-link/n5-make/index.ts
Normal file
@@ -0,0 +1,4 @@
|
||||
import './list.ts';
|
||||
import './create.ts';
|
||||
import './update.ts';
|
||||
import './delete.ts';
|
||||
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);
|
||||
29
src/routes/n5-link/n5-make/update.ts
Normal file
29
src/routes/n5-link/n5-make/update.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
import { eq } from 'drizzle-orm';
|
||||
import { app, db, schema } from '@/app.ts';
|
||||
|
||||
app.route({
|
||||
path: 'n5-make',
|
||||
key: 'update',
|
||||
middleware: ['auth'],
|
||||
description: `更新 Make, 参数:
|
||||
id: Make ID, 必填
|
||||
slug: 唯一业务ID, 选填
|
||||
resources: 资源列表(数组), 选填
|
||||
`,
|
||||
}).define(async (ctx) => {
|
||||
const tokenUser = ctx.state.tokenUser;
|
||||
const { id, userId, createdAt, updatedAt, ...rest } = ctx.query.data || {};
|
||||
if (!id) {
|
||||
ctx.throw(400, 'id 参数缺失');
|
||||
}
|
||||
const existing = await db.select().from(schema.n5Make).where(eq(schema.n5Make.id, id)).limit(1);
|
||||
if (existing.length === 0) {
|
||||
ctx.throw(404, 'Make 不存在');
|
||||
}
|
||||
if (existing[0].userId !== tokenUser.id) {
|
||||
ctx.throw(403, '没有权限更新该 Make');
|
||||
}
|
||||
const result = await db.update(schema.n5Make).set({ ...rest }).where(eq(schema.n5Make.id, id)).returning();
|
||||
ctx.body = result[0];
|
||||
return ctx;
|
||||
}).addTo(app);
|
||||
Reference in New Issue
Block a user