26 lines
766 B
TypeScript
26 lines
766 B
TypeScript
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);
|