feat: 添加短链管理功能,包括创建、更新、删除和列表接口
This commit is contained in:
@@ -20,4 +20,6 @@ import './views/index.ts';
|
||||
|
||||
import './query-views/index.ts';
|
||||
|
||||
import './flowme/index.ts'
|
||||
import './flowme/index.ts'
|
||||
|
||||
import './n5-link/index.ts'
|
||||
1
src/routes/n5-link/index.ts
Normal file
1
src/routes/n5-link/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
import './list.ts';
|
||||
3
src/routes/n5-link/list.ts
Normal file
3
src/routes/n5-link/list.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
import './short-link.ts';
|
||||
import './n5-make/index.ts';
|
||||
import './n5-shop/index.ts';
|
||||
7
src/routes/n5-link/modules/n5.services.ts
Normal file
7
src/routes/n5-link/modules/n5.services.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
import { desc, eq, count, or, like, and } from 'drizzle-orm';
|
||||
import { app, db, schema } from '@/app.ts';
|
||||
export class N5Service {
|
||||
static async getBySlug(slug: string) {
|
||||
return db.select().from(schema.shortLink).where(eq(schema.shortLink.slug, slug)).limit(1);
|
||||
}
|
||||
}
|
||||
24
src/routes/n5-link/n5-make/create.ts
Normal file
24
src/routes/n5-link/n5-make/create.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
import { eq } from 'drizzle-orm';
|
||||
import { app, db, schema } from '@/app.ts';
|
||||
|
||||
app.route({
|
||||
path: 'n5-make',
|
||||
key: 'create',
|
||||
middleware: ['auth'],
|
||||
description: `创建 Make, 参数:
|
||||
slug: 唯一业务ID, 必填
|
||||
resources: 资源列表(数组), 选填
|
||||
`,
|
||||
}).define(async (ctx) => {
|
||||
const tokenUser = ctx.state.tokenUser;
|
||||
const { userId, createdAt, updatedAt, id: _id, ...rest } = ctx.query.data || {};
|
||||
if (!rest.slug) {
|
||||
ctx.throw(400, 'slug 参数缺失');
|
||||
}
|
||||
const result = await db.insert(schema.n5Make).values({
|
||||
...rest,
|
||||
userId: tokenUser.id,
|
||||
}).returning();
|
||||
ctx.body = result[0];
|
||||
return ctx;
|
||||
}).addTo(app);
|
||||
25
src/routes/n5-link/n5-make/delete.ts
Normal file
25
src/routes/n5-link/n5-make/delete.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
import { eq } from 'drizzle-orm';
|
||||
import { app, db, schema } from '@/app.ts';
|
||||
|
||||
app.route({
|
||||
path: 'n5-make',
|
||||
key: 'delete',
|
||||
middleware: ['auth'],
|
||||
description: '删除 Make, 参数: id Make 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.n5Make).where(eq(schema.n5Make.id, id)).limit(1);
|
||||
if (existing.length === 0) {
|
||||
ctx.throw(404, 'Make 不存在');
|
||||
}
|
||||
if (existing[0].userId !== tokenUser.id) {
|
||||
ctx.throw(403, '没有权限删除该 Make');
|
||||
}
|
||||
await db.delete(schema.n5Make).where(eq(schema.n5Make.id, id));
|
||||
ctx.body = { success: true };
|
||||
return ctx;
|
||||
}).addTo(app);
|
||||
4
src/routes/n5-link/n5-make/index.ts
Normal file
4
src/routes/n5-link/n5-make/index.ts
Normal file
@@ -0,0 +1,4 @@
|
||||
import './list.ts';
|
||||
import './create.ts';
|
||||
import './update.ts';
|
||||
import './delete.ts';
|
||||
48
src/routes/n5-link/n5-make/list.ts
Normal file
48
src/routes/n5-link/n5-make/list.ts
Normal file
@@ -0,0 +1,48 @@
|
||||
import { desc, eq, count } from 'drizzle-orm';
|
||||
import { app, db, schema } from '@/app.ts';
|
||||
|
||||
// 列表
|
||||
app.route({
|
||||
path: 'n5-make',
|
||||
key: 'list',
|
||||
middleware: ['auth'],
|
||||
description: '获取 Make 列表, 参数: page, pageSize, sort',
|
||||
}).define(async (ctx) => {
|
||||
const tokenUser = ctx.state.tokenUser;
|
||||
const uid = tokenUser.id;
|
||||
const { page = 1, pageSize = 20, sort = 'DESC' } = ctx.query || {};
|
||||
|
||||
const offset = (page - 1) * pageSize;
|
||||
const orderByField = sort === 'ASC' ? schema.n5Make.updatedAt : desc(schema.n5Make.updatedAt);
|
||||
|
||||
const whereCondition = eq(schema.n5Make.userId, uid);
|
||||
const [list, totalCount] = await Promise.all([
|
||||
db.select().from(schema.n5Make).where(whereCondition).limit(pageSize).offset(offset).orderBy(orderByField),
|
||||
db.select({ count: count() }).from(schema.n5Make).where(whereCondition),
|
||||
]);
|
||||
|
||||
ctx.body = {
|
||||
list,
|
||||
pagination: { page, current: page, pageSize, total: totalCount[0]?.count || 0 },
|
||||
};
|
||||
return ctx;
|
||||
}).addTo(app);
|
||||
|
||||
// 获取单个
|
||||
app.route({
|
||||
path: 'n5-make',
|
||||
key: 'get',
|
||||
description: '获取单个 Make, 参数: id 或 slug',
|
||||
}).define(async (ctx) => {
|
||||
const { id, slug } = ctx.query || {};
|
||||
if (!id && !slug) {
|
||||
ctx.throw(400, 'id 或 slug 参数缺失');
|
||||
}
|
||||
const condition = id ? eq(schema.n5Make.id, id) : eq(schema.n5Make.slug, slug);
|
||||
const existing = await db.select().from(schema.n5Make).where(condition).limit(1);
|
||||
if (existing.length === 0) {
|
||||
ctx.throw(404, 'Make 不存在');
|
||||
}
|
||||
ctx.body = existing[0];
|
||||
return ctx;
|
||||
}).addTo(app);
|
||||
29
src/routes/n5-link/n5-make/update.ts
Normal file
29
src/routes/n5-link/n5-make/update.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
import { eq } from 'drizzle-orm';
|
||||
import { app, db, schema } from '@/app.ts';
|
||||
|
||||
app.route({
|
||||
path: 'n5-make',
|
||||
key: 'update',
|
||||
middleware: ['auth'],
|
||||
description: `更新 Make, 参数:
|
||||
id: Make ID, 必填
|
||||
slug: 唯一业务ID, 选填
|
||||
resources: 资源列表(数组), 选填
|
||||
`,
|
||||
}).define(async (ctx) => {
|
||||
const tokenUser = ctx.state.tokenUser;
|
||||
const { id, userId, createdAt, updatedAt, ...rest } = ctx.query.data || {};
|
||||
if (!id) {
|
||||
ctx.throw(400, 'id 参数缺失');
|
||||
}
|
||||
const existing = await db.select().from(schema.n5Make).where(eq(schema.n5Make.id, id)).limit(1);
|
||||
if (existing.length === 0) {
|
||||
ctx.throw(404, 'Make 不存在');
|
||||
}
|
||||
if (existing[0].userId !== tokenUser.id) {
|
||||
ctx.throw(403, '没有权限更新该 Make');
|
||||
}
|
||||
const result = await db.update(schema.n5Make).set({ ...rest }).where(eq(schema.n5Make.id, id)).returning();
|
||||
ctx.body = result[0];
|
||||
return ctx;
|
||||
}).addTo(app);
|
||||
32
src/routes/n5-link/n5-shop/create.ts
Normal file
32
src/routes/n5-link/n5-shop/create.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
import { eq } from 'drizzle-orm';
|
||||
import { app, db, schema } from '@/app.ts';
|
||||
|
||||
app.route({
|
||||
path: 'n5-shop',
|
||||
key: 'create',
|
||||
middleware: ['auth'],
|
||||
description: `创建商品, 参数:
|
||||
slug: 唯一业务ID, 必填
|
||||
title: 商品标题, 必填
|
||||
platform: 商品平台, 必填
|
||||
orderLink: 订单链接, 必填
|
||||
description: 商品描述, 选填
|
||||
link: 商品链接, 选填
|
||||
tags: 标签数组, 选填
|
||||
data: 其他商品信息对象, 选填
|
||||
userinfo: 卖家信息, 选填
|
||||
`,
|
||||
}).define(async (ctx) => {
|
||||
const tokenUser = ctx.state.tokenUser;
|
||||
const { userId, createdAt, updatedAt, id: _id, ...rest } = ctx.query.data || {};
|
||||
if (!rest.slug) ctx.throw(400, 'slug 参数缺失');
|
||||
if (!rest.title) ctx.throw(400, 'title 参数缺失');
|
||||
if (!rest.platform) ctx.throw(400, 'platform 参数缺失');
|
||||
if (!rest.orderLink) ctx.throw(400, 'orderLink 参数缺失');
|
||||
const result = await db.insert(schema.n5Shop).values({
|
||||
...rest,
|
||||
userId: tokenUser.id,
|
||||
}).returning();
|
||||
ctx.body = result[0];
|
||||
return ctx;
|
||||
}).addTo(app);
|
||||
25
src/routes/n5-link/n5-shop/delete.ts
Normal file
25
src/routes/n5-link/n5-shop/delete.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
import { eq } from 'drizzle-orm';
|
||||
import { app, db, schema } from '@/app.ts';
|
||||
|
||||
app.route({
|
||||
path: 'n5-shop',
|
||||
key: 'delete',
|
||||
middleware: ['auth'],
|
||||
description: '删除商品, 参数: id 商品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.n5Shop).where(eq(schema.n5Shop.id, id)).limit(1);
|
||||
if (existing.length === 0) {
|
||||
ctx.throw(404, '商品不存在');
|
||||
}
|
||||
if (existing[0].userId !== tokenUser.id) {
|
||||
ctx.throw(403, '没有权限删除该商品');
|
||||
}
|
||||
await db.delete(schema.n5Shop).where(eq(schema.n5Shop.id, id));
|
||||
ctx.body = { success: true };
|
||||
return ctx;
|
||||
}).addTo(app);
|
||||
4
src/routes/n5-link/n5-shop/index.ts
Normal file
4
src/routes/n5-link/n5-shop/index.ts
Normal file
@@ -0,0 +1,4 @@
|
||||
import './list.ts';
|
||||
import './create.ts';
|
||||
import './update.ts';
|
||||
import './delete.ts';
|
||||
58
src/routes/n5-link/n5-shop/list.ts
Normal file
58
src/routes/n5-link/n5-shop/list.ts
Normal file
@@ -0,0 +1,58 @@
|
||||
import { desc, eq, count, or, like, and } from 'drizzle-orm';
|
||||
import { app, db, schema } from '@/app.ts';
|
||||
|
||||
// 列表
|
||||
app.route({
|
||||
path: 'n5-shop',
|
||||
key: 'list',
|
||||
middleware: ['auth'],
|
||||
description: '获取商品列表, 参数: page, pageSize, search, sort',
|
||||
}).define(async (ctx) => {
|
||||
const tokenUser = ctx.state.tokenUser;
|
||||
const uid = tokenUser.id;
|
||||
const { page = 1, pageSize = 20, search, sort = 'DESC' } = ctx.query || {};
|
||||
|
||||
const offset = (page - 1) * pageSize;
|
||||
const orderByField = sort === 'ASC' ? schema.n5Shop.updatedAt : desc(schema.n5Shop.updatedAt);
|
||||
|
||||
let whereCondition: any = eq(schema.n5Shop.userId, uid);
|
||||
if (search) {
|
||||
whereCondition = and(
|
||||
eq(schema.n5Shop.userId, uid),
|
||||
or(
|
||||
like(schema.n5Shop.title, `%${search}%`),
|
||||
like(schema.n5Shop.slug, `%${search}%`),
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
const [list, totalCount] = await Promise.all([
|
||||
db.select().from(schema.n5Shop).where(whereCondition).limit(pageSize).offset(offset).orderBy(orderByField),
|
||||
db.select({ count: count() }).from(schema.n5Shop).where(whereCondition),
|
||||
]);
|
||||
|
||||
ctx.body = {
|
||||
list,
|
||||
pagination: { page, current: page, pageSize, total: totalCount[0]?.count || 0 },
|
||||
};
|
||||
return ctx;
|
||||
}).addTo(app);
|
||||
|
||||
// 获取单个
|
||||
app.route({
|
||||
path: 'n5-shop',
|
||||
key: 'get',
|
||||
description: '获取单个商品, 参数: id 或 slug',
|
||||
}).define(async (ctx) => {
|
||||
const { id, slug } = ctx.query || {};
|
||||
if (!id && !slug) {
|
||||
ctx.throw(400, 'id 或 slug 参数缺失');
|
||||
}
|
||||
const condition = id ? eq(schema.n5Shop.id, id) : eq(schema.n5Shop.slug, slug);
|
||||
const existing = await db.select().from(schema.n5Shop).where(condition).limit(1);
|
||||
if (existing.length === 0) {
|
||||
ctx.throw(404, '商品不存在');
|
||||
}
|
||||
ctx.body = existing[0];
|
||||
return ctx;
|
||||
}).addTo(app);
|
||||
35
src/routes/n5-link/n5-shop/update.ts
Normal file
35
src/routes/n5-link/n5-shop/update.ts
Normal file
@@ -0,0 +1,35 @@
|
||||
import { eq } from 'drizzle-orm';
|
||||
import { app, db, schema } from '@/app.ts';
|
||||
|
||||
app.route({
|
||||
path: 'n5-shop',
|
||||
key: 'update',
|
||||
middleware: ['auth'],
|
||||
description: `更新商品, 参数:
|
||||
id: 商品ID, 必填
|
||||
title: 商品标题, 选填
|
||||
platform: 商品平台, 选填
|
||||
orderLink: 订单链接, 选填
|
||||
description: 商品描述, 选填
|
||||
link: 商品链接, 选填
|
||||
tags: 标签数组, 选填
|
||||
data: 其他商品信息对象, 选填
|
||||
userinfo: 卖家信息, 选填
|
||||
`,
|
||||
}).define(async (ctx) => {
|
||||
const tokenUser = ctx.state.tokenUser;
|
||||
const { id, userId, createdAt, updatedAt, ...rest } = ctx.query.data || {};
|
||||
if (!id) {
|
||||
ctx.throw(400, 'id 参数缺失');
|
||||
}
|
||||
const existing = await db.select().from(schema.n5Shop).where(eq(schema.n5Shop.id, id)).limit(1);
|
||||
if (existing.length === 0) {
|
||||
ctx.throw(404, '商品不存在');
|
||||
}
|
||||
if (existing[0].userId !== tokenUser.id) {
|
||||
ctx.throw(403, '没有权限更新该商品');
|
||||
}
|
||||
const result = await db.update(schema.n5Shop).set({ ...rest }).where(eq(schema.n5Shop.id, id)).returning();
|
||||
ctx.body = result[0];
|
||||
return ctx;
|
||||
}).addTo(app);
|
||||
157
src/routes/n5-link/short-link.ts
Normal file
157
src/routes/n5-link/short-link.ts
Normal file
@@ -0,0 +1,157 @@
|
||||
import { desc, eq, count, or, like, and } from 'drizzle-orm';
|
||||
import { app, db, schema } from '@/app.ts';
|
||||
import { nanoid } from 'nanoid';
|
||||
import z from 'zod';
|
||||
|
||||
// 列表
|
||||
app.route({
|
||||
path: 'n5-short-link',
|
||||
key: 'list',
|
||||
middleware: ['auth'],
|
||||
description: '获取短链列表, 参数: page, pageSize, search, sort',
|
||||
}).define(async (ctx) => {
|
||||
const tokenUser = ctx.state.tokenUser;
|
||||
const uid = tokenUser.id;
|
||||
const { page = 1, pageSize = 20, search, sort = 'DESC' } = ctx.query || {};
|
||||
|
||||
const offset = (page - 1) * pageSize;
|
||||
const orderByField = sort === 'ASC' ? schema.shortLink.updatedAt : desc(schema.shortLink.updatedAt);
|
||||
|
||||
let whereCondition: any = eq(schema.shortLink.userId, uid);
|
||||
if (search) {
|
||||
whereCondition = and(
|
||||
eq(schema.shortLink.userId, uid),
|
||||
or(
|
||||
like(schema.shortLink.title, `%${search}%`),
|
||||
like(schema.shortLink.slug, `%${search}%`),
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
const [list, totalCount] = await Promise.all([
|
||||
db.select().from(schema.shortLink).where(whereCondition).limit(pageSize).offset(offset).orderBy(orderByField),
|
||||
db.select({ count: count() }).from(schema.shortLink).where(whereCondition),
|
||||
]);
|
||||
|
||||
ctx.body = {
|
||||
list,
|
||||
pagination: { page, current: page, pageSize, total: totalCount[0]?.count || 0 },
|
||||
};
|
||||
return ctx;
|
||||
}).addTo(app);
|
||||
|
||||
// 获取单个
|
||||
app.route({
|
||||
path: 'n5-short-link',
|
||||
key: 'get',
|
||||
description: '获取单个短链, 参数: id 或 slug',
|
||||
middleware: ['auth'],
|
||||
}).define(async (ctx) => {
|
||||
const { id, slug } = ctx.query || {};
|
||||
if (!id && !slug) {
|
||||
ctx.throw(400, 'id 或 slug 参数缺失');
|
||||
}
|
||||
const condition = id ? eq(schema.shortLink.id, id) : eq(schema.shortLink.slug, slug);
|
||||
const existing = await db.select().from(schema.shortLink).where(condition).limit(1);
|
||||
if (existing.length === 0) {
|
||||
ctx.throw(404, '短链不存在');
|
||||
}
|
||||
ctx.body = existing[0];
|
||||
return ctx;
|
||||
}).addTo(app);
|
||||
|
||||
// 创建
|
||||
app.route({
|
||||
path: 'n5-short-link',
|
||||
key: 'create',
|
||||
middleware: ['auth'],
|
||||
description: `创建短链`,
|
||||
metadata: {
|
||||
args: {
|
||||
data: z.object({
|
||||
slug: z.string().optional().describe('对外唯一业务ID'),
|
||||
title: z.string().optional().describe('标题'),
|
||||
description: z.string().optional().describe('描述'),
|
||||
tags: z.array(z.string()).optional().describe('标签数组'),
|
||||
data: z.record(z.string(), z.any()).optional().describe('附加数据对象'),
|
||||
type: z.enum(['link', 'agent']).optional().describe('类型'),
|
||||
version: z.string().optional().describe('版本号'),
|
||||
}).describe('创建短链参数对象'),
|
||||
}
|
||||
}
|
||||
}).define(async (ctx) => {
|
||||
const tokenUser = ctx.state.tokenUser;
|
||||
const { userId, createdAt, updatedAt, id: _id, ...rest } = ctx.query.data || {};
|
||||
if (!rest.slug) {
|
||||
ctx.throw(400, 'slug 参数缺失');
|
||||
}
|
||||
const code = rest.code || nanoid(8);
|
||||
const result = await db.insert(schema.shortLink).values({
|
||||
...rest,
|
||||
code,
|
||||
userId: tokenUser.id,
|
||||
}).returning();
|
||||
ctx.body = result[0];
|
||||
return ctx;
|
||||
}).addTo(app);
|
||||
|
||||
// 更新
|
||||
app.route({
|
||||
path: 'n5-short-link',
|
||||
key: 'update',
|
||||
middleware: ['auth'],
|
||||
description: `更新短链`,
|
||||
metadata: {
|
||||
args: {
|
||||
data: z.object({
|
||||
id: z.string().optional().describe('短链ID'),
|
||||
title: z.string().optional().describe('标题'),
|
||||
description: z.string().optional().describe('描述'),
|
||||
tags: z.array(z.string()).optional().describe('标签数组'),
|
||||
data: z.record(z.string(), z.any()).optional().describe('附加数据对象'),
|
||||
type: z.enum(['link', 'agent']).optional().describe('类型'),
|
||||
version: z.string().optional().describe('版本号'),
|
||||
}).describe('更新短链参数对象'),
|
||||
}
|
||||
}
|
||||
}).define(async (ctx) => {
|
||||
const tokenUser = ctx.state.tokenUser;
|
||||
const { id, userId, createdAt, updatedAt, ...rest } = ctx.query.data || {};
|
||||
if (!id) {
|
||||
ctx.throw(400, 'id 参数缺失');
|
||||
}
|
||||
const existing = await db.select().from(schema.shortLink).where(eq(schema.shortLink.id, id)).limit(1);
|
||||
if (existing.length === 0) {
|
||||
ctx.throw(404, '短链不存在');
|
||||
}
|
||||
if (existing[0].userId !== tokenUser.id) {
|
||||
ctx.throw(403, '没有权限更新该短链');
|
||||
}
|
||||
const result = await db.update(schema.shortLink).set({ ...rest }).where(eq(schema.shortLink.id, id)).returning();
|
||||
ctx.body = result[0];
|
||||
return ctx;
|
||||
}).addTo(app);
|
||||
|
||||
// 删除
|
||||
app.route({
|
||||
path: 'n5-short-link',
|
||||
key: 'delete',
|
||||
middleware: ['auth'],
|
||||
description: '删除短链, 参数: id 短链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.shortLink).where(eq(schema.shortLink.id, id)).limit(1);
|
||||
if (existing.length === 0) {
|
||||
ctx.throw(404, '短链不存在');
|
||||
}
|
||||
if (existing[0].userId !== tokenUser.id) {
|
||||
ctx.throw(403, '没有权限删除该短链');
|
||||
}
|
||||
await db.delete(schema.shortLink).where(eq(schema.shortLink.id, id));
|
||||
ctx.body = { success: true };
|
||||
return ctx;
|
||||
}).addTo(app);
|
||||
Reference in New Issue
Block a user