두 가지 사고방식: (1) 먼저 태그를 찾아보고platetag(2)연표분조부례: 서비스와dao층(두 가지 실현)
9042 단어 go
// -tag
func GetTagList(req *request.TagListCurrentReq) (*response.TagItemListResp, error) {
tags, total, err := store.TagMysql.GetTagList(req)
if err != nil {
logrus.Errorf("GetTagList error: %v, value: %v", err, tags)
}
//
plateApi := client.NewPlateAPI()
// :100
ctx, _ := context.WithTimeout(context.Background(), 100*time.Millisecond)
isIncludeUnCommendPlates := int32(1)
plates, _, err := plateApi.AllPlateInternal(ctx, isIncludeUnCommendPlates)
if err != nil {
logrus.Errorf("AllPlateInternal err: %v", err)
}
plateMap := make(map[string]*plate.ResponsePlateMainDataResp, 0)
for _, plateItem := range plates {
plateCurrent := plateItem
plateMap[*plateCurrent.Id] = &plateCurrent
}
logrus.Infof("plateMap len: %v, content: %v", len(plateMap), plateMap)
// plateName
funcGetPlateName := func(plateID int64) string {
if plateName, ok := plateMap[utils.SNToID(plateID)]; ok {
return *plateName.Name
} else {
return " "
}
}
// plateName
tagsCurrent := make([]*response.TagListItemResp, 0)
num := 0
for i := range tags {
if i != 0 && tags[i].ID == tags[i-1].ID {
tagsCurrent[num-1].TagPlateNames = append(tagsCurrent[num-1].TagPlateNames, &response.TagPlateName{
PlateID: utils.SNToID(tags[i].PlateID),
PlateName: funcGetPlateName(tags[i].PlateID),
})
total--
continue
}
tagPlateNames := make([]*response.TagPlateName, 0)
tagPlateNames = append(tagPlateNames, &response.TagPlateName{
PlateID: utils.SNToID(tags[i].PlateID),
PlateName: funcGetPlateName(tags[i].PlateID),
})
tagsCurrent = append(tagsCurrent, &response.TagListItemResp{
Tag: tags[i].Tag,
TagPlateNames: tagPlateNames,
})
num++
}
//
if req.Offset < 0 || req.Limit < 0 {
return &response.TagItemListResp{
TagList: make([]*response.TagListItemResp, 0),
Total: total,
}, nil
}
if req.Limit == 0 {
req.Limit = 20
}
if req.Offset+req.Limit <= int64(len(tagsCurrent)) {
tagsCurrent = tagsCurrent[req.Offset : req.Offset+req.Limit]
} else if req.Offset < int64(len(tagsCurrent)) {
tagsCurrent = tagsCurrent[req.Offset:int64(len(tagsCurrent))]
} else {
tagsCurrent = make([]*response.TagListItemResp, 0)
}
return &response.TagItemListResp{
TagList: tagsCurrent,
Total: total,
}, nil
}
// -
func GetTagArticleList(req *request.ArticleTagReq) (*response.TagArticleListResp, error) {
tagArticles, total, err := store.TagMysql.GetTagArticleList(req)
if err != nil {
logrus.Errorf("GetTagArticleList error: %v", err)
}
// tags
tags, _, err := store.TagMysql.GetTagList(&request.TagListCurrentReq{})
if err != nil {
logrus.Errorf("GetTagList error: %v", err)
}
tagsMap := make(map[int64]string)
for _, item := range tags {
tagsMap[item.ID] = item.Title
}
tagArticleListItemResp := make([]*response.TagArticleListItemResp, 0)
for _, item := range tagArticles {
tagName := ""
// tag title
if tagTitle, ok := tagsMap[item.TagID]; ok {
tagName = tagTitle
} else {
//
total--
continue
}
tagArticleListItemResp = append(tagArticleListItemResp, &response.TagArticleListItemResp{
TagArticle: item,
TagName: tagName,
})
}
return &response.TagArticleListResp{
TagArticleList: tagArticleListItemResp,
Total: total,
}, nil
}
// -tag
func GetTagList(req *request.TagListCurrentReq) (*response.TagItemListResp, error) {
tags, total, err := store.TagMysql.GetTagListWithGroup(req)
if err != nil {
logrus.Errorf("GetTagListWithGroup error: %v, value: %v", err, tags)
}
//
plateApi := client.NewPlateAPI()
// :500
ctx, _ := context.WithTimeout(context.Background(), 500*time.Millisecond)
isIncludeUnCommendPlates := int32(1)
plates, _, err := plateApi.AllPlateInternal(ctx, isIncludeUnCommendPlates)
if err != nil {
logrus.Errorf("AllPlateInternal err: %v", err)
}
plateMap := make(map[string]*plate.ResponsePlateMainDataResp, 0)
for _, plateItem := range plates {
plateCurrent := plateItem
plateMap[*plateCurrent.Id] = &plateCurrent
}
logrus.Infof("plateMap len: %v, content: %v", len(plateMap), plateMap)
// plateName
funcGetPlateName := func(plateID int64) string {
if plateName, ok := plateMap[utils.SNToID(plateID)]; ok {
return *plateName.Name
} else {
return " "
}
}
// plateName
tagsCurrent := make([]*response.TagListItemResp, 0)
for _, item := range tags {
var plateIDs []string
if item.PlateID != "" {
plateIDs = strings.Split(item.PlateID, ",")
} else {
plateIDs = []string{}
}
tagPlateNames := make([]*response.TagPlateName, 0)
isExistMap := make(map[int64]string, 0)
for _, plateIDItem := range plateIDs {
plateIDItemInt, _ := strconv.ParseInt(plateIDItem, 10, 64)
if _, ok := isExistMap[plateIDItemInt]; !ok {
tagPlateNames = append(tagPlateNames, &response.TagPlateName{
PlateID: utils.SNToID(plateIDItemInt),
PlateName: funcGetPlateName(plateIDItemInt),
})
isExistMap[plateIDItemInt] = "exist"
}
}
tagsCurrent = append(tagsCurrent, &response.TagListItemResp{
Tag: item.Tag,
TagPlateNames: tagPlateNames,
})
}
return &response.TagItemListResp{
TagList: tagsCurrent,
Total: total,
}, nil
}
//
func (s *tagMysqlStore) GetTagListWithGroup(req *request.TagListCurrentReq) ([]*response.TagListAdminItemRespWithGroup, int64, error) {
tags := []*response.TagListAdminItemRespWithGroup{}
var (
total int64
err error
)
if req.PlateID == "" {
if req.TagKeyword == "" {
sql := "SELECT `tag`.*, GROUP_CONCAT(plate_tag.plate_id) as `plate_id` " +
"FROM `tag` left join plate_tag " +
"ON plate_tag.tag_id = tag.id AND `plate_tag`.`deleted_at` IS NULL " +
"WHERE `tag`.`deleted_at` IS NULL " +
"GROUP BY tag.id " +
"ORDER BY `tag`.`created_at` DESC "
total = s.db.Raw(sql).Scan(&tags).RowsAffected
if req.Limit != 0 {
err = s.db.Raw(sql+"limit ? offset ?", req.Limit, req.Offset).Scan(&tags).Error
} else {
err = s.db.Raw(sql).Scan(&tags).Error
}
} else {
sql := "SELECT `tag`.*, GROUP_CONCAT(plate_tag.plate_id) as `plate_id` " +
"FROM `tag` left join plate_tag " +
"ON plate_tag.tag_id = tag.id AND `plate_tag`.`deleted_at` IS NULL " +
"WHERE `tag`.`deleted_at` IS NULL " +
"AND (`tag`.`title` like ? OR `tag`.`alias` like ?) " +
"GROUP BY tag.id " +
"ORDER BY `tag`.`created_at` DESC "
total = s.db.Raw(sql, "%"+req.TagKeyword+"%", "%"+req.TagKeyword+"%").Scan(&tags).RowsAffected
if req.Limit != 0 {
err = s.db.Raw(sql+"limit ? offset ?", "%"+req.TagKeyword+"%", "%"+req.TagKeyword+"%", req.Limit, req.Offset).Scan(&tags).Error
} else {
err = s.db.Raw(sql, "%"+req.TagKeyword+"%", "%"+req.TagKeyword+"%").Scan(&tags).Error
}
}
} else {
if req.TagKeyword == "" {
sql := "SELECT `tag`.*, GROUP_CONCAT(plate_tag.plate_id) as `plate_id` " +
"FROM `tag` left join plate_tag " +
"ON plate_tag.tag_id = tag.id AND `plate_tag`.`deleted_at` IS NULL " +
"WHERE `tag`.`deleted_at` IS NULL " +
"AND `plate_tag`.`plate_id` = ? " +
"GROUP BY tag.id " +
"ORDER BY `tag`.`created_at` DESC "
total = s.db.Raw(sql, req.PlateID).Scan(&tags).RowsAffected
if req.Limit != 0 {
err = s.db.Raw(sql+"limit ? offset ?", req.PlateID, req.Limit, req.Offset).Scan(&tags).Error
} else {
err = s.db.Raw(sql, req.PlateID).Scan(&tags).Error
}
} else {
sql := "SELECT `tag`.*, GROUP_CONCAT(plate_tag.plate_id) as `plate_id` " +
"FROM `tag` left join plate_tag " +
"ON plate_tag.tag_id = tag.id AND `plate_tag`.`deleted_at` IS NULL " +
"WHERE `tag`.`deleted_at` IS NULL " +
"AND `plate_tag`.`plate_id` = ? " +
"AND (`tag`.`title` like ? OR `tag`.`alias` like ?) " +
"GROUP BY tag.id " +
"ORDER BY `tag`.`created_at` DESC "
total = s.db.Raw(sql, req.PlateID, "%"+req.TagKeyword+"%", "%"+req.TagKeyword+"%").Scan(&tags).RowsAffected
if req.Limit != 0 {
err = s.db.Raw(sql+"limit ? offset ?", req.PlateID, "%"+req.TagKeyword+"%", "%"+req.TagKeyword+"%", req.Limit, req.Offset).Scan(&tags).Error
} else {
err = s.db.Raw(sql, req.PlateID, "%"+req.TagKeyword+"%", "%"+req.TagKeyword+"%").Scan(&tags).Error
}
}
}
return tags, total, err
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Go Fiber 및 PlanetScale로 REST API 구축 - 4부다시 사용자 핸들러에 UpdateUser라는 새 함수를 추가합니다. 업데이트 사용자를 main.go에 등록 이제 응용 프로그램을 다시 실행하십시오. 이전에 생성한 사용자를 업데이트합니다. 응답 사용자가 존재하지 않을...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.