update
This commit is contained in:
@@ -41,7 +41,7 @@ app.route({
|
||||
text,
|
||||
cost: cost,
|
||||
model,
|
||||
payload
|
||||
action: payload
|
||||
}
|
||||
}).addTo(app)
|
||||
|
||||
|
||||
@@ -15,4 +15,6 @@ import './ai/index.ts';
|
||||
|
||||
import './prompts/index.ts'
|
||||
|
||||
import './views/index.ts';
|
||||
import './views/index.ts';
|
||||
|
||||
import './query-views/index.ts';
|
||||
1
src/routes/query-views/index.ts
Normal file
1
src/routes/query-views/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
import './list.ts'
|
||||
138
src/routes/query-views/list.ts
Normal file
138
src/routes/query-views/list.ts
Normal file
@@ -0,0 +1,138 @@
|
||||
import { desc, eq, count, or, like, and } from 'drizzle-orm';
|
||||
import { schema, app, db } from '@/app.ts'
|
||||
|
||||
|
||||
app.route({
|
||||
path: 'query-views',
|
||||
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.queryViews.updatedAt : desc(schema.queryViews.updatedAt);
|
||||
|
||||
let whereCondition = eq(schema.queryViews.uid, uid);
|
||||
if (search) {
|
||||
whereCondition = and(
|
||||
eq(schema.queryViews.uid, uid),
|
||||
or(
|
||||
like(schema.queryViews.title, `%${search}%`),
|
||||
like(schema.queryViews.summary, `%${search}%`)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
const [list, totalCount] = await Promise.all([
|
||||
db.select()
|
||||
.from(schema.queryViews)
|
||||
.where(whereCondition)
|
||||
.limit(pageSize)
|
||||
.offset(offset)
|
||||
.orderBy(orderByField),
|
||||
db.select({ count: count() })
|
||||
.from(schema.queryViews)
|
||||
.where(whereCondition)
|
||||
]);
|
||||
|
||||
ctx.body = {
|
||||
list,
|
||||
pagination: {
|
||||
page,
|
||||
current: page,
|
||||
pageSize,
|
||||
total: totalCount[0]?.count || 0,
|
||||
},
|
||||
};
|
||||
return ctx;
|
||||
}).addTo(app);
|
||||
|
||||
const viewUpdate = `创建或更新一个查询视图, 参数定义:
|
||||
title: 视图标题, 必填
|
||||
data: 数据, 对象, 选填
|
||||
`;
|
||||
app.route({
|
||||
path: 'query-views',
|
||||
key: 'update',
|
||||
middleware: ['auth'],
|
||||
description: viewUpdate,
|
||||
}).define(async (ctx) => {
|
||||
const { id, uid, updatedAt, ...rest } = ctx.query.data || {};
|
||||
const tokenUser = ctx.state.tokenUser;
|
||||
let view;
|
||||
if (!id) {
|
||||
view = await db.insert(schema.queryViews).values({
|
||||
title: rest.title || '',
|
||||
description: rest.description || '',
|
||||
summary: rest.summary || '',
|
||||
tags: rest.tags || [],
|
||||
link: rest.link || '',
|
||||
data: rest.data || { items: [] },
|
||||
uid: tokenUser.id,
|
||||
}).returning();
|
||||
} else {
|
||||
const existing = await db.select().from(schema.queryViews).where(eq(schema.queryViews.id, id)).limit(1);
|
||||
if (existing.length === 0) {
|
||||
ctx.throw(404, '没有找到对应的查询视图');
|
||||
}
|
||||
if (existing[0].uid !== tokenUser.id) {
|
||||
ctx.throw(403, '没有权限更新该查询视图');
|
||||
}
|
||||
view = await db.update(schema.queryViews).set({
|
||||
title: rest.title,
|
||||
description: rest.description,
|
||||
summary: rest.summary,
|
||||
tags: rest.tags,
|
||||
link: rest.link,
|
||||
data: rest.data,
|
||||
}).where(eq(schema.queryViews.id, id)).returning();
|
||||
}
|
||||
ctx.body = view;
|
||||
}).addTo(app);
|
||||
|
||||
|
||||
app.route({
|
||||
path: 'query-views',
|
||||
key: 'delete',
|
||||
middleware: ['auth'],
|
||||
description: '删除查询视图, 参数: data.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.queryViews).where(eq(schema.queryViews.id, id)).limit(1);
|
||||
if (existing.length === 0) {
|
||||
ctx.throw(404, '没有找到对应的查询视图');
|
||||
}
|
||||
if (existing[0].uid !== tokenUser.id) {
|
||||
ctx.throw(403, '没有权限删除该查询视图');
|
||||
}
|
||||
await db.delete(schema.queryViews).where(eq(schema.queryViews.id, id));
|
||||
ctx.body = { success: true };
|
||||
}).addTo(app);
|
||||
|
||||
app.route({
|
||||
path: 'query-views',
|
||||
key: 'get',
|
||||
middleware: ['auth'],
|
||||
description: '获取单个查询视图, 参数: data.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.queryViews).where(eq(schema.queryViews.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);
|
||||
@@ -101,7 +101,7 @@ app.route({
|
||||
path: 'views',
|
||||
key: 'delete',
|
||||
middleware: ['auth'],
|
||||
description: '删除视图, 参数: id 视图ID',
|
||||
description: '删除视图, 参数: data.id 视图ID',
|
||||
}).define(async (ctx) => {
|
||||
const tokenUser = ctx.state.tokenUser;
|
||||
const { id } = ctx.query.data || {};
|
||||
@@ -123,7 +123,7 @@ app.route({
|
||||
path: 'views',
|
||||
key: 'get',
|
||||
middleware: ['auth'],
|
||||
description: '获取单个视图, 参数: id 视图ID',
|
||||
description: '获取单个视图, 参数: data.id 视图ID',
|
||||
}).define(async (ctx) => {
|
||||
const tokenUser = ctx.state.tokenUser;
|
||||
const { id } = ctx.query.data || {};
|
||||
|
||||
Reference in New Issue
Block a user