bindbox-game/internal/api/app/categories.go

57 lines
1.7 KiB
Go
Executable File

package app
import (
"net/http"
"bindbox-game/internal/code"
"bindbox-game/internal/pkg/core"
"bindbox-game/internal/pkg/validation"
"bindbox-game/internal/pkg/logger"
"bindbox-game/internal/repository/mysql"
"bindbox-game/internal/repository/mysql/dao"
)
type categoryHandler struct {
logger logger.CustomLogger
readDB *dao.Query
}
func NewCategory(logger logger.CustomLogger, db mysql.Repo) *categoryHandler {
return &categoryHandler{logger: logger, readDB: dao.Use(db.GetDbR())}
}
type appCategoryItem struct {
ID int64 `json:"id"`
Name string `json:"name"`
}
type listAppCategoriesResponse struct {
List []appCategoryItem `json:"list"`
}
// ListCategoriesForApp 获取分类列表
// @Summary 获取分类列表
// @Description 获取APP端商品分类列表
// @Tags APP端.基础
// @Accept json
// @Produce json
// @Success 200 {object} listAppCategoriesResponse
// @Failure 400 {object} code.Failure
// @Router /api/app/categories [get]
func (h *categoryHandler) ListCategoriesForApp() core.HandlerFunc {
return func(ctx core.Context) {
q := h.readDB.ProductCategories.WithContext(ctx.RequestContext())
items, err := q.Where(h.readDB.ProductCategories.Status.Eq(1)).Order(h.readDB.ProductCategories.ID.Asc()).Find()
if err != nil {
ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ServerError, validation.Error(err)))
return
}
res := &listAppCategoriesResponse{List: make([]appCategoryItem, len(items))}
for i, it := range items {
res.List[i] = appCategoryItem{ID: it.ID, Name: it.Name}
}
ctx.Payload(res)
}
}