feat(training-plans): 添加训练计划名称字段并实现软删除功能
- 在 DTO 和模型中新增 name 字段,支持可选的计划名称 - 实现分页查询功能,优化列表接口返回结构 - 将删除操作改为软删除,新增 deleted 字段控制 - 更新服务逻辑以支持新字段和分页参数
This commit is contained in:
@@ -19,6 +19,7 @@ export class TrainingPlansService {
|
||||
const plan = await this.trainingPlanModel.create({
|
||||
id,
|
||||
userId,
|
||||
name: dto.name ?? '',
|
||||
createdAt,
|
||||
startDate: new Date(dto.startDate),
|
||||
mode: dto.mode,
|
||||
@@ -39,7 +40,10 @@ export class TrainingPlansService {
|
||||
}
|
||||
|
||||
async remove(userId: string, id: string) {
|
||||
const count = await this.trainingPlanModel.destroy({ where: { id, userId } });
|
||||
const [count] = await this.trainingPlanModel.update(
|
||||
{ deleted: true },
|
||||
{ where: { id, userId, deleted: false } }
|
||||
);
|
||||
if (count > 0) {
|
||||
await this.activityLogsService.record({
|
||||
userId,
|
||||
@@ -52,14 +56,26 @@ export class TrainingPlansService {
|
||||
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 list(userId: string, page: number = 1, limit: number = 10) {
|
||||
const offset = (page - 1) * limit;
|
||||
const { rows, count } = await this.trainingPlanModel.findAndCountAll({
|
||||
where: { userId, deleted: false },
|
||||
order: [['created_at', 'DESC']],
|
||||
limit,
|
||||
offset,
|
||||
});
|
||||
|
||||
return {
|
||||
data: rows.map(r => ({
|
||||
id: r.id,
|
||||
createdAt: r.createdAt,
|
||||
startDate: r.startDate,
|
||||
goal: r.goal,
|
||||
})),
|
||||
total: count,
|
||||
page,
|
||||
limit,
|
||||
};
|
||||
}
|
||||
|
||||
async detail(userId: string, id: string) {
|
||||
|
||||
Reference in New Issue
Block a user