实现GetPlan
This commit is contained in:
@@ -1,11 +1,15 @@
|
||||
package plan
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strconv"
|
||||
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/app/controller"
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/infra/logs"
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/infra/models"
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/infra/repository"
|
||||
"github.com/gin-gonic/gin"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// --- 请求和响应 DTO 定义 ---
|
||||
@@ -143,11 +147,34 @@ func (c *Controller) CreatePlan(ctx *gin.Context) {
|
||||
// @Success 200 {object} controller.Response{data=plan.PlanResponse} "业务码为200代表成功获取"
|
||||
// @Failure 200 {object} controller.Response "业务失败,具体错误码和信息见响应体(例如400, 404, 500)"
|
||||
// @Router /plans/{id} [get]
|
||||
func (pc *Controller) GetPlan(c *gin.Context) {
|
||||
// 占位符:此处应调用服务层或仓库层来获取计划
|
||||
pc.logger.Infof("收到获取计划 (占位符)")
|
||||
controller.SendResponse(c, controller.CodeSuccess, "获取计划接口占位符", ListPlansResponse{Plans: []PlanResponse{}, Total: 0})
|
||||
func (c *Controller) GetPlan(ctx *gin.Context) {
|
||||
// 1. 从 URL 路径中获取 ID
|
||||
idStr := ctx.Param("id")
|
||||
id, err := strconv.ParseUint(idStr, 10, 32)
|
||||
if err != nil {
|
||||
controller.SendErrorResponse(ctx, controller.CodeBadRequest, "无效的计划ID格式")
|
||||
return
|
||||
}
|
||||
|
||||
// 2. 调用仓库层获取计划详情
|
||||
plan, err := c.planRepo.GetPlanByID(uint(id))
|
||||
if err != nil {
|
||||
// 判断是否为“未找到”错误
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
controller.SendErrorResponse(ctx, controller.CodeNotFound, "计划不存在")
|
||||
return
|
||||
}
|
||||
// 其他数据库错误视为内部错误
|
||||
c.logger.Errorf("获取计划详情失败: %v", err)
|
||||
controller.SendErrorResponse(ctx, controller.CodeInternalError, "获取计划详情时发生内部错误")
|
||||
return
|
||||
}
|
||||
|
||||
// 3. 将模型转换为响应 DTO
|
||||
resp := PlanToResponse(plan)
|
||||
|
||||
// 4. 发送成功响应
|
||||
controller.SendResponse(ctx, controller.CodeSuccess, "获取计划详情成功", resp)
|
||||
}
|
||||
|
||||
// ListPlans godoc
|
||||
|
||||
Reference in New Issue
Block a user