150 lines
4.1 KiB
Markdown
150 lines
4.1 KiB
Markdown
---
|
||
name: create-routes
|
||
description: 创建路由例子模板代码
|
||
---
|
||
# 创建路由例子模板代码
|
||
|
||
app是自定义@kevisual/router的一个APP
|
||
|
||
1. 一般来说,修改path,和对应的schema表,就可以快速创建对应的增删改查接口。
|
||
2. 根据需要,每一个功能需要添加对应的描述
|
||
3. 根据需要,对应schema表的字段进行修改代码
|
||
|
||
示例:
|
||
```ts
|
||
import { desc, eq, count, or, like, and } from 'drizzle-orm';
|
||
import { schema, app, db } from '@/app.ts'
|
||
|
||
|
||
app.route({
|
||
path: 'prompts',
|
||
key: 'list',
|
||
middleware: ['auth'],
|
||
description: '获取提示词列表',
|
||
}).define(async (ctx) => {
|
||
const tokenUser = ctx.state.tokenUser;
|
||
const uid = tokenUser.id;
|
||
const { page = 1, pageSize = 20, search, sort = 'DESC' } = ctx.query || {};
|
||
|
||
const offset = (page - 1) * pageSize;
|
||
const orderByField = sort === 'ASC' ? schema.prompts.updatedAt : desc(schema.prompts.updatedAt);
|
||
|
||
let whereCondition = eq(schema.prompts.uid, uid);
|
||
if (search) {
|
||
whereCondition = and(
|
||
eq(schema.prompts.uid, uid),
|
||
or(
|
||
like(schema.prompts.title, `%${search}%`),
|
||
like(schema.prompts.summary, `%${search}%`)
|
||
)
|
||
);
|
||
}
|
||
|
||
const [list, totalCount] = await Promise.all([
|
||
db.select()
|
||
.from(schema.prompts)
|
||
.where(whereCondition)
|
||
.limit(pageSize)
|
||
.offset(offset)
|
||
.orderBy(orderByField),
|
||
db.select({ count: count() })
|
||
.from(schema.prompts)
|
||
.where(whereCondition)
|
||
]);
|
||
|
||
ctx.body = {
|
||
list,
|
||
pagination: {
|
||
page,
|
||
current: page,
|
||
pageSize,
|
||
total: totalCount[0]?.count || 0,
|
||
},
|
||
};
|
||
return ctx;
|
||
}).addTo(app);
|
||
|
||
const promptUpdate = `创建或更新一个提示词, 参数定义:
|
||
title: 提示词标题, 必填
|
||
description: 描述, 选填
|
||
summary: 摘要, 选填
|
||
tags: 标签, 数组, 选填
|
||
link: 链接, 选填
|
||
data: 数据, 对象, 选填
|
||
parents: 父级ID数组, 选填
|
||
`;
|
||
app.route({
|
||
path: 'prompts',
|
||
key: 'update',
|
||
middleware: ['auth'],
|
||
description: promptUpdate,
|
||
}).define(async (ctx) => {
|
||
const { id, uid, updatedAt, ...rest } = ctx.query.data || {};
|
||
const tokenUser = ctx.state.tokenUser;
|
||
let prompt;
|
||
if (!id) {
|
||
prompt = await db.insert(schema.prompts).values({
|
||
title: rest.title,
|
||
description: rest.description,
|
||
...rest,
|
||
uid: tokenUser.id,
|
||
}).returning();
|
||
} else {
|
||
const existing = await db.select().from(schema.prompts).where(eq(schema.prompts.id, id)).limit(1);
|
||
if (existing.length === 0) {
|
||
ctx.throw(404, '没有找到对应的提示词');
|
||
}
|
||
if (existing[0].uid !== tokenUser.id) {
|
||
ctx.throw(403, '没有权限更新该提示词');
|
||
}
|
||
prompt = await db.update(schema.prompts).set({
|
||
...rest,
|
||
}).where(eq(schema.prompts.id, id)).returning();
|
||
}
|
||
ctx.body = prompt;
|
||
}).addTo(app);
|
||
|
||
|
||
app.route({
|
||
path: 'prompts',
|
||
key: 'delete',
|
||
middleware: ['auth'],
|
||
description: '删除提示词, 参数: id 提示词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.prompts).where(eq(schema.prompts.id, id)).limit(1);
|
||
if (existing.length === 0) {
|
||
ctx.throw(404, '没有找到对应的提示词');
|
||
}
|
||
if (existing[0].uid !== tokenUser.id) {
|
||
ctx.throw(403, '没有权限删除该提示词');
|
||
}
|
||
await db.delete(schema.prompts).where(eq(schema.prompts.id, id));
|
||
ctx.body = { success: true };
|
||
}).addTo(app);
|
||
|
||
app.route({
|
||
path: 'prompts',
|
||
key: 'get',
|
||
middleware: ['auth'],
|
||
description: '获取单个提示词, 参数: id 提示词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.prompts).where(eq(schema.prompts.id, id)).limit(1);
|
||
if (existing.length === 0) {
|
||
ctx.throw(404, '没有找到对应的提示词');
|
||
}
|
||
if (existing[0].uid !== tokenUser.id) {
|
||
ctx.throw(403, '没有权限查看该提示词');
|
||
}
|
||
ctx.body = existing[0];
|
||
}).addTo(app);
|
||
``` |