161 lines
4.4 KiB
TypeScript
161 lines
4.4 KiB
TypeScript
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);
|