Compare commits

...
2 Commits
Author SHA1 Message Date
ilhamtahir 79255480bc feat: 产品管理相关接口 2025-01-19 22:05:29 +08:00
ilhamtahir e0cf2c00d6 fix: 增加错误打印 2025-01-19 20:26:51 +08:00
8 changed files with 57 additions and 1 deletions

No files matched your search

@@ -4,16 +4,21 @@ import {
ArgumentsHost,
HttpException,
HttpStatus,
Logger,
} from '@nestjs/common';
import { ErrorResponse } from '../error/error.response';
import * as process from 'node:process';
@Catch()
export class GlobalExceptionFilter implements ExceptionFilter {
private readonly logger = new Logger(GlobalExceptionFilter.name); // 使用 Logger
catch(exception: any, host: ArgumentsHost) {
const ctx = host.switchToHttp();
const response = ctx.getResponse();
const { status, errorResponse } = this.formatException(exception);
this.logError(exception);
response.status(status).json(errorResponse);
}
@@ -68,4 +73,14 @@ export class GlobalExceptionFilter implements ExceptionFilter {
},
};
}
private logError(exception: any): void {
// 记录错误信息
this.logger.error(`Error: ${exception.message}`);
// 仅在开发或测试环境中打印堆栈跟踪
if (['development', 'test'].includes(process.env.NODE_ENV)) {
this.logger.error(`Stack Trace: ${exception.stack}`);
}
}
}
@@ -1,5 +1,6 @@
import { IsNotEmpty, IsOptional } from 'class-validator';
import { ApiProperty } from '@nestjs/swagger';
import { ProductCategory } from '@/pet-feeder/enum/product-category';
export class CreateProductRequest {
@IsNotEmpty({
@@ -8,6 +9,12 @@ export class CreateProductRequest {
@ApiProperty()
name: string;
@IsNotEmpty({
message: '产品分类不能为空',
})
@ApiProperty()
category: ProductCategory;
@IsNotEmpty({
message: '产品图片不能为空',
})
+5 -1
View File
@@ -1,3 +1,7 @@
import { BaseFilter } from '@/core/dto/base.filter';
import { ApiProperty } from '@nestjs/swagger';
export class SearchProductFilter extends BaseFilter {}
export class SearchProductFilter extends BaseFilter {
@ApiProperty()
order = ['-createdTime'];
}
+8
View File
@@ -1,12 +1,20 @@
import { BaseEntity } from '@/core/entity/base.entity';
import { Column, Entity, OneToMany } from 'typeorm';
import { FeedingPlanProduct } from '@/pet-feeder/entity/feeding-plan-product.entity';
import { ProductCategory } from '@/pet-feeder/enum/product-category';
@Entity()
export class Product extends BaseEntity {
@Column()
name: string;
@Column({
type: 'enum',
enum: ProductCategory,
default: ProductCategory.DRY_FOOD,
})
category: ProductCategory;
@Column()
photo: string;
+9
View File
@@ -0,0 +1,9 @@
export enum ProductCategory {
DRY_FOOD,
WET_FOOD,
}
export const productCategoryLabels: Record<ProductCategory, string> = {
[ProductCategory.DRY_FOOD]: '干粮',
[ProductCategory.WET_FOOD]: '湿粮',
};
+2
View File
@@ -2,6 +2,7 @@ import { BaseMapper } from '@/core/mapper/base.mapper';
import { Product } from '@/pet-feeder/entity/product.entity';
import { ProductVo } from '@/pet-feeder/vo/product.vo';
import { Injectable } from '@nestjs/common';
import { productCategoryLabels } from '@/pet-feeder/enum/product-category';
@Injectable()
export class ProductMapper extends BaseMapper<Product, ProductVo> {
@@ -9,6 +10,7 @@ export class ProductMapper extends BaseMapper<Product, ProductVo> {
return {
...super.toVo(entity),
name: entity.name,
category: productCategoryLabels[entity.category],
photo: entity.photo,
tags: entity.tags,
totalCalories: entity.totalCalories,
@@ -28,6 +28,7 @@ export class ProductService {
): Promise<Product> {
const product = await this.get(id);
product.name = updateProductRequest.name;
product.category = updateProductRequest.category;
product.tags = updateProductRequest.tags;
product.photo = updateProductRequest.photo;
product.totalCalories = updateProductRequest.totalCalories;
+10
View File
@@ -1,5 +1,9 @@
import { BaseVo } from '@/core/vo/base.vo';
import { ApiProperty, ApiSchema } from '@nestjs/swagger';
import {
ProductCategory,
productCategoryLabels,
} from '@/pet-feeder/enum/product-category';
@ApiSchema({
name: 'Product',
@@ -7,6 +11,12 @@ import { ApiProperty, ApiSchema } from '@nestjs/swagger';
export class ProductVo extends BaseVo {
@ApiProperty()
name: string;
@ApiProperty({
example: [ProductCategory.DRY_FOOD, ProductCategory.WET_FOOD],
})
category: string;
@ApiProperty()
photo: string;
@ApiProperty()