🀄 完善标签查询功能

This commit is contained in:
dushixiang
2021-01-06 20:29:48 +08:00
parent fbac4c7c2b
commit 1a3a4025d5
4 changed files with 82 additions and 10 deletions

View File

@ -29,8 +29,9 @@ func AssetPagingEndpoint(c echo.Context) error {
pageSize, _ := strconv.Atoi(c.QueryParam("pageSize"))
name := c.QueryParam("name")
protocol := c.QueryParam("protocol")
tags := c.QueryParam("tags")
items, total, _ := model.FindPageAsset(pageIndex, pageSize, name, protocol)
items, total, _ := model.FindPageAsset(pageIndex, pageSize, name, protocol, tags)
return Success(c, H{
"total": total,

View File

@ -43,7 +43,7 @@ func FindAssetByConditions(protocol string) (o []Asset, err error) {
return
}
func FindPageAsset(pageIndex, pageSize int, name, protocol string) (o []Asset, total int64, err error) {
func FindPageAsset(pageIndex, pageSize int, name, protocol, tags string) (o []Asset, total int64, err error) {
db := global.DB
if len(name) > 0 {
db = db.Where("name like ?", "%"+name+"%")
@ -53,6 +53,13 @@ func FindPageAsset(pageIndex, pageSize int, name, protocol string) (o []Asset, t
db = db.Where("protocol = ?", protocol)
}
if len(tags) > 0 {
tagArr := strings.Split(tags, ",")
for i := range tagArr {
db = db.Where("find_in_set(?, tags)", tagArr[i])
}
}
err = db.Order("created desc").Find(&o).Offset((pageIndex - 1) * pageSize).Limit(pageSize).Count(&total).Error
if o == nil {
@ -105,5 +112,5 @@ func FindAssetTags() (o []string, err error) {
o = append(o, split...)
}
return o, nil
return utils.Distinct(o), nil
}

View File

@ -119,3 +119,16 @@ func IsDir(path string) bool {
func IsFile(path string) bool {
return !IsDir(path)
}
// 去除重复元素
func Distinct(list []string) []string {
result := make([]string, 0, len(list))
temp := map[string]struct{}{}
for _, item := range list {
if _, ok := temp[item]; !ok {
temp[item] = struct{}{}
result = append(result, item)
}
}
return result
}