添加 flowme 和 flowme-channel 路由,支持增删改查功能;更新 schema 以包含相关字段
This commit is contained in:
@@ -502,12 +502,35 @@ export const flowme = pgTable("flowme", {
|
||||
tags: jsonb().default([]),
|
||||
link: text('link').default(''),
|
||||
data: jsonb().default({}),
|
||||
|
||||
|
||||
channelId: uuid().references(() => flowmeChannels.id, { onDelete: 'set null' }),
|
||||
type: text('type').default(''),
|
||||
|
||||
source: text('source').default(''),
|
||||
importance: integer('importance').default(0), // 重要性等级
|
||||
isArchived: boolean('isArchived').default(false), // 是否归档
|
||||
|
||||
createdAt: timestamp('createdAt').notNull().defaultNow(),
|
||||
updatedAt: timestamp('updatedAt').notNull().defaultNow(),
|
||||
|
||||
}, (table) => [
|
||||
index('flowme_uid_idx').using('btree', table.uid.asc().nullsLast()),
|
||||
index('flowme_title_idx').using('btree', table.title.asc().nullsLast()),
|
||||
index('flowme_channel_id_idx').using('btree', table.channelId.asc().nullsLast()),
|
||||
]);
|
||||
|
||||
|
||||
export const flowmeChannels = pgTable("flowme_channels", {
|
||||
id: uuid().primaryKey().notNull().defaultRandom(),
|
||||
uid: uuid(),
|
||||
title: text('title').default(''),
|
||||
description: text('description').default(''),
|
||||
tags: jsonb().default([]),
|
||||
link: text('link').default(''),
|
||||
data: jsonb().default({}),
|
||||
color: text('color').default('#007bff'),
|
||||
createdAt: timestamp('createdAt').notNull().defaultNow(),
|
||||
updatedAt: timestamp('updatedAt').notNull().defaultNow(),
|
||||
}, (table) => [
|
||||
index('flowme_channels_uid_idx').using('btree', table.uid.asc().nullsLast()),
|
||||
index('flowme_channels_title_idx').using('btree', table.title.asc().nullsLast()),
|
||||
]);
|
||||
144
src/routes/flowme/flowme-channel/list.ts
Normal file
144
src/routes/flowme/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);
|
||||
5
src/routes/flowme/index.ts
Normal file
5
src/routes/flowme/index.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
import './list.ts'
|
||||
|
||||
// flowme channel 相关路由
|
||||
|
||||
import './flowme-channel/list.ts'
|
||||
160
src/routes/flowme/list.ts
Normal file
160
src/routes/flowme/list.ts
Normal file
@@ -0,0 +1,160 @@
|
||||
import { desc, eq, count, or, like, and } from 'drizzle-orm';
|
||||
import { schema, app, db } from '@/app.ts'
|
||||
|
||||
// 获取 flowme 列表
|
||||
app.route({
|
||||
path: 'flowme',
|
||||
key: 'list',
|
||||
middleware: ['auth'],
|
||||
description: '获取 flowme 列表',
|
||||
}).define(async (ctx) => {
|
||||
const tokenUser = ctx.state.tokenUser;
|
||||
const uid = tokenUser.id;
|
||||
const { page = 1, pageSize = 20, search, channelId, sort = 'DESC' } = ctx.query || {};
|
||||
|
||||
const offset = (page - 1) * pageSize;
|
||||
const orderByField = sort === 'ASC' ? schema.flowme.updatedAt : desc(schema.flowme.updatedAt);
|
||||
|
||||
let whereCondition = eq(schema.flowme.uid, uid);
|
||||
if (search) {
|
||||
whereCondition = and(
|
||||
eq(schema.flowme.uid, uid),
|
||||
or(
|
||||
like(schema.flowme.title, `%${search}%`),
|
||||
like(schema.flowme.description, `%${search}%`)
|
||||
)
|
||||
);
|
||||
}
|
||||
if (channelId) {
|
||||
whereCondition = and(
|
||||
whereCondition,
|
||||
eq(schema.flowme.channelId, channelId)
|
||||
);
|
||||
}
|
||||
|
||||
const [list, totalCount] = await Promise.all([
|
||||
db.select()
|
||||
.from(schema.flowme)
|
||||
.where(whereCondition)
|
||||
.limit(pageSize)
|
||||
.offset(offset)
|
||||
.orderBy(orderByField),
|
||||
db.select({ count: count() })
|
||||
.from(schema.flowme)
|
||||
.where(whereCondition)
|
||||
]);
|
||||
|
||||
ctx.body = {
|
||||
list,
|
||||
pagination: {
|
||||
page,
|
||||
current: page,
|
||||
pageSize,
|
||||
total: totalCount[0]?.count || 0,
|
||||
},
|
||||
};
|
||||
return ctx;
|
||||
}).addTo(app);
|
||||
|
||||
// 创建或更新 flowme
|
||||
const flowmeUpdate = `创建或更新一个 flowme, 参数定义:
|
||||
title: 标题, 必填
|
||||
description: 描述, 选填
|
||||
tags: 标签, 数组, 选填
|
||||
link: 链接, 选填
|
||||
data: 数据, 对象, 选填
|
||||
channelId: 频道ID, 选填
|
||||
type: 类型, 选填
|
||||
source: 来源, 选填
|
||||
importance: 重要性等级, 数字, 选填
|
||||
`;
|
||||
app.route({
|
||||
path: 'flowme',
|
||||
key: 'update',
|
||||
middleware: ['auth'],
|
||||
description: flowmeUpdate,
|
||||
}).define(async (ctx) => {
|
||||
const { id, uid, updatedAt, createdAt, ...rest } = ctx.query.data || {};
|
||||
const tokenUser = ctx.state.tokenUser;
|
||||
let flowmeItem;
|
||||
if (!id) {
|
||||
flowmeItem = await db.insert(schema.flowme).values({
|
||||
title: rest.title || '',
|
||||
description: rest.description || '',
|
||||
tags: rest.tags || [],
|
||||
link: rest.link || '',
|
||||
data: rest.data || {},
|
||||
channelId: rest.channelId || null,
|
||||
type: rest.type || '',
|
||||
source: rest.source || '',
|
||||
importance: rest.importance || 0,
|
||||
uid: tokenUser.id,
|
||||
}).returning();
|
||||
} else {
|
||||
const existing = await db.select().from(schema.flowme).where(eq(schema.flowme.id, id)).limit(1);
|
||||
if (existing.length === 0) {
|
||||
ctx.throw(404, '没有找到对应的 flowme');
|
||||
}
|
||||
if (existing[0].uid !== tokenUser.id) {
|
||||
ctx.throw(403, '没有权限更新该 flowme');
|
||||
}
|
||||
flowmeItem = await db.update(schema.flowme).set({
|
||||
title: rest.title,
|
||||
description: rest.description,
|
||||
tags: rest.tags,
|
||||
link: rest.link,
|
||||
data: rest.data,
|
||||
channelId: rest.channelId,
|
||||
type: rest.type,
|
||||
source: rest.source,
|
||||
importance: rest.importance,
|
||||
isArchived: rest.isArchived,
|
||||
}).where(eq(schema.flowme.id, id)).returning();
|
||||
}
|
||||
ctx.body = flowmeItem;
|
||||
}).addTo(app);
|
||||
|
||||
// 删除 flowme
|
||||
app.route({
|
||||
path: 'flowme',
|
||||
key: 'delete',
|
||||
middleware: ['auth'],
|
||||
description: '删除 flowme, 参数: 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.flowme).where(eq(schema.flowme.id, id)).limit(1);
|
||||
if (existing.length === 0) {
|
||||
ctx.throw(404, '没有找到对应的 flowme');
|
||||
}
|
||||
if (existing[0].uid !== tokenUser.id) {
|
||||
ctx.throw(403, '没有权限删除该 flowme');
|
||||
}
|
||||
await db.delete(schema.flowme).where(eq(schema.flowme.id, id));
|
||||
ctx.body = { success: true };
|
||||
}).addTo(app);
|
||||
|
||||
// 获取单个 flowme
|
||||
app.route({
|
||||
path: 'flowme',
|
||||
key: 'get',
|
||||
middleware: ['auth'],
|
||||
description: '获取单个 flowme, 参数: 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.flowme).where(eq(schema.flowme.id, id)).limit(1);
|
||||
if (existing.length === 0) {
|
||||
ctx.throw(404, '没有找到对应的 flowme');
|
||||
}
|
||||
if (existing[0].uid !== tokenUser.id) {
|
||||
ctx.throw(403, '没有权限查看该 flowme');
|
||||
}
|
||||
ctx.body = existing[0];
|
||||
}).addTo(app);
|
||||
Reference in New Issue
Block a user