feat: 新增饮食记录模块,含增删改查及营养汇总功能
This commit is contained in:
124
src/diet-records/diet-records.controller.ts
Normal file
124
src/diet-records/diet-records.controller.ts
Normal file
@@ -0,0 +1,124 @@
|
||||
import {
|
||||
Controller,
|
||||
Get,
|
||||
Post,
|
||||
Body,
|
||||
Param,
|
||||
HttpCode,
|
||||
HttpStatus,
|
||||
Put,
|
||||
Delete,
|
||||
Query,
|
||||
Logger,
|
||||
UseGuards,
|
||||
NotFoundException,
|
||||
} from '@nestjs/common';
|
||||
import { ApiOperation, ApiBody, ApiResponse, ApiTags, ApiQuery } from '@nestjs/swagger';
|
||||
import { DietRecordsService } from './diet-records.service';
|
||||
import { CreateDietRecordDto, UpdateDietRecordDto, GetDietHistoryQueryDto, DietRecordResponseDto, DietHistoryResponseDto, NutritionSummaryDto } from '../users/dto/diet-record.dto';
|
||||
import { JwtAuthGuard } from '../common/guards/jwt-auth.guard';
|
||||
import { CurrentUser } from '../common/decorators/current-user.decorator';
|
||||
import { AccessTokenPayload } from '../users/services/apple-auth.service';
|
||||
|
||||
@ApiTags('diet-records')
|
||||
@Controller('diet-records')
|
||||
export class DietRecordsController {
|
||||
private readonly logger = new Logger(DietRecordsController.name);
|
||||
|
||||
constructor(
|
||||
private readonly dietRecordsService: DietRecordsService,
|
||||
) { }
|
||||
|
||||
/**
|
||||
* 添加饮食记录
|
||||
*/
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Post()
|
||||
@HttpCode(HttpStatus.CREATED)
|
||||
@ApiOperation({ summary: '添加饮食记录' })
|
||||
@ApiBody({ type: CreateDietRecordDto })
|
||||
@ApiResponse({ status: 201, description: '成功添加饮食记录', type: DietRecordResponseDto })
|
||||
async addDietRecord(
|
||||
@Body() createDto: CreateDietRecordDto,
|
||||
@CurrentUser() user: AccessTokenPayload,
|
||||
): Promise<DietRecordResponseDto> {
|
||||
this.logger.log(`添加饮食记录 - 用户ID: ${user.sub}, 食物: ${createDto.foodName}`);
|
||||
return this.dietRecordsService.addDietRecord(user.sub, createDto);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取饮食记录历史
|
||||
*/
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Get()
|
||||
@HttpCode(HttpStatus.OK)
|
||||
@ApiOperation({ summary: '获取饮食记录历史' })
|
||||
@ApiQuery({ name: 'startDate', required: false, description: '开始日期' })
|
||||
@ApiQuery({ name: 'endDate', required: false, description: '结束日期' })
|
||||
@ApiQuery({ name: 'mealType', required: false, description: '餐次类型' })
|
||||
@ApiQuery({ name: 'page', required: false, description: '页码' })
|
||||
@ApiQuery({ name: 'limit', required: false, description: '每页数量' })
|
||||
@ApiResponse({ status: 200, description: '成功获取饮食记录', type: DietHistoryResponseDto })
|
||||
async getDietHistory(
|
||||
@Query() query: GetDietHistoryQueryDto,
|
||||
@CurrentUser() user: AccessTokenPayload,
|
||||
): Promise<DietHistoryResponseDto> {
|
||||
this.logger.log(`获取饮食记录 - 用户ID: ${user.sub}`);
|
||||
return this.dietRecordsService.getDietHistory(user.sub, query);
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新饮食记录
|
||||
*/
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Put(':id')
|
||||
@HttpCode(HttpStatus.OK)
|
||||
@ApiOperation({ summary: '更新饮食记录' })
|
||||
@ApiBody({ type: UpdateDietRecordDto })
|
||||
@ApiResponse({ status: 200, description: '成功更新饮食记录', type: DietRecordResponseDto })
|
||||
async updateDietRecord(
|
||||
@Param('id') recordId: string,
|
||||
@Body() updateDto: UpdateDietRecordDto,
|
||||
@CurrentUser() user: AccessTokenPayload,
|
||||
): Promise<DietRecordResponseDto> {
|
||||
this.logger.log(`更新饮食记录 - 用户ID: ${user.sub}, 记录ID: ${recordId}`);
|
||||
return this.dietRecordsService.updateDietRecord(user.sub, parseInt(recordId), updateDto);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除饮食记录
|
||||
*/
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Delete(':id')
|
||||
@HttpCode(HttpStatus.NO_CONTENT)
|
||||
@ApiOperation({ summary: '删除饮食记录' })
|
||||
@ApiResponse({ status: 204, description: '成功删除饮食记录' })
|
||||
async deleteDietRecord(
|
||||
@Param('id') recordId: string,
|
||||
@CurrentUser() user: AccessTokenPayload,
|
||||
): Promise<void> {
|
||||
this.logger.log(`删除饮食记录 - 用户ID: ${user.sub}, 记录ID: ${recordId}`);
|
||||
const success = await this.dietRecordsService.deleteDietRecord(user.sub, parseInt(recordId));
|
||||
if (!success) {
|
||||
throw new NotFoundException('饮食记录不存在');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取营养汇总分析
|
||||
*/
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Get('nutrition-summary')
|
||||
@HttpCode(HttpStatus.OK)
|
||||
@ApiOperation({ summary: '获取营养汇总分析' })
|
||||
@ApiQuery({ name: 'mealCount', required: false, description: '分析的餐次数量,默认10' })
|
||||
@ApiResponse({ status: 200, description: '成功获取营养汇总', type: NutritionSummaryDto })
|
||||
async getNutritionSummary(
|
||||
@Query('mealCount') mealCount: string,
|
||||
@CurrentUser() user: AccessTokenPayload,
|
||||
): Promise<NutritionSummaryDto> {
|
||||
this.logger.log(`获取营养汇总 - 用户ID: ${user.sub}`);
|
||||
const count = mealCount ? parseInt(mealCount) : 10;
|
||||
return this.dietRecordsService.getRecentNutritionSummary(user.sub, count);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user