Skip to content

Commit

Permalink
Merge pull request #145 from decert-me/feature/label-filter
Browse files Browse the repository at this point in the history
En 过滤无挑战/教程的标签
  • Loading branch information
0xdwong authored Jul 3, 2024
2 parents a0cba0e + 9be2da0 commit cae398a
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 4 deletions.
2 changes: 1 addition & 1 deletion internal/app/api/v1/tutorial.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
32 changes: 32 additions & 0 deletions internal/app/dao/tutorial.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ import (
"crypto/md5"
"encoding/hex"
"encoding/json"
"errors"
"github.com/tidwall/gjson"
"go.uber.org/zap"
"gorm.io/gorm"
"math"
)

Expand Down Expand Up @@ -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
}
3 changes: 2 additions & 1 deletion internal/app/model/request/tutorial.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
28 changes: 26 additions & 2 deletions internal/app/service/tutorial.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 获取分类标签列表
Expand Down

0 comments on commit cae398a

Please sign in to comment.