Files
code-center/src/routes/light-code/list.ts
2026-02-04 03:08:53 +08:00

184 lines
5.0 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { eq, desc, and, like, or } from 'drizzle-orm';
import { CustomError } from '@kevisual/router';
import { app, db, schema } from '../../app.ts';
import { filter } from '@kevisual/js-filter'
import { z } from 'zod';
app
.route({
path: 'light-code',
key: 'list',
description: `获取轻代码列表,参数
type: 代码类型light-code, ts`,
middleware: ['auth'],
metadata: {
args: {
type: z.string().optional().describe('代码类型light-code, ts'),
search: z.string().optional().describe('搜索关键词,匹配标题和描述'),
filter: z
.string()
.optional()
.describe(
'过滤条件SQL like格式字符串例如WHERE tags LIKE \'%tag1%\' AND tags LIKE \'%tag2%\'',
),
}
}
})
.define(async (ctx) => {
const tokenUser = ctx.state.tokenUser;
const { type, search, filter: filterQuery } = ctx.query || {};
const conditions = [eq(schema.kvContainer.uid, tokenUser.id)];
if (type) {
conditions.push(eq(schema.kvContainer.type, type as string));
}
if (search) {
const searchTerm = `%${search}%`;
conditions.push(
or(
like(schema.kvContainer.title, searchTerm),
like(schema.kvContainer.description, searchTerm),
),
);
}
const list = await db
.select({
id: schema.kvContainer.id,
title: schema.kvContainer.title,
description: schema.kvContainer.description,
type: schema.kvContainer.type,
tags: schema.kvContainer.tags,
data: schema.kvContainer.data,
code: schema.kvContainer.code,
uid: schema.kvContainer.uid,
createdAt: schema.kvContainer.createdAt,
updatedAt: schema.kvContainer.updatedAt,
hash: schema.kvContainer.hash,
})
.from(schema.kvContainer)
.where(and(...conditions))
.orderBy(desc(schema.kvContainer.updatedAt));
if (filterQuery) {
const filteredList = filter(list, filterQuery);
ctx.body = { list: filteredList }
} else {
ctx.body = { list };
}
return ctx;
})
.addTo(app);
app
.route({
path: 'light-code',
key: 'get',
description: '获取轻代码详情',
middleware: ['auth'],
})
.define(async (ctx) => {
const tokenUser = ctx.state.tokenUser;
const id = ctx.query.id;
if (!id) {
throw new CustomError('id is required');
}
const result = await db
.select()
.from(schema.kvContainer)
.where(eq(schema.kvContainer.id, id))
.limit(1);
const container = result[0];
if (!container) {
ctx.throw('未发现该代码内容');
}
if (container.uid !== tokenUser.id) {
ctx.throw('没有权限访问该代码内容');
}
ctx.body = container;
})
.addTo(app);
app
.route({
path: 'light-code',
key: 'update',
middleware: ['auth'],
isDebug: true,
})
.define(async (ctx) => {
const tokenUser = ctx.state.tokenUser;
const data = ctx.query.data;
const { id, ...container } = data;
if (id) {
const result = await db
.select()
.from(schema.kvContainer)
.where(eq(schema.kvContainer.id, id))
.limit(1);
const existing = result[0];
if (existing) {
await db
.update(schema.kvContainer)
.set({
...container,
updatedAt: new Date().toISOString(),
})
.where(eq(schema.kvContainer.id, id));
const updated = await db
.select()
.from(schema.kvContainer)
.where(eq(schema.kvContainer.id, id))
.limit(1);
ctx.body = updated[0];
} else {
ctx.body = null;
}
} else {
try {
console.log('created', container, 'userId', tokenUser.id);
const [created] = await db
.insert(schema.kvContainer)
.values({
title: container.title || '',
description: container.description || '',
type: container.type || 'light-code',
code: container.code || '',
data: container.data || {},
tags: container.tags || [],
hash: container.hash || '',
uid: tokenUser.id,
})
.returning();
ctx.body = created;
} catch (error) {
console.error('Error creating container:', error);
throw error;
}
}
})
.addTo(app);
app
.route({
path: 'light-code',
key: 'delete',
middleware: ['auth'],
})
.define(async (ctx) => {
const tokenUser = ctx.state.tokenUser;
const id = ctx.query.id;
const result = await db
.select()
.from(schema.kvContainer)
.where(eq(schema.kvContainer.id, id))
.limit(1);
const container = result[0];
if (!container) {
ctx.throw('未发现该容器');
}
if (container.uid !== tokenUser.id) {
ctx.throw('没有权限访问该容器');
}
await db.delete(schema.kvContainer).where(eq(schema.kvContainer.id, id));
ctx.body = container;
})
.addTo(app);