新增训练计划模块,包括控制器、服务、模型及数据传输对象,更新应用模块以引入新模块,同时在AI教练模块中添加体态评估功能,支持体重识别与更新,优化用户体重历史记录管理。
This commit is contained in:
53
src/training-plans/training-plans.service.ts
Normal file
53
src/training-plans/training-plans.service.ts
Normal file
@@ -0,0 +1,53 @@
|
||||
import { Injectable, NotFoundException } from '@nestjs/common';
|
||||
import { InjectModel } from '@nestjs/sequelize';
|
||||
import { TrainingPlan } from './models/training-plan.model';
|
||||
import { CreateTrainingPlanDto } from './dto/training-plan.dto';
|
||||
|
||||
@Injectable()
|
||||
export class TrainingPlansService {
|
||||
constructor(
|
||||
@InjectModel(TrainingPlan)
|
||||
private trainingPlanModel: typeof TrainingPlan,
|
||||
) { }
|
||||
|
||||
async create(userId: string, dto: CreateTrainingPlanDto) {
|
||||
const id = `plan_${Date.now()}_${Math.random().toString(36).slice(2, 8)}`;
|
||||
const createdAt = new Date();
|
||||
const plan = await this.trainingPlanModel.create({
|
||||
id,
|
||||
userId,
|
||||
createdAt,
|
||||
startDate: new Date(dto.startDate),
|
||||
mode: dto.mode,
|
||||
daysOfWeek: dto.daysOfWeek,
|
||||
sessionsPerWeek: dto.sessionsPerWeek,
|
||||
goal: dto.goal,
|
||||
startWeightKg: dto.startWeightKg ?? null,
|
||||
preferredTimeOfDay: dto.preferredTimeOfDay ?? '',
|
||||
});
|
||||
return plan.toJSON();
|
||||
}
|
||||
|
||||
async remove(userId: string, id: string) {
|
||||
const count = await this.trainingPlanModel.destroy({ where: { id, userId } });
|
||||
return { success: count > 0 };
|
||||
}
|
||||
|
||||
async list(userId: string) {
|
||||
const rows = await this.trainingPlanModel.findAll({ where: { userId }, order: [['created_at', 'DESC']] });
|
||||
return rows.map(r => ({
|
||||
id: r.id,
|
||||
createdAt: r.createdAt,
|
||||
startDate: r.startDate,
|
||||
goal: r.goal,
|
||||
}));
|
||||
}
|
||||
|
||||
async detail(userId: string, id: string) {
|
||||
const plan = await this.trainingPlanModel.findOne({ where: { id, userId } });
|
||||
if (!plan) throw new NotFoundException('训练计划不存在');
|
||||
return plan.toJSON();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user