API/src/controller/product.controller.ts

410 lines
10 KiB
TypeScript

import {
Inject,
Post,
Put,
Get,
Body,
Param,
Del,
Query,
Controller,
} from '@midwayjs/core';
import { ProductService } from '../service/product.service';
import { errorResponse, successResponse } from '../utils/response.util';
import {
BatchSetSkuDTO,
CreateBrandDTO,
CreateFlavorsDTO,
CreateProductDTO,
CreateStrengthDTO,
QueryBrandDTO,
QueryFlavorsDTO,
QueryProductDTO,
QueryStrengthDTO,
UpdateBrandDTO,
UpdateFlavorsDTO,
UpdateProductDTO,
UpdateStrengthDTO,
} from '../dto/product.dto';
import { ApiOkResponse } from '@midwayjs/swagger';
import {
BooleanRes,
ProductBrandListRes,
ProductBrandRes,
ProductListRes,
ProductRes,
ProductsRes,
} from '../dto/reponse.dto';
@Controller('/product')
export class ProductController {
@Inject()
productService: ProductService;
ProductRes;
@ApiOkResponse({
description: '通过name搜索产品',
type: ProductsRes,
})
@Get('/search')
async searchProducts(@Query('name') name: string) {
try {
// 调用服务获取产品数据
const products = await this.productService.findProductsByName(name);
return successResponse(products);
} catch (error) {
return errorResponse(error.message || '获取数据失败');
}
}
@ApiOkResponse({
type: ProductRes,
})
@Get('/sku/:sku')
async productBySku(@Param('sku') sku: string) {
try {
// 调用服务获取产品数据
const product = await this.productService.findProductBySku(sku);
return successResponse(product);
} catch (error) {
return errorResponse(error.message || '获取数据失败');
}
}
@ApiOkResponse({
description: '成功返回产品列表',
type: ProductListRes,
})
@Get('/list')
async getProductList(
@Query() query: QueryProductDTO
): Promise<ProductListRes> {
const { current = 1, pageSize = 10, name, brandId } = query;
try {
const data = await this.productService.getProductList(
{ current, pageSize },
name,
brandId
);
return successResponse(data);
} catch (error) {
console.log(error);
return errorResponse(error?.message || error);
}
}
@ApiOkResponse({
type: ProductRes,
})
@Post('/')
async createProduct(@Body() productData: CreateProductDTO) {
try {
const data = this.productService.createProduct(productData);
return successResponse(data);
} catch (error) {
return errorResponse(error?.message || error);
}
}
@ApiOkResponse({
type: ProductRes,
})
@Put('/:id')
async updateProduct(
@Param('id') id: number,
@Body() productData: UpdateProductDTO
) {
try {
const data = this.productService.updateProduct(id, productData);
return successResponse(data);
} catch (error) {
return errorResponse(error?.message || error);
}
}
@ApiOkResponse({
type: ProductRes,
})
@Put('updateNameCn/:id/:nameCn')
async updateProductNameCn(
@Param('id') id: number,
@Param('nameCn') nameCn: string
) {
try {
const data = this.productService.updateProductNameCn(id, nameCn);
return successResponse(data);
} catch (error) {
return errorResponse(error?.message || error);
}
}
@ApiOkResponse({
type: BooleanRes,
})
@Del('/:id')
async deleteProduct(@Param('id') id: number) {
try {
const data = await this.productService.deleteProduct(id);
return successResponse(data);
} catch (error) {
return errorResponse(error?.message || error);
}
}
@ApiOkResponse({
type: ProductBrandListRes,
})
@Get('/brands')
async getBrands(@Query() query: QueryBrandDTO) {
const { current = 1, pageSize = 10, name } = query;
try {
let data = await this.productService.getBrandList(
{ current, pageSize },
name
);
return successResponse(data);
} catch (error) {
return errorResponse(error?.message || error);
}
}
@ApiOkResponse()
@Get('/brandAll')
async getBrandAll() {
try {
let data = await this.productService.getBrandAll();
return successResponse(data);
} catch (error) {
return errorResponse(error?.message || error);
}
}
@ApiOkResponse({
type: ProductBrandRes,
})
@Post('/brand')
async createBrand(@Body() brandData: CreateBrandDTO) {
try {
const hasBrand = await this.productService.hasAttribute(
'brand',
brandData.name
);
if (hasBrand) {
return errorResponse('品牌已存在');
}
let data = await this.productService.createBrand(brandData);
return successResponse(data);
} catch (error) {
return errorResponse(error?.message || error);
}
}
@ApiOkResponse({
type: ProductBrandRes,
})
@Put('/brand/:id')
async updateBrand(
@Param('id') id: number,
@Body() brandData: UpdateBrandDTO
) {
try {
const hasBrand = await this.productService.hasAttribute(
'brand',
brandData.name,
id
);
if (hasBrand) {
return errorResponse('品牌已存在');
}
const data = this.productService.updateBrand(id, brandData);
return successResponse(data);
} catch (error) {
return errorResponse(error?.message || error);
}
}
@ApiOkResponse({
type: BooleanRes,
})
@Del('/brand/:id')
async deleteBrand(@Param('id') id: number) {
try {
const hasProducts = await this.productService.hasProductsInAttribute(id);
if (hasProducts) throw new Error('该品牌下有商品,无法删除');
const data = await this.productService.deleteBrand(id);
return successResponse(data);
} catch (error) {
return errorResponse(error?.message || error);
}
}
@Post('/batchSetSku')
@ApiOkResponse({
description: '批量设置 sku 的响应结果',
type: BooleanRes,
})
async batchSetSku(@Body() body: BatchSetSkuDTO) {
try {
const result = await this.productService.batchSetSku(body.skus);
return successResponse(result, '批量设置 sku 成功');
} catch (error) {
return errorResponse(error.message, 400);
}
}
@ApiOkResponse()
@Get('/flavorsAll')
async getFlavorsAll() {
try {
let data = await this.productService.getFlavorsAll();
return successResponse(data);
} catch (error) {
return errorResponse(error?.message || error);
}
}
@ApiOkResponse()
@Get('/flavors')
async getFlavors(@Query() query: QueryFlavorsDTO) {
const { current = 1, pageSize = 10, name } = query;
try {
let data = await this.productService.getFlavorsList(
{ current, pageSize },
name
);
return successResponse(data);
} catch (error) {
return errorResponse(error?.message || error);
}
}
@ApiOkResponse()
@Post('/flavors')
async createFlavors(@Body() flavorsData: CreateFlavorsDTO) {
try {
const hasFlavors = await this.productService.hasAttribute('flavor', flavorsData.name);
if (hasFlavors) {
return errorResponse('口味已存在');
}
let data = await this.productService.createFlavors(flavorsData);
return successResponse(data);
} catch (error) {
return errorResponse(error?.message || error);
}
}
@ApiOkResponse()
@Put('/flavors/:id')
async updateFlavors(
@Param('id') id: number,
@Body() flavorsData: UpdateFlavorsDTO
) {
try {
const hasFlavors = await this.productService.hasAttribute('flavor', flavorsData.name, id);
if (hasFlavors) {
return errorResponse('口味已存在');
}
const data = this.productService.updateFlavors(id, flavorsData);
return successResponse(data);
} catch (error) {
return errorResponse(error?.message || error);
}
}
@ApiOkResponse({
type: BooleanRes,
})
@Del('/flavors/:id')
async deleteFlavors(@Param('id') id: number) {
try {
const hasProducts = await this.productService.hasProductsInAttribute(id);
if (hasProducts) throw new Error('该口味下有商品,无法删除');
const data = await this.productService.deleteFlavors(id);
return successResponse(data);
} catch (error) {
return errorResponse(error?.message || error);
}
}
@ApiOkResponse()
@Get('/strengthAll')
async getStrengthAll() {
try {
let data = await this.productService.getStrengthAll();
return successResponse(data);
} catch (error) {
return errorResponse(error?.message || error);
}
}
@ApiOkResponse()
@Get('/strength')
async getStrength(@Query() query: QueryStrengthDTO) {
const { current = 1, pageSize = 10, name } = query;
try {
let data = await this.productService.getStrengthList(
{ current, pageSize },
name
);
return successResponse(data);
} catch (error) {
return errorResponse(error?.message || error);
}
}
@ApiOkResponse()
@Post('/strength')
async createStrength(@Body() strengthData: CreateStrengthDTO) {
try {
const hasStrength = await this.productService.hasAttribute(
'strength',
strengthData.name
);
if (hasStrength) {
return errorResponse('规格已存在');
}
let data = await this.productService.createStrength(strengthData);
return successResponse(data);
} catch (error) {
return errorResponse(error?.message || error);
}
}
@ApiOkResponse()
@Put('/strength/:id')
async updateStrength(
@Param('id') id: number,
@Body() strengthData: UpdateStrengthDTO
) {
try {
const hasStrength = await this.productService.hasAttribute(
'strength',
strengthData.name,
id
);
if (hasStrength) {
return errorResponse('规格已存在');
}
const data = this.productService.updateStrength(id, strengthData);
return successResponse(data);
} catch (error) {
return errorResponse(error?.message || error);
}
}
@ApiOkResponse({
type: BooleanRes,
})
@Del('/strength/:id')
async deleteStrength(@Param('id') id: number) {
try {
const hasProducts = await this.productService.hasProductsInAttribute(id);
if (hasProducts) throw new Error('该规格下有商品,无法删除');
const data = await this.productService.deleteStrength(id);
return successResponse(data);
} catch (error) {
return errorResponse(error?.message || error);
}
}
}