Browse Source

#3169

update addTopic api
fix-1523
chenyifan01 2 years ago
parent
commit
7c8e7ce199
2 changed files with 28 additions and 2 deletions
  1. +14
    -0
      models/topic.go
  2. +14
    -2
      routers/api/v1/repo/topic.go

+ 14
- 0
models/topic.go View File

@@ -9,6 +9,7 @@ import (
"regexp"
"strings"
"unicode/utf8"
"xorm.io/xorm"

"code.gitea.io/gitea/modules/timeutil"

@@ -337,3 +338,16 @@ func GetOrgTopics(orgId int64) ([]Topic, error) {

return result, nil
}

func UpdateRepoTopics(repoID int64, topicNames []string, sess ...*xorm.Engine) error {
e := x
if len(sess) > 0 {
e = sess[0]
}
if _, err := e.ID(repoID).Cols("topics").Update(&Repository{
Topics: topicNames,
}); err != nil {
return err
}
return nil
}

+ 14
- 2
routers/api/v1/repo/topic.go View File

@@ -177,13 +177,25 @@ func AddTopic(ctx *context.APIContext) {
return
}

_, err = models.AddTopic(ctx.Repo.Repository.ID, topicName)
topic, err := models.AddTopic(ctx.Repo.Repository.ID, topicName)
if err != nil {
log.Error("AddTopic failed: %v", err)
ctx.InternalServerError(err)
return
}

found := false
topicNames := make([]string, len(topics))
for i, t := range topics {
topicNames[i] = t.Name
if strings.EqualFold(topic.Name, t.Name) {
found = true
break
}
}
if !found && topic.Name != "" {
topicNames = append(topicNames, topic.Name)
}
models.UpdateRepoTopics(ctx.Repo.Repository.ID, topicNames)
ctx.Status(http.StatusNoContent)
}



Loading…
Cancel
Save