|
| 1 | +// Copyright 2018 The Gitea Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a MIT-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +package models |
| 6 | + |
| 7 | +import ( |
| 8 | + "fmt" |
| 9 | + "strings" |
| 10 | + |
| 11 | + "code.gitea.io/gitea/modules/util" |
| 12 | + |
| 13 | + "github.com/go-xorm/builder" |
| 14 | +) |
| 15 | + |
| 16 | +func init() { |
| 17 | + tables = append(tables, |
| 18 | + new(Topic), |
| 19 | + new(RepoTopic), |
| 20 | + ) |
| 21 | +} |
| 22 | + |
| 23 | +// Topic represents a topic of repositories |
| 24 | +type Topic struct { |
| 25 | + ID int64 |
| 26 | + Name string `xorm:"unique"` |
| 27 | + RepoCount int |
| 28 | + CreatedUnix util.TimeStamp `xorm:"INDEX created"` |
| 29 | + UpdatedUnix util.TimeStamp `xorm:"INDEX updated"` |
| 30 | +} |
| 31 | + |
| 32 | +// RepoTopic represents associated repositories and topics |
| 33 | +type RepoTopic struct { |
| 34 | + RepoID int64 `xorm:"unique(s)"` |
| 35 | + TopicID int64 `xorm:"unique(s)"` |
| 36 | +} |
| 37 | + |
| 38 | +// ErrTopicNotExist represents an error that a topic is not exist |
| 39 | +type ErrTopicNotExist struct { |
| 40 | + Name string |
| 41 | +} |
| 42 | + |
| 43 | +// IsErrTopicNotExist checks if an error is an ErrTopicNotExist. |
| 44 | +func IsErrTopicNotExist(err error) bool { |
| 45 | + _, ok := err.(ErrTopicNotExist) |
| 46 | + return ok |
| 47 | +} |
| 48 | + |
| 49 | +// Error implements error interface |
| 50 | +func (err ErrTopicNotExist) Error() string { |
| 51 | + return fmt.Sprintf("topic is not exist [name: %s]", err.Name) |
| 52 | +} |
| 53 | + |
| 54 | +// GetTopicByName retrieves topic by name |
| 55 | +func GetTopicByName(name string) (*Topic, error) { |
| 56 | + var topic Topic |
| 57 | + if has, err := x.Where("name = ?", name).Get(&topic); err != nil { |
| 58 | + return nil, err |
| 59 | + } else if !has { |
| 60 | + return nil, ErrTopicNotExist{name} |
| 61 | + } |
| 62 | + return &topic, nil |
| 63 | +} |
| 64 | + |
| 65 | +// FindTopicOptions represents the options when fdin topics |
| 66 | +type FindTopicOptions struct { |
| 67 | + RepoID int64 |
| 68 | + Keyword string |
| 69 | + Limit int |
| 70 | + Page int |
| 71 | +} |
| 72 | + |
| 73 | +func (opts *FindTopicOptions) toConds() builder.Cond { |
| 74 | + var cond = builder.NewCond() |
| 75 | + if opts.RepoID > 0 { |
| 76 | + cond = cond.And(builder.Eq{"repo_topic.repo_id": opts.RepoID}) |
| 77 | + } |
| 78 | + |
| 79 | + if opts.Keyword != "" { |
| 80 | + cond = cond.And(builder.Like{"topic.name", opts.Keyword}) |
| 81 | + } |
| 82 | + |
| 83 | + return cond |
| 84 | +} |
| 85 | + |
| 86 | +// FindTopics retrieves the topics via FindTopicOptions |
| 87 | +func FindTopics(opts *FindTopicOptions) (topics []*Topic, err error) { |
| 88 | + sess := x.Select("topic.*").Where(opts.toConds()) |
| 89 | + if opts.RepoID > 0 { |
| 90 | + sess.Join("INNER", "repo_topic", "repo_topic.topic_id = topic.id") |
| 91 | + } |
| 92 | + if opts.Limit > 0 { |
| 93 | + sess.Limit(opts.Limit, opts.Page*opts.Limit) |
| 94 | + } |
| 95 | + return topics, sess.Desc("topic.repo_count").Find(&topics) |
| 96 | +} |
| 97 | + |
| 98 | +// SaveTopics save topics to a repository |
| 99 | +func SaveTopics(repoID int64, topicNames ...string) error { |
| 100 | + topics, err := FindTopics(&FindTopicOptions{ |
| 101 | + RepoID: repoID, |
| 102 | + }) |
| 103 | + if err != nil { |
| 104 | + return err |
| 105 | + } |
| 106 | + |
| 107 | + sess := x.NewSession() |
| 108 | + defer sess.Close() |
| 109 | + |
| 110 | + if err := sess.Begin(); err != nil { |
| 111 | + return err |
| 112 | + } |
| 113 | + |
| 114 | + var addedTopicNames []string |
| 115 | + for _, topicName := range topicNames { |
| 116 | + if strings.TrimSpace(topicName) == "" { |
| 117 | + continue |
| 118 | + } |
| 119 | + |
| 120 | + var found bool |
| 121 | + for _, t := range topics { |
| 122 | + if strings.EqualFold(topicName, t.Name) { |
| 123 | + found = true |
| 124 | + break |
| 125 | + } |
| 126 | + } |
| 127 | + if !found { |
| 128 | + addedTopicNames = append(addedTopicNames, topicName) |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + var removeTopics []*Topic |
| 133 | + for _, t := range topics { |
| 134 | + var found bool |
| 135 | + for _, topicName := range topicNames { |
| 136 | + if strings.EqualFold(topicName, t.Name) { |
| 137 | + found = true |
| 138 | + break |
| 139 | + } |
| 140 | + } |
| 141 | + if !found { |
| 142 | + removeTopics = append(removeTopics, t) |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + for _, topicName := range addedTopicNames { |
| 147 | + var topic Topic |
| 148 | + if has, err := sess.Where("name = ?", topicName).Get(&topic); err != nil { |
| 149 | + return err |
| 150 | + } else if !has { |
| 151 | + topic.Name = topicName |
| 152 | + topic.RepoCount = 1 |
| 153 | + if _, err := sess.Insert(&topic); err != nil { |
| 154 | + return err |
| 155 | + } |
| 156 | + } else { |
| 157 | + topic.RepoCount++ |
| 158 | + if _, err := sess.ID(topic.ID).Cols("repo_count").Update(&topic); err != nil { |
| 159 | + return err |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + if _, err := sess.Insert(&RepoTopic{ |
| 164 | + RepoID: repoID, |
| 165 | + TopicID: topic.ID, |
| 166 | + }); err != nil { |
| 167 | + return err |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + for _, topic := range removeTopics { |
| 172 | + topic.RepoCount-- |
| 173 | + if _, err := sess.ID(topic.ID).Cols("repo_count").Update(topic); err != nil { |
| 174 | + return err |
| 175 | + } |
| 176 | + |
| 177 | + if _, err := sess.Delete(&RepoTopic{ |
| 178 | + RepoID: repoID, |
| 179 | + TopicID: topic.ID, |
| 180 | + }); err != nil { |
| 181 | + return err |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + if _, err := sess.ID(repoID).Cols("topics").Update(&Repository{ |
| 186 | + Topics: topicNames, |
| 187 | + }); err != nil { |
| 188 | + return err |
| 189 | + } |
| 190 | + |
| 191 | + return sess.Commit() |
| 192 | +} |
0 commit comments