Compare commits
2
Commits
fce8ada51e
...
79255480bc
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
79255480bc | ||
|
|
e0cf2c00d6 |
No files matched your search
@@ -4,16 +4,21 @@ import {
|
|||||||
ArgumentsHost,
|
ArgumentsHost,
|
||||||
HttpException,
|
HttpException,
|
||||||
HttpStatus,
|
HttpStatus,
|
||||||
|
Logger,
|
||||||
} from '@nestjs/common';
|
} from '@nestjs/common';
|
||||||
import { ErrorResponse } from '../error/error.response';
|
import { ErrorResponse } from '../error/error.response';
|
||||||
|
import * as process from 'node:process';
|
||||||
|
|
||||||
@Catch()
|
@Catch()
|
||||||
export class GlobalExceptionFilter implements ExceptionFilter {
|
export class GlobalExceptionFilter implements ExceptionFilter {
|
||||||
|
private readonly logger = new Logger(GlobalExceptionFilter.name); // 使用 Logger
|
||||||
|
|
||||||
catch(exception: any, host: ArgumentsHost) {
|
catch(exception: any, host: ArgumentsHost) {
|
||||||
const ctx = host.switchToHttp();
|
const ctx = host.switchToHttp();
|
||||||
const response = ctx.getResponse();
|
const response = ctx.getResponse();
|
||||||
|
|
||||||
const { status, errorResponse } = this.formatException(exception);
|
const { status, errorResponse } = this.formatException(exception);
|
||||||
|
this.logError(exception);
|
||||||
|
|
||||||
response.status(status).json(errorResponse);
|
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 { IsNotEmpty, IsOptional } from 'class-validator';
|
||||||
import { ApiProperty } from '@nestjs/swagger';
|
import { ApiProperty } from '@nestjs/swagger';
|
||||||
|
import { ProductCategory } from '@/pet-feeder/enum/product-category';
|
||||||
|
|
||||||
export class CreateProductRequest {
|
export class CreateProductRequest {
|
||||||
@IsNotEmpty({
|
@IsNotEmpty({
|
||||||
@@ -8,6 +9,12 @@ export class CreateProductRequest {
|
|||||||
@ApiProperty()
|
@ApiProperty()
|
||||||
name: string;
|
name: string;
|
||||||
|
|
||||||
|
@IsNotEmpty({
|
||||||
|
message: '产品分类不能为空',
|
||||||
|
})
|
||||||
|
@ApiProperty()
|
||||||
|
category: ProductCategory;
|
||||||
|
|
||||||
@IsNotEmpty({
|
@IsNotEmpty({
|
||||||
message: '产品图片不能为空',
|
message: '产品图片不能为空',
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -1,3 +1,7 @@
|
|||||||
import { BaseFilter } from '@/core/dto/base.filter';
|
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'];
|
||||||
|
}
|
||||||
@@ -1,12 +1,20 @@
|
|||||||
import { BaseEntity } from '@/core/entity/base.entity';
|
import { BaseEntity } from '@/core/entity/base.entity';
|
||||||
import { Column, Entity, OneToMany } from 'typeorm';
|
import { Column, Entity, OneToMany } from 'typeorm';
|
||||||
import { FeedingPlanProduct } from '@/pet-feeder/entity/feeding-plan-product.entity';
|
import { FeedingPlanProduct } from '@/pet-feeder/entity/feeding-plan-product.entity';
|
||||||
|
import { ProductCategory } from '@/pet-feeder/enum/product-category';
|
||||||
|
|
||||||
@Entity()
|
@Entity()
|
||||||
export class Product extends BaseEntity {
|
export class Product extends BaseEntity {
|
||||||
@Column()
|
@Column()
|
||||||
name: string;
|
name: string;
|
||||||
|
|
||||||
|
@Column({
|
||||||
|
type: 'enum',
|
||||||
|
enum: ProductCategory,
|
||||||
|
default: ProductCategory.DRY_FOOD,
|
||||||
|
})
|
||||||
|
category: ProductCategory;
|
||||||
|
|
||||||
@Column()
|
@Column()
|
||||||
photo: string;
|
photo: string;
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,9 @@
|
|||||||
|
export enum ProductCategory {
|
||||||
|
DRY_FOOD,
|
||||||
|
WET_FOOD,
|
||||||
|
}
|
||||||
|
|
||||||
|
export const productCategoryLabels: Record<ProductCategory, string> = {
|
||||||
|
[ProductCategory.DRY_FOOD]: '干粮',
|
||||||
|
[ProductCategory.WET_FOOD]: '湿粮',
|
||||||
|
};
|
||||||
@@ -2,6 +2,7 @@ import { BaseMapper } from '@/core/mapper/base.mapper';
|
|||||||
import { Product } from '@/pet-feeder/entity/product.entity';
|
import { Product } from '@/pet-feeder/entity/product.entity';
|
||||||
import { ProductVo } from '@/pet-feeder/vo/product.vo';
|
import { ProductVo } from '@/pet-feeder/vo/product.vo';
|
||||||
import { Injectable } from '@nestjs/common';
|
import { Injectable } from '@nestjs/common';
|
||||||
|
import { productCategoryLabels } from '@/pet-feeder/enum/product-category';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class ProductMapper extends BaseMapper<Product, ProductVo> {
|
export class ProductMapper extends BaseMapper<Product, ProductVo> {
|
||||||
@@ -9,6 +10,7 @@ export class ProductMapper extends BaseMapper<Product, ProductVo> {
|
|||||||
return {
|
return {
|
||||||
...super.toVo(entity),
|
...super.toVo(entity),
|
||||||
name: entity.name,
|
name: entity.name,
|
||||||
|
category: productCategoryLabels[entity.category],
|
||||||
photo: entity.photo,
|
photo: entity.photo,
|
||||||
tags: entity.tags,
|
tags: entity.tags,
|
||||||
totalCalories: entity.totalCalories,
|
totalCalories: entity.totalCalories,
|
||||||
|
|||||||
@@ -28,6 +28,7 @@ export class ProductService {
|
|||||||
): Promise<Product> {
|
): Promise<Product> {
|
||||||
const product = await this.get(id);
|
const product = await this.get(id);
|
||||||
product.name = updateProductRequest.name;
|
product.name = updateProductRequest.name;
|
||||||
|
product.category = updateProductRequest.category;
|
||||||
product.tags = updateProductRequest.tags;
|
product.tags = updateProductRequest.tags;
|
||||||
product.photo = updateProductRequest.photo;
|
product.photo = updateProductRequest.photo;
|
||||||
product.totalCalories = updateProductRequest.totalCalories;
|
product.totalCalories = updateProductRequest.totalCalories;
|
||||||
|
|||||||
@@ -1,5 +1,9 @@
|
|||||||
import { BaseVo } from '@/core/vo/base.vo';
|
import { BaseVo } from '@/core/vo/base.vo';
|
||||||
import { ApiProperty, ApiSchema } from '@nestjs/swagger';
|
import { ApiProperty, ApiSchema } from '@nestjs/swagger';
|
||||||
|
import {
|
||||||
|
ProductCategory,
|
||||||
|
productCategoryLabels,
|
||||||
|
} from '@/pet-feeder/enum/product-category';
|
||||||
|
|
||||||
@ApiSchema({
|
@ApiSchema({
|
||||||
name: 'Product',
|
name: 'Product',
|
||||||
@@ -7,6 +11,12 @@ import { ApiProperty, ApiSchema } from '@nestjs/swagger';
|
|||||||
export class ProductVo extends BaseVo {
|
export class ProductVo extends BaseVo {
|
||||||
@ApiProperty()
|
@ApiProperty()
|
||||||
name: string;
|
name: string;
|
||||||
|
|
||||||
|
@ApiProperty({
|
||||||
|
example: [ProductCategory.DRY_FOOD, ProductCategory.WET_FOOD],
|
||||||
|
})
|
||||||
|
category: string;
|
||||||
|
|
||||||
@ApiProperty()
|
@ApiProperty()
|
||||||
photo: string;
|
photo: string;
|
||||||
@ApiProperty()
|
@ApiProperty()
|
||||||
|
|||||||
Reference in new issue
Block a user