package handler import ( "lyra/internal/api" "lyra/internal/httpx" "lyra/internal/model" "net/http" "github.com/gin-gonic/gin" ) type TestHandler struct { categoryModel model.CategoryModel } func NewTestHandler(categoryModel model.CategoryModel) *TestHandler { return &TestHandler{categoryModel: categoryModel} } func (h *TestHandler) Hello() gin.HandlerFunc { return func(c *gin.Context) { categories, err := h.categoryModel.FindAll(c.Request.Context()) if err != nil { httpx.Fail(c, http.StatusInternalServerError, api.CodeInternalError, "获取分类列表失败") return } httpx.OK(c, toCategoryResponses(categories)) } } // toCategoryResponses 显式把内部模型转换为响应 DTO;make 保证空结果输出 data:[] 而非 null。 func toCategoryResponses(categories []*model.Category) []api.CategoryResponse { responses := make([]api.CategoryResponse, 0, len(categories)) for _, category := range categories { responses = append(responses, api.CategoryResponse{ ID: category.ID, Slug: category.Slug, Title: category.Title, Sort: category.Sort, }) } return responses }