feat: 添加 flowme-channel 功能,包括获取、创建、更新和删除接口;更新 flowme 列表接口以支持时间范围和类型过滤
This commit is contained in:
144
src/routes/flowme-channel/list.ts
Normal file
144
src/routes/flowme-channel/list.ts
Normal file
@@ -0,0 +1,144 @@
|
||||
import { desc, eq, count, or, like, and } from 'drizzle-orm';
|
||||
import { schema, app, db } from '@/app.ts'
|
||||
|
||||
// 获取 flowme-channel 列表
|
||||
app.route({
|
||||
path: 'flowme-channel',
|
||||
key: 'list',
|
||||
middleware: ['auth'],
|
||||
description: '获取 flowme-channel 列表',
|
||||
}).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.flowmeChannels.updatedAt : desc(schema.flowmeChannels.updatedAt);
|
||||
|
||||
let whereCondition = eq(schema.flowmeChannels.uid, uid);
|
||||
if (search) {
|
||||
whereCondition = and(
|
||||
eq(schema.flowmeChannels.uid, uid),
|
||||
or(
|
||||
like(schema.flowmeChannels.title, `%${search}%`),
|
||||
like(schema.flowmeChannels.description, `%${search}%`)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
const [list, totalCount] = await Promise.all([
|
||||
db.select()
|
||||
.from(schema.flowmeChannels)
|
||||
.where(whereCondition)
|
||||
.limit(pageSize)
|
||||
.offset(offset)
|
||||
.orderBy(orderByField),
|
||||
db.select({ count: count() })
|
||||
.from(schema.flowmeChannels)
|
||||
.where(whereCondition)
|
||||
]);
|
||||
|
||||
ctx.body = {
|
||||
list,
|
||||
pagination: {
|
||||
page,
|
||||
current: page,
|
||||
pageSize,
|
||||
total: totalCount[0]?.count || 0,
|
||||
},
|
||||
};
|
||||
return ctx;
|
||||
}).addTo(app);
|
||||
|
||||
// 创建或更新 flowme-channel
|
||||
const channelUpdate = `创建或更新一个 flowme-channel, 参数定义:
|
||||
title: 标题, 必填
|
||||
description: 描述, 选填
|
||||
tags: 标签, 数组, 选填
|
||||
link: 链接, 选填
|
||||
data: 数据, 对象, 选填
|
||||
color: 颜色, 选填, 默认 #007bff
|
||||
`;
|
||||
app.route({
|
||||
path: 'flowme-channel',
|
||||
key: 'update',
|
||||
middleware: ['auth'],
|
||||
description: channelUpdate,
|
||||
}).define(async (ctx) => {
|
||||
const { id, uid, updatedAt, createdAt, ...rest } = ctx.query.data || {};
|
||||
const tokenUser = ctx.state.tokenUser;
|
||||
let channel;
|
||||
if (!id) {
|
||||
channel = await db.insert(schema.flowmeChannels).values({
|
||||
title: rest.title || '',
|
||||
description: rest.description || '',
|
||||
tags: rest.tags || [],
|
||||
link: rest.link || '',
|
||||
data: rest.data || {},
|
||||
color: rest.color || '#007bff',
|
||||
uid: tokenUser.id,
|
||||
}).returning();
|
||||
} else {
|
||||
const existing = await db.select().from(schema.flowmeChannels).where(eq(schema.flowmeChannels.id, id)).limit(1);
|
||||
if (existing.length === 0) {
|
||||
ctx.throw(404, '没有找到对应的 channel');
|
||||
}
|
||||
if (existing[0].uid !== tokenUser.id) {
|
||||
ctx.throw(403, '没有权限更新该 channel');
|
||||
}
|
||||
channel = await db.update(schema.flowmeChannels).set({
|
||||
title: rest.title,
|
||||
description: rest.description,
|
||||
tags: rest.tags,
|
||||
link: rest.link,
|
||||
data: rest.data,
|
||||
color: rest.color,
|
||||
}).where(eq(schema.flowmeChannels.id, id)).returning();
|
||||
}
|
||||
ctx.body = channel;
|
||||
}).addTo(app);
|
||||
|
||||
// 删除 flowme-channel
|
||||
app.route({
|
||||
path: 'flowme-channel',
|
||||
key: 'delete',
|
||||
middleware: ['auth'],
|
||||
description: '删除 flowme-channel, 参数: data.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.flowmeChannels).where(eq(schema.flowmeChannels.id, id)).limit(1);
|
||||
if (existing.length === 0) {
|
||||
ctx.throw(404, '没有找到对应的 channel');
|
||||
}
|
||||
if (existing[0].uid !== tokenUser.id) {
|
||||
ctx.throw(403, '没有权限删除该 channel');
|
||||
}
|
||||
await db.delete(schema.flowmeChannels).where(eq(schema.flowmeChannels.id, id));
|
||||
ctx.body = { success: true };
|
||||
}).addTo(app);
|
||||
|
||||
// 获取单个 flowme-channel
|
||||
app.route({
|
||||
path: 'flowme-channel',
|
||||
key: 'get',
|
||||
middleware: ['auth'],
|
||||
description: '获取单个 flowme-channel, 参数: data.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.flowmeChannels).where(eq(schema.flowmeChannels.id, id)).limit(1);
|
||||
if (existing.length === 0) {
|
||||
ctx.throw(404, '没有找到对应的 channel');
|
||||
}
|
||||
if (existing[0].uid !== tokenUser.id) {
|
||||
ctx.throw(403, '没有权限查看该 channel');
|
||||
}
|
||||
ctx.body = existing[0];
|
||||
}).addTo(app);
|
||||
Reference in New Issue
Block a user