商品删除功能(需要二次确认)
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { Body, Controller, Get, Param, Post, Put, Query, UseGuards } from '@nestjs/common';
|
||||
import { Body, Controller, Delete, Get, Param, Post, Put, Query, UseGuards } from '@nestjs/common';
|
||||
import { HqAuthGuard } from '../../common/guards/hq-auth.guard';
|
||||
import { AdminProductsService } from './admin-products.service';
|
||||
import { AdminProductsQueryDto } from './dto/admin-query.dto';
|
||||
@@ -28,4 +28,9 @@ export class AdminProductsController {
|
||||
update(@Param('id') id: string, @Body() dto: UpdateProductDto) {
|
||||
return this.service.update(BigInt(id), dto);
|
||||
}
|
||||
|
||||
@Delete(':id')
|
||||
remove(@Param('id') id: string) {
|
||||
return this.service.remove(BigInt(id));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -135,6 +135,23 @@ export class AdminProductsService {
|
||||
return this.detail(id);
|
||||
}
|
||||
|
||||
async remove(id: bigint) {
|
||||
const product = await this.prisma.commonProductItem.findUnique({ where: { id } });
|
||||
if (!product) throw new NotFoundException('商品不存在');
|
||||
|
||||
const orderCount = await this.prisma.order.count({ where: { productId: id } });
|
||||
if (orderCount > 0) {
|
||||
throw new BadRequestException(`该商品已有 ${orderCount} 笔关联订单,无法删除`);
|
||||
}
|
||||
|
||||
await this.prisma.commonResource.deleteMany({
|
||||
where: { ownerType: 'PRODUCT', ownerId: id },
|
||||
});
|
||||
await this.prisma.commonProductItem.delete({ where: { id } });
|
||||
|
||||
return { ok: true };
|
||||
}
|
||||
|
||||
private formatProduct(
|
||||
product: Prisma.CommonProductItemGetPayload<{ include: { coverResource: true } }>,
|
||||
extraResources: Prisma.CommonResourceGetPayload<object>[],
|
||||
|
||||
Reference in New Issue
Block a user