feat: 更新 mark 路由,添加 metadata 验证,移除未使用的代码
This commit is contained in:
@@ -1,8 +1,7 @@
|
||||
import { eq, desc, and, like, or, count, sql } from 'drizzle-orm';
|
||||
import { app, db, schema } from '../../app.ts';
|
||||
import { MarkServices } from './services/mark.ts';
|
||||
import dayjs from 'dayjs';
|
||||
import { nanoid } from 'nanoid';
|
||||
import z from 'zod';
|
||||
|
||||
app
|
||||
.route({
|
||||
@@ -10,6 +9,13 @@ app
|
||||
key: 'list',
|
||||
description: 'mark list.',
|
||||
middleware: ['auth'],
|
||||
metadata: {
|
||||
page: z.number().optional().describe('页码'),
|
||||
pageSize: z.number().optional().describe('每页数量'),
|
||||
search: z.string().optional().describe('搜索关键词'),
|
||||
markType: z.string().optional().describe('mark类型,simple,wallnote,md,draw等'),
|
||||
sort: z.enum(['DESC', 'ASC']).default('DESC').describe('排序字段'),
|
||||
}
|
||||
})
|
||||
.define(async (ctx) => {
|
||||
const tokenUser = ctx.state.tokenUser;
|
||||
@@ -26,6 +32,9 @@ app
|
||||
path: 'mark',
|
||||
key: 'getVersion',
|
||||
middleware: ['auth'],
|
||||
metadata: {
|
||||
id: z.string().describe('mark id'),
|
||||
},
|
||||
})
|
||||
.define(async (ctx) => {
|
||||
const tokenUser = ctx.state.tokenUser;
|
||||
@@ -47,26 +56,6 @@ app
|
||||
};
|
||||
} else {
|
||||
ctx.throw(400, 'id is required');
|
||||
// const [markModel, created] = await MarkModel.findOrCreate({
|
||||
// where: {
|
||||
// uid: tokenUser.id,
|
||||
// puid: tokenUser.uid,
|
||||
// title: dayjs().format('YYYY-MM-DD'),
|
||||
// },
|
||||
// defaults: {
|
||||
// title: dayjs().format('YYYY-MM-DD'),
|
||||
// uid: tokenUser.id,
|
||||
// markType: 'wallnote',
|
||||
// tags: ['daily'],
|
||||
// },
|
||||
// });
|
||||
// ctx.body = {
|
||||
// version: Number(markModel.version),
|
||||
// updatedAt: markModel.updatedAt,
|
||||
// createdAt: markModel.createdAt,
|
||||
// id: markModel.id,
|
||||
// created: created,
|
||||
// };
|
||||
}
|
||||
})
|
||||
.addTo(app);
|
||||
@@ -76,6 +65,9 @@ app
|
||||
path: 'mark',
|
||||
key: 'get',
|
||||
middleware: ['auth'],
|
||||
metadata: {
|
||||
id: z.string().describe('mark id'),
|
||||
},
|
||||
})
|
||||
.define(async (ctx) => {
|
||||
const tokenUser = ctx.state.tokenUser;
|
||||
@@ -92,24 +84,6 @@ app
|
||||
ctx.body = markModel;
|
||||
} else {
|
||||
ctx.throw(400, 'id is required');
|
||||
// id 不存在,获取当天的title为 日期的一条数据
|
||||
// const [markModel, created] = await MarkModel.findOrCreate({
|
||||
// where: {
|
||||
// uid: tokenUser.id,
|
||||
// puid: tokenUser.uid,
|
||||
// title: dayjs().format('YYYY-MM-DD'),
|
||||
// },
|
||||
// defaults: {
|
||||
// title: dayjs().format('YYYY-MM-DD'),
|
||||
// uid: tokenUser.id,
|
||||
// markType: 'wallnote',
|
||||
// tags: ['daily'],
|
||||
// uname: tokenUser.username,
|
||||
// puid: tokenUser.uid,
|
||||
// version: 1,
|
||||
// },
|
||||
// });
|
||||
// ctx.body = markModel;
|
||||
}
|
||||
})
|
||||
.addTo(app);
|
||||
@@ -120,6 +94,9 @@ app
|
||||
key: 'update',
|
||||
middleware: ['auth'],
|
||||
isDebug: true,
|
||||
metadata: {
|
||||
id: z.string().describe('mark id'),
|
||||
},
|
||||
})
|
||||
.define(async (ctx) => {
|
||||
const tokenUser = ctx.state.tokenUser;
|
||||
@@ -166,6 +143,14 @@ app
|
||||
path: 'mark',
|
||||
key: 'updateNode',
|
||||
middleware: ['auth'],
|
||||
metadata: {
|
||||
id: z.string().describe('mark id'),
|
||||
operate: z.enum(['update', 'delete']).default('update').describe('节点操作类型,update或delete'),
|
||||
data: z.object({
|
||||
id: z.string().describe('节点id'),
|
||||
node: z.any().describe('要更新的节点数据'),
|
||||
}).describe('要更新的节点数据'),
|
||||
},
|
||||
})
|
||||
.define(async (ctx) => {
|
||||
const tokenUser = ctx.state.tokenUser;
|
||||
@@ -183,7 +168,7 @@ app
|
||||
const currentData = markModel.data as any || {};
|
||||
const nodes = currentData.nodes || [];
|
||||
const nodeIndex = nodes.findIndex((n: any) => n.id === node.id);
|
||||
|
||||
|
||||
let updatedNodes;
|
||||
if (operate === 'delete') {
|
||||
updatedNodes = nodes.filter((n: any) => n.id !== node.id);
|
||||
@@ -193,7 +178,7 @@ app
|
||||
} else {
|
||||
updatedNodes = [...nodes, node];
|
||||
}
|
||||
|
||||
|
||||
const version = Number(markModel.version) + 1;
|
||||
const updated = await db.update(schema.microMark)
|
||||
.set({
|
||||
@@ -211,6 +196,13 @@ app
|
||||
path: 'mark',
|
||||
key: 'updateNodes',
|
||||
middleware: ['auth'],
|
||||
metadata: {
|
||||
id: z.string().describe('mark id'),
|
||||
nodeOperateList: z.array(z.object({
|
||||
operate: z.enum(['update', 'delete']).default('update').describe('节点操作类型,update或delete'),
|
||||
node: z.any().describe('要更新的节点数据'),
|
||||
})).describe('要更新的节点列表'),
|
||||
},
|
||||
})
|
||||
.define(async (ctx) => {
|
||||
const tokenUser = ctx.state.tokenUser;
|
||||
@@ -229,15 +221,15 @@ app
|
||||
if (nodeOperateList.some((item: any) => !item.node)) {
|
||||
ctx.throw(400, 'nodeOperateList node is required');
|
||||
}
|
||||
|
||||
|
||||
// Update multiple JSON nodes logic with Drizzle
|
||||
const currentData = markModel.data as any || {};
|
||||
let nodes = currentData.nodes || [];
|
||||
|
||||
|
||||
for (const item of nodeOperateList) {
|
||||
const { node, operate = 'update' } = item;
|
||||
const nodeIndex = nodes.findIndex((n: any) => n.id === node.id);
|
||||
|
||||
|
||||
if (operate === 'delete') {
|
||||
nodes = nodes.filter((n: any) => n.id !== node.id);
|
||||
} else if (nodeIndex >= 0) {
|
||||
@@ -246,7 +238,7 @@ app
|
||||
nodes.push(node);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
const version = Number(markModel.version) + 1;
|
||||
const updated = await db.update(schema.microMark)
|
||||
.set({
|
||||
@@ -265,6 +257,9 @@ app
|
||||
path: 'mark',
|
||||
key: 'delete',
|
||||
middleware: ['auth'],
|
||||
metadata: {
|
||||
id: z.string().describe('mark id'),
|
||||
},
|
||||
})
|
||||
.define(async (ctx) => {
|
||||
const tokenUser = ctx.state.tokenUser;
|
||||
@@ -283,7 +278,12 @@ app
|
||||
.addTo(app);
|
||||
|
||||
app
|
||||
.route({ path: 'mark', key: 'getMenu', description: '获取菜单', middleware: ['auth'] })
|
||||
.route({
|
||||
path: 'mark',
|
||||
key: 'getMenu',
|
||||
description: '获取菜单',
|
||||
middleware: ['auth']
|
||||
})
|
||||
.define(async (ctx) => {
|
||||
const tokenUser = ctx.state.tokenUser;
|
||||
const [rows, totalResult] = await Promise.all([
|
||||
|
||||
Reference in New Issue
Block a user