diff --git a/internal/app/api/v1/tutorial.go b/internal/app/api/v1/tutorial.go index ab752c1..d8d099c 100644 --- a/internal/app/api/v1/tutorial.go +++ b/internal/app/api/v1/tutorial.go @@ -90,7 +90,7 @@ func GetLabelList(c *gin.Context) { if label.Type == "language" { data, err = srv.LabelLangList() } else if label.Type == "category" { - data, err = srv.LabelCategoryList() + data, err = srv.LabelCategoryList(label.Class) } else if label.Type == "theme" { data, err = srv.LabelThemeList() } else { diff --git a/internal/app/dao/tutorial.go b/internal/app/dao/tutorial.go index b8d9e5f..4c8f6dd 100644 --- a/internal/app/dao/tutorial.go +++ b/internal/app/dao/tutorial.go @@ -8,8 +8,10 @@ import ( "crypto/md5" "encoding/hex" "encoding/json" + "errors" "github.com/tidwall/gjson" "go.uber.org/zap" + "gorm.io/gorm" "math" ) @@ -243,3 +245,33 @@ func (d *Dao) LabelThemeList() (theme []model.Theme, err error) { err = db.Order("weight desc,created_at desc").Find(&theme).Error return } + +func (d *Dao) IsExistTutorialByCatalogueID(catalogueID uint) (bool, error) { + var count int + err := d.db.Model(&model.Tutorial{}). + Select("1"). + Where("? = ANY(category)", catalogueID). + First(&count).Error + if err != nil { + if errors.Is(err, gorm.ErrRecordNotFound) { + return false, nil + } + return false, err + } + return true, nil +} + +func (d *Dao) IsExistQuestByCatalogueID(catalogueID uint) (bool, error) { + var count int + err := d.db.Model(&model.Quest{}). + Select("1"). + Where("? = ANY(category)", catalogueID). + First(&count).Error + if err != nil { + if errors.Is(err, gorm.ErrRecordNotFound) { + return false, nil + } + return false, err + } + return true, nil +} diff --git a/internal/app/model/request/tutorial.go b/internal/app/model/request/tutorial.go index 8aa7e37..273ba63 100644 --- a/internal/app/model/request/tutorial.go +++ b/internal/app/model/request/tutorial.go @@ -27,5 +27,6 @@ type GetTutorialListStatusRequest struct { } type GetLabelRequest struct { - Type string `json:"type" binding:"required"` + Type string `json:"type" binding:"required"` + Class string `json:"class"` } diff --git a/internal/app/service/tutorial.go b/internal/app/service/tutorial.go index 6c73637..ee3fee9 100644 --- a/internal/app/service/tutorial.go +++ b/internal/app/service/tutorial.go @@ -54,8 +54,32 @@ func (s *Service) LabelLangList() (language []model.Language, err error) { } // LabelCategoryList 获取分类标签列表 -func (s *Service) LabelCategoryList() (category []model.Category, err error) { - return s.dao.LabelCategoryList() +func (s *Service) LabelCategoryList(class string) (res []model.Category, err error) { + category, err := s.dao.LabelCategoryList() + if err != nil { + return nil, err + } + // 查询 category 数量 + var categoryList []model.Category + for i, v := range category { + exist, err := s.IsExistLabelCategory(v.ID, class) + if err != nil { + return nil, err + } + if exist { + categoryList = append(categoryList, category[i]) + } + } + + return categoryList, nil +} + +// IsExistLabelCategory 按照分类判断分类标签是否存在 +func (s *Service) IsExistLabelCategory(categoryID uint, class string) (exist bool, err error) { + if class == "quest" { + return s.dao.IsExistQuestByCatalogueID(categoryID) + } + return s.dao.IsExistTutorialByCatalogueID(categoryID) } // LabelThemeList 获取分类标签列表