fixed topics create/update error

pull/351/head
Michael Li 2 years ago
parent 52f691fd5f
commit 6e6ab81c1c
No known key found for this signature in database

@ -68,14 +68,13 @@ func (s *topicServant) ListTags(typ cs.TagType, offset, limit int) (res cs.TagLi
// TODO: 优化查询方式,直接返回[]*core.Tag, 目前保持先转换一下 // TODO: 优化查询方式,直接返回[]*core.Tag, 目前保持先转换一下
var ( var (
tags []*dbr.Tag tags []*dbr.Tag
tagMap map[int64]*cs.TagItem
item *cs.TagItem item *cs.TagItem
) )
if tags, err = (&dbr.Tag{}).List(s.db, conditions, offset, limit); err == nil { if tags, err = (&dbr.Tag{}).List(s.db, conditions, offset, limit); err == nil {
if len(tags) == 0 { if len(tags) == 0 {
return return
} }
ids := make([]int64, 0, len(tags)) tagMap := make(map[int64][]*cs.TagItem, len(tags))
for _, tag := range tags { for _, tag := range tags {
item = &cs.TagItem{ item = &cs.TagItem{
ID: tag.ID, ID: tag.ID,
@ -83,19 +82,23 @@ func (s *topicServant) ListTags(typ cs.TagType, offset, limit int) (res cs.TagLi
Tag: tag.Tag, Tag: tag.Tag,
QuoteNum: tag.QuoteNum, QuoteNum: tag.QuoteNum,
} }
tagMap[item.UserID] = item tagMap[item.UserID] = append(tagMap[item.UserID], item)
res = append(res, item) res = append(res, item)
ids = append(ids, tag.UserID) }
ids := make([]int64, len(tagMap))
for userId := range tagMap {
ids = append(ids, userId)
} }
userInfos, err := (&dbr.User{}).ListUserInfoById(s.db, ids) userInfos, err := (&dbr.User{}).ListUserInfoById(s.db, ids)
if err != nil { if err != nil {
return nil, err return nil, err
} }
for _, userInfo := range userInfos { for _, userInfo := range userInfos {
item = tagMap[userInfo.ID] for _, item = range tagMap[userInfo.ID] {
item.User = userInfo item.User = userInfo
} }
} }
}
return return
} }

@ -20,10 +20,10 @@ func createTags(db *gorm.DB, userId int64, tags []string) (res cs.TagInfoList, e
return return
} }
} else { } else {
tag.UserID, tag.QuoteNum = userId, 1
if tag, err = (&dbr.Tag{ if tag, err = (&dbr.Tag{
UserID: userId, UserID: userId,
QuoteNum: 1, QuoteNum: 1,
Tag: name,
}).Create(db); err != nil { }).Create(db); err != nil {
return return
} }

Loading…
Cancel
Save