remove old apps
This commit is contained in:
@@ -10,6 +10,7 @@ import './config/index.ts';
|
||||
|
||||
// import './file-listener/index.ts';
|
||||
|
||||
import './light-code/index.ts';
|
||||
|
||||
import './ai/index.ts';
|
||||
|
||||
|
||||
151
src/routes/light-code/list.ts
Normal file
151
src/routes/light-code/list.ts
Normal file
@@ -0,0 +1,151 @@
|
||||
import { eq, desc, and, like, or } from 'drizzle-orm';
|
||||
import { CustomError } from '@kevisual/router';
|
||||
import { app, db, schema } from '../../app.ts';
|
||||
|
||||
app
|
||||
.route({
|
||||
path: 'light-code',
|
||||
key: 'list',
|
||||
description: `获取轻代码列表,参数
|
||||
type: 代码类型light-code, ts`,
|
||||
middleware: ['auth'],
|
||||
})
|
||||
.define(async (ctx) => {
|
||||
const tokenUser = ctx.state.tokenUser;
|
||||
const { type, search } = 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,
|
||||
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));
|
||||
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'],
|
||||
})
|
||||
.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 {
|
||||
const [created] = await db
|
||||
.insert(schema.kvContainer)
|
||||
.values({
|
||||
...container,
|
||||
uid: tokenUser.id,
|
||||
})
|
||||
.returning();
|
||||
ctx.body = created;
|
||||
}
|
||||
return ctx;
|
||||
})
|
||||
.addTo(app);
|
||||
|
||||
app
|
||||
.route({
|
||||
path: 'container',
|
||||
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);
|
||||
Reference in New Issue
Block a user