feat: 更新 mark 路由,添加 metadata 验证,移除未使用的代码
This commit is contained in:
@@ -1,8 +1,7 @@
|
|||||||
import { eq, desc, and, like, or, count, sql } from 'drizzle-orm';
|
import { eq, desc, and, like, or, count, sql } from 'drizzle-orm';
|
||||||
import { app, db, schema } from '../../app.ts';
|
import { app, db, schema } from '../../app.ts';
|
||||||
import { MarkServices } from './services/mark.ts';
|
import { MarkServices } from './services/mark.ts';
|
||||||
import dayjs from 'dayjs';
|
import z from 'zod';
|
||||||
import { nanoid } from 'nanoid';
|
|
||||||
|
|
||||||
app
|
app
|
||||||
.route({
|
.route({
|
||||||
@@ -10,6 +9,13 @@ app
|
|||||||
key: 'list',
|
key: 'list',
|
||||||
description: 'mark list.',
|
description: 'mark list.',
|
||||||
middleware: ['auth'],
|
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) => {
|
.define(async (ctx) => {
|
||||||
const tokenUser = ctx.state.tokenUser;
|
const tokenUser = ctx.state.tokenUser;
|
||||||
@@ -26,6 +32,9 @@ app
|
|||||||
path: 'mark',
|
path: 'mark',
|
||||||
key: 'getVersion',
|
key: 'getVersion',
|
||||||
middleware: ['auth'],
|
middleware: ['auth'],
|
||||||
|
metadata: {
|
||||||
|
id: z.string().describe('mark id'),
|
||||||
|
},
|
||||||
})
|
})
|
||||||
.define(async (ctx) => {
|
.define(async (ctx) => {
|
||||||
const tokenUser = ctx.state.tokenUser;
|
const tokenUser = ctx.state.tokenUser;
|
||||||
@@ -47,26 +56,6 @@ app
|
|||||||
};
|
};
|
||||||
} else {
|
} else {
|
||||||
ctx.throw(400, 'id is required');
|
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);
|
.addTo(app);
|
||||||
@@ -76,6 +65,9 @@ app
|
|||||||
path: 'mark',
|
path: 'mark',
|
||||||
key: 'get',
|
key: 'get',
|
||||||
middleware: ['auth'],
|
middleware: ['auth'],
|
||||||
|
metadata: {
|
||||||
|
id: z.string().describe('mark id'),
|
||||||
|
},
|
||||||
})
|
})
|
||||||
.define(async (ctx) => {
|
.define(async (ctx) => {
|
||||||
const tokenUser = ctx.state.tokenUser;
|
const tokenUser = ctx.state.tokenUser;
|
||||||
@@ -92,24 +84,6 @@ app
|
|||||||
ctx.body = markModel;
|
ctx.body = markModel;
|
||||||
} else {
|
} else {
|
||||||
ctx.throw(400, 'id is required');
|
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);
|
.addTo(app);
|
||||||
@@ -120,6 +94,9 @@ app
|
|||||||
key: 'update',
|
key: 'update',
|
||||||
middleware: ['auth'],
|
middleware: ['auth'],
|
||||||
isDebug: true,
|
isDebug: true,
|
||||||
|
metadata: {
|
||||||
|
id: z.string().describe('mark id'),
|
||||||
|
},
|
||||||
})
|
})
|
||||||
.define(async (ctx) => {
|
.define(async (ctx) => {
|
||||||
const tokenUser = ctx.state.tokenUser;
|
const tokenUser = ctx.state.tokenUser;
|
||||||
@@ -166,6 +143,14 @@ app
|
|||||||
path: 'mark',
|
path: 'mark',
|
||||||
key: 'updateNode',
|
key: 'updateNode',
|
||||||
middleware: ['auth'],
|
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) => {
|
.define(async (ctx) => {
|
||||||
const tokenUser = ctx.state.tokenUser;
|
const tokenUser = ctx.state.tokenUser;
|
||||||
@@ -183,7 +168,7 @@ app
|
|||||||
const currentData = markModel.data as any || {};
|
const currentData = markModel.data as any || {};
|
||||||
const nodes = currentData.nodes || [];
|
const nodes = currentData.nodes || [];
|
||||||
const nodeIndex = nodes.findIndex((n: any) => n.id === node.id);
|
const nodeIndex = nodes.findIndex((n: any) => n.id === node.id);
|
||||||
|
|
||||||
let updatedNodes;
|
let updatedNodes;
|
||||||
if (operate === 'delete') {
|
if (operate === 'delete') {
|
||||||
updatedNodes = nodes.filter((n: any) => n.id !== node.id);
|
updatedNodes = nodes.filter((n: any) => n.id !== node.id);
|
||||||
@@ -193,7 +178,7 @@ app
|
|||||||
} else {
|
} else {
|
||||||
updatedNodes = [...nodes, node];
|
updatedNodes = [...nodes, node];
|
||||||
}
|
}
|
||||||
|
|
||||||
const version = Number(markModel.version) + 1;
|
const version = Number(markModel.version) + 1;
|
||||||
const updated = await db.update(schema.microMark)
|
const updated = await db.update(schema.microMark)
|
||||||
.set({
|
.set({
|
||||||
@@ -211,6 +196,13 @@ app
|
|||||||
path: 'mark',
|
path: 'mark',
|
||||||
key: 'updateNodes',
|
key: 'updateNodes',
|
||||||
middleware: ['auth'],
|
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) => {
|
.define(async (ctx) => {
|
||||||
const tokenUser = ctx.state.tokenUser;
|
const tokenUser = ctx.state.tokenUser;
|
||||||
@@ -229,15 +221,15 @@ app
|
|||||||
if (nodeOperateList.some((item: any) => !item.node)) {
|
if (nodeOperateList.some((item: any) => !item.node)) {
|
||||||
ctx.throw(400, 'nodeOperateList node is required');
|
ctx.throw(400, 'nodeOperateList node is required');
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update multiple JSON nodes logic with Drizzle
|
// Update multiple JSON nodes logic with Drizzle
|
||||||
const currentData = markModel.data as any || {};
|
const currentData = markModel.data as any || {};
|
||||||
let nodes = currentData.nodes || [];
|
let nodes = currentData.nodes || [];
|
||||||
|
|
||||||
for (const item of nodeOperateList) {
|
for (const item of nodeOperateList) {
|
||||||
const { node, operate = 'update' } = item;
|
const { node, operate = 'update' } = item;
|
||||||
const nodeIndex = nodes.findIndex((n: any) => n.id === node.id);
|
const nodeIndex = nodes.findIndex((n: any) => n.id === node.id);
|
||||||
|
|
||||||
if (operate === 'delete') {
|
if (operate === 'delete') {
|
||||||
nodes = nodes.filter((n: any) => n.id !== node.id);
|
nodes = nodes.filter((n: any) => n.id !== node.id);
|
||||||
} else if (nodeIndex >= 0) {
|
} else if (nodeIndex >= 0) {
|
||||||
@@ -246,7 +238,7 @@ app
|
|||||||
nodes.push(node);
|
nodes.push(node);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const version = Number(markModel.version) + 1;
|
const version = Number(markModel.version) + 1;
|
||||||
const updated = await db.update(schema.microMark)
|
const updated = await db.update(schema.microMark)
|
||||||
.set({
|
.set({
|
||||||
@@ -265,6 +257,9 @@ app
|
|||||||
path: 'mark',
|
path: 'mark',
|
||||||
key: 'delete',
|
key: 'delete',
|
||||||
middleware: ['auth'],
|
middleware: ['auth'],
|
||||||
|
metadata: {
|
||||||
|
id: z.string().describe('mark id'),
|
||||||
|
},
|
||||||
})
|
})
|
||||||
.define(async (ctx) => {
|
.define(async (ctx) => {
|
||||||
const tokenUser = ctx.state.tokenUser;
|
const tokenUser = ctx.state.tokenUser;
|
||||||
@@ -283,7 +278,12 @@ app
|
|||||||
.addTo(app);
|
.addTo(app);
|
||||||
|
|
||||||
app
|
app
|
||||||
.route({ path: 'mark', key: 'getMenu', description: '获取菜单', middleware: ['auth'] })
|
.route({
|
||||||
|
path: 'mark',
|
||||||
|
key: 'getMenu',
|
||||||
|
description: '获取菜单',
|
||||||
|
middleware: ['auth']
|
||||||
|
})
|
||||||
.define(async (ctx) => {
|
.define(async (ctx) => {
|
||||||
const tokenUser = ctx.state.tokenUser;
|
const tokenUser = ctx.state.tokenUser;
|
||||||
const [rows, totalResult] = await Promise.all([
|
const [rows, totalResult] = await Promise.all([
|
||||||
|
|||||||
Reference in New Issue
Block a user