Skip to content

Commit

Permalink
fix: 渠道标签开启下使用ID排序出错
Browse files Browse the repository at this point in the history
  • Loading branch information
Calcium-Ion committed Dec 9, 2024
1 parent 2c79811 commit 56ccb30
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 9 deletions.
4 changes: 2 additions & 2 deletions controller/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func GetAllChannels(c *gin.Context) {
}
for _, tag := range tags {
if tag != nil && *tag != "" {
tagChannel, err := model.GetChannelsByTag(*tag)
tagChannel, err := model.GetChannelsByTag(*tag, idSort)
if err == nil {
channelData = append(channelData, tagChannel...)
}
Expand Down Expand Up @@ -181,7 +181,7 @@ func SearchChannels(c *gin.Context) {
}
for _, tag := range tags {
if tag != nil && *tag != "" {
tagChannel, err := model.GetChannelsByTag(*tag)
tagChannel, err := model.GetChannelsByTag(*tag, idSort)
if err == nil {
channelData = append(channelData, tagChannel...)
}
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ var indexPage []byte
func main() {
err := godotenv.Load(".env")
if err != nil {
common.SysLog("Can't load .env file")
common.SysError("failed to load .env file: " + err.Error())
}

common.SetupLogger()
Expand Down
19 changes: 13 additions & 6 deletions model/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,13 @@ func GetAllChannels(startIdx int, num int, selectAll bool, idSort bool) ([]*Chan
return channels, err
}

func GetChannelsByTag(tag string) ([]*Channel, error) {
func GetChannelsByTag(tag string, idSort bool) ([]*Channel, error) {
var channels []*Channel
err := DB.Where("tag = ?", tag).Find(&channels).Error
order := "priority desc"
if idSort {
order = "id desc"
}
err := DB.Where("tag = ?", tag).Order(order).Find(&channels).Error
return channels, err
}

Expand Down Expand Up @@ -362,7 +366,7 @@ func EditChannelByTag(tag string, newTag *string, modelMapping *string, models *
return err
}
if shouldReCreateAbilities {
channels, err := GetChannelsByTag(updatedTag)
channels, err := GetChannelsByTag(updatedTag, false)
if err == nil {
for _, channel := range channels {
err = channel.UpdateAbilities()
Expand Down Expand Up @@ -450,10 +454,13 @@ func SearchTags(keyword string, group string, model string, idSort bool) ([]*str
args = append(args, common.String2Int(keyword), "%"+keyword+"%", keyword, "%"+model+"%")
}

err := baseQuery.Where(whereClause, args...).
Select("DISTINCT tag").
subQuery := baseQuery.Where(whereClause, args...).
Select("tag").
Where("tag != ''").
Order(order).
Order(order)

err := DB.Table("(?) as sub", subQuery).
Select("DISTINCT tag").
Find(&tags).Error

if err != nil {
Expand Down

0 comments on commit 56ccb30

Please sign in to comment.