修复GET请求查询参数验证装饰器缺失问题,添加正确的class-validator装饰器 在控制器中实现查询参数类型转换,确保数字参数正确处理 更新技术文档,添加DTO验证装饰器编写规范和GET请求参数处理指南
125 lines
3.2 KiB
TypeScript
125 lines
3.2 KiB
TypeScript
import { ApiProperty } from '@nestjs/swagger';
|
|
import { IsOptional, IsDateString, IsNumber, IsString } from 'class-validator';
|
|
import { ApiResponseDto } from '../../base.dto';
|
|
|
|
/**
|
|
* 营养成分分析记录项DTO
|
|
*/
|
|
export class NutritionAnalysisRecordDto {
|
|
@ApiProperty({ description: '记录ID', example: 1 })
|
|
id: number;
|
|
|
|
@ApiProperty({ description: '用户ID', example: 'user123' })
|
|
userId: string;
|
|
|
|
@ApiProperty({ description: '分析图片URL', example: 'https://example.com/nutrition-label.jpg' })
|
|
imageUrl: string;
|
|
|
|
@ApiProperty({ description: '营养成分分析结果' })
|
|
analysisResult: any;
|
|
|
|
@ApiProperty({ description: '分析状态', example: 'success' })
|
|
status: string;
|
|
|
|
@ApiProperty({ description: '分析消息', example: '分析成功' })
|
|
message: string;
|
|
|
|
@ApiProperty({ description: 'AI模型提供商', example: 'dashscope' })
|
|
aiProvider: string;
|
|
|
|
@ApiProperty({ description: '使用的AI模型', example: 'qwen-vl-max' })
|
|
aiModel: string;
|
|
|
|
@ApiProperty({ description: '识别到的营养素数量', example: 15 })
|
|
nutritionCount: number;
|
|
|
|
@ApiProperty({ description: '创建时间' })
|
|
createdAt: Date;
|
|
|
|
@ApiProperty({ description: '更新时间' })
|
|
updatedAt: Date;
|
|
}
|
|
|
|
/**
|
|
* 营养成分分析记录列表响应DTO
|
|
*/
|
|
export class NutritionAnalysisRecordsResponseDto extends ApiResponseDto<{
|
|
records: NutritionAnalysisRecordDto[];
|
|
total: number;
|
|
page: number;
|
|
limit: number;
|
|
totalPages: number;
|
|
}> {
|
|
constructor(code: number, message: string, data: {
|
|
records: NutritionAnalysisRecordDto[];
|
|
total: number;
|
|
page: number;
|
|
limit: number;
|
|
totalPages: number;
|
|
}) {
|
|
super(code, message, data);
|
|
}
|
|
|
|
/**
|
|
* 创建成功响应
|
|
*/
|
|
static createSuccess(
|
|
records: NutritionAnalysisRecordDto[],
|
|
total: number,
|
|
page: number,
|
|
limit: number,
|
|
message: string = '获取营养分析记录成功'
|
|
): NutritionAnalysisRecordsResponseDto {
|
|
const totalPages = Math.ceil(total / limit);
|
|
return new NutritionAnalysisRecordsResponseDto(0, message, {
|
|
records,
|
|
total,
|
|
page,
|
|
limit,
|
|
totalPages,
|
|
});
|
|
}
|
|
|
|
/**
|
|
* 创建失败响应
|
|
*/
|
|
static createError(message: string = '获取营养分析记录失败'): NutritionAnalysisRecordsResponseDto {
|
|
return new NutritionAnalysisRecordsResponseDto(1, message, {
|
|
records: [],
|
|
total: 0,
|
|
page: 1,
|
|
limit: 20,
|
|
totalPages: 0,
|
|
});
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 查询营养分析记录请求DTO
|
|
*/
|
|
export class GetNutritionAnalysisRecordsQueryDto {
|
|
@ApiProperty({ description: '页码', example: 1, required: false })
|
|
@IsOptional()
|
|
@IsString()
|
|
page?: string;
|
|
|
|
@ApiProperty({ description: '每页数量', example: 20, required: false })
|
|
@IsOptional()
|
|
@IsString()
|
|
limit?: string;
|
|
|
|
@ApiProperty({ description: '开始日期', example: '2023-01-01', required: false })
|
|
@IsOptional()
|
|
@IsDateString()
|
|
startDate?: string;
|
|
|
|
@ApiProperty({ description: '结束日期', example: '2023-12-31', required: false })
|
|
@IsOptional()
|
|
@IsDateString()
|
|
endDate?: string;
|
|
|
|
@ApiProperty({ description: '分析状态', example: 'success', required: false })
|
|
@IsOptional()
|
|
@IsString()
|
|
status?: string;
|
|
} |