diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2023-09-09 05:09:23 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-09-08 21:09:23 +0000 |
commit | e3ed67859a434f2fef1db175e9c82251a7f9f6c9 (patch) | |
tree | e4c48b0c17d6d5a9507c3fc1479d7da2faba59dd /models/issues/issue.go | |
parent | b8ad558c93052ee3e2f43f1e24e6f5c134da36c4 (diff) | |
download | gitea-e3ed67859a434f2fef1db175e9c82251a7f9f6c9.tar.gz gitea-e3ed67859a434f2fef1db175e9c82251a7f9f6c9.zip |
Move some functions to service layer (#26969)
Diffstat (limited to 'models/issues/issue.go')
-rw-r--r-- | models/issues/issue.go | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/models/issues/issue.go b/models/issues/issue.go index 8f59c9cb42..341ec8547a 100644 --- a/models/issues/issue.go +++ b/models/issues/issue.go @@ -892,3 +892,50 @@ func IsNewPinAllowed(ctx context.Context, repoID int64, isPull bool) (bool, erro func IsErrIssueMaxPinReached(err error) bool { return err == ErrIssueMaxPinReached } + +// InsertIssues insert issues to database +func InsertIssues(issues ...*Issue) error { + ctx, committer, err := db.TxContext(db.DefaultContext) + if err != nil { + return err + } + defer committer.Close() + + for _, issue := range issues { + if err := insertIssue(ctx, issue); err != nil { + return err + } + } + return committer.Commit() +} + +func insertIssue(ctx context.Context, issue *Issue) error { + sess := db.GetEngine(ctx) + if _, err := sess.NoAutoTime().Insert(issue); err != nil { + return err + } + issueLabels := make([]IssueLabel, 0, len(issue.Labels)) + for _, label := range issue.Labels { + issueLabels = append(issueLabels, IssueLabel{ + IssueID: issue.ID, + LabelID: label.ID, + }) + } + if len(issueLabels) > 0 { + if _, err := sess.Insert(issueLabels); err != nil { + return err + } + } + + for _, reaction := range issue.Reactions { + reaction.IssueID = issue.ID + } + + if len(issue.Reactions) > 0 { + if _, err := sess.Insert(issue.Reactions); err != nil { + return err + } + } + + return nil +} |