From a4bfef265d9e512830350635a0489c2cdcd6508f Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Sun, 19 Sep 2021 19:49:59 +0800 Subject: Move db related basic functions to models/db (#17075) * Move db related basic functions to models/db * Fix lint * Fix lint * Fix test * Fix lint * Fix lint * revert unnecessary change * Fix test * Fix wrong replace string * Use *Context * Correct committer spelling and fix wrong replaced words Co-authored-by: zeripath --- models/issue_reaction.go | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) (limited to 'models/issue_reaction.go') diff --git a/models/issue_reaction.go b/models/issue_reaction.go index 5721d8cc1f..8fd22f6ca8 100644 --- a/models/issue_reaction.go +++ b/models/issue_reaction.go @@ -8,6 +8,7 @@ import ( "bytes" "fmt" + "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/timeutil" @@ -28,6 +29,10 @@ type Reaction struct { CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"` } +func init() { + db.RegisterModel(new(Reaction)) +} + // FindReactionsOptions describes the conditions to Find reactions type FindReactionsOptions struct { ListOptions @@ -66,7 +71,7 @@ func (opts *FindReactionsOptions) toConds() builder.Cond { // FindCommentReactions returns a ReactionList of all reactions from an comment func FindCommentReactions(comment *Comment) (ReactionList, error) { - return findReactions(x, FindReactionsOptions{ + return findReactions(db.DefaultContext().Engine(), FindReactionsOptions{ IssueID: comment.IssueID, CommentID: comment.ID, }) @@ -74,14 +79,14 @@ func FindCommentReactions(comment *Comment) (ReactionList, error) { // FindIssueReactions returns a ReactionList of all reactions from an issue func FindIssueReactions(issue *Issue, listOptions ListOptions) (ReactionList, error) { - return findReactions(x, FindReactionsOptions{ + return findReactions(db.DefaultContext().Engine(), FindReactionsOptions{ ListOptions: listOptions, IssueID: issue.ID, CommentID: -1, }) } -func findReactions(e Engine, opts FindReactionsOptions) ([]*Reaction, error) { +func findReactions(e db.Engine, opts FindReactionsOptions) ([]*Reaction, error) { e = e. Where(opts.toConds()). In("reaction.`type`", setting.UI.Reactions). @@ -143,7 +148,7 @@ func CreateReaction(opts *ReactionOptions) (*Reaction, error) { return nil, ErrForbiddenIssueReaction{opts.Type} } - sess := x.NewSession() + sess := db.DefaultContext().NewSession() defer sess.Close() if err := sess.Begin(); err != nil { return nil, err @@ -179,7 +184,7 @@ func CreateCommentReaction(doer *User, issue *Issue, comment *Comment, content s }) } -func deleteReaction(e Engine, opts *ReactionOptions) error { +func deleteReaction(e db.Engine, opts *ReactionOptions) error { reaction := &Reaction{ Type: opts.Type, } @@ -198,7 +203,7 @@ func deleteReaction(e Engine, opts *ReactionOptions) error { // DeleteReaction deletes reaction for issue or comment. func DeleteReaction(opts *ReactionOptions) error { - sess := x.NewSession() + sess := db.DefaultContext().NewSession() defer sess.Close() if err := sess.Begin(); err != nil { return err @@ -235,7 +240,7 @@ func (r *Reaction) LoadUser() (*User, error) { if r.User != nil { return r.User, nil } - user, err := getUserByID(x, r.UserID) + user, err := getUserByID(db.DefaultContext().Engine(), r.UserID) if err != nil { return nil, err } @@ -281,7 +286,7 @@ func (list ReactionList) getUserIDs() []int64 { return keysInt64(userIDs) } -func (list ReactionList) loadUsers(e Engine, repo *Repository) ([]*User, error) { +func (list ReactionList) loadUsers(e db.Engine, repo *Repository) ([]*User, error) { if len(list) == 0 { return nil, nil } @@ -309,7 +314,7 @@ func (list ReactionList) loadUsers(e Engine, repo *Repository) ([]*User, error) // LoadUsers loads reactions' all users func (list ReactionList) LoadUsers(repo *Repository) ([]*User, error) { - return list.loadUsers(x, repo) + return list.loadUsers(db.DefaultContext().Engine(), repo) } // GetFirstUsers returns first reacted user display names separated by comma -- cgit v1.2.3