重构 #4
| @@ -107,10 +107,31 @@ func NewController(logger *logs.Logger, planRepo repository.PlanRepository) *Con | |||||||
| // @Success      200 {object} controller.Response{data=plan.PlanResponse} "业务码为201代表创建成功" | // @Success      200 {object} controller.Response{data=plan.PlanResponse} "业务码为201代表创建成功" | ||||||
| // @Failure      200 {object} controller.Response "业务失败,具体错误码和信息见响应体(例如400, 500)" | // @Failure      200 {object} controller.Response "业务失败,具体错误码和信息见响应体(例如400, 500)" | ||||||
| // @Router       /plans [post] | // @Router       /plans [post] | ||||||
| func (pc *Controller) CreatePlan(c *gin.Context) { | func (c *Controller) CreatePlan(ctx *gin.Context) { | ||||||
| 	// 占位符:此处应调用服务层或仓库层来创建计划 | 	var req CreatePlanRequest | ||||||
| 	pc.logger.Infof("收到创建计划请求 (占位符)") | 	if err := ctx.ShouldBindJSON(&req); err != nil { | ||||||
| 	controller.SendResponse(c, controller.CodeCreated, "创建计划接口占位符", PlanResponse{ID: 0, Name: "占位计划"}) | 		controller.SendErrorResponse(ctx, controller.CodeBadRequest, "无效的请求体: "+err.Error()) | ||||||
|  | 		return | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	// 使用已有的转换函数,它已经包含了验证和重排逻辑 | ||||||
|  | 	planToCreate, err := PlanFromCreateRequest(&req) | ||||||
|  | 	if err != nil { | ||||||
|  | 		controller.SendErrorResponse(ctx, controller.CodeBadRequest, "计划数据校验失败: "+err.Error()) | ||||||
|  | 		return | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	// 调用仓库方法创建计划 | ||||||
|  | 	if err := c.planRepo.CreatePlan(planToCreate); err != nil { | ||||||
|  | 		controller.SendErrorResponse(ctx, controller.CodeBadRequest, "创建计划失败: "+err.Error()) | ||||||
|  | 		return | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	// 使用已有的转换函数将创建后的模型转换为响应对象 | ||||||
|  | 	resp := PlanToResponse(planToCreate) | ||||||
|  |  | ||||||
|  | 	// 使用统一的成功响应函数 | ||||||
|  | 	controller.SendResponse(ctx, controller.CodeCreated, "计划创建成功", resp) | ||||||
| } | } | ||||||
|  |  | ||||||
| // GetPlan godoc | // GetPlan godoc | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user