fixed create not expected empty string tag when create post

pull/109/head
alimy 2 years ago
parent 58171998a3
commit ed32dc614c

@ -83,12 +83,24 @@ func (p *PostContentItem) Check() error {
return nil
}
func tagsFrom(originTags []string) []string {
tags := make([]string, 0, len(originTags))
for _, tag := range originTags {
// TODO: 优化tag有效性检测
if tag = strings.TrimSpace(tag); len(tag) > 0 {
tags = append(tags, tag)
}
}
return tags
}
func CreatePost(c *gin.Context, userID int64, param PostCreationReq) (*model.Post, error) {
ip := c.ClientIP()
tags := tagsFrom(param.Tags)
post := &model.Post{
UserID: userID,
Tags: strings.Join(param.Tags, ","),
Tags: strings.Join(tags, ","),
IP: ip,
IPLoc: util.GetIPLoc(ip),
AttachmentPrice: param.AttachmentPrice,
@ -99,7 +111,7 @@ func CreatePost(c *gin.Context, userID int64, param PostCreationReq) (*model.Pos
}
// 创建标签
for _, t := range param.Tags {
for _, t := range tags {
tag := &model.Tag{
UserID: userID,
Tag: t,

Loading…
Cancel
Save