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 <art27@cantab.net>
This commit is contained in:
Lunny Xiao 2021-09-19 19:49:59 +08:00 committed by GitHub
parent 462306e263
commit a4bfef265d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
335 changed files with 4191 additions and 3654 deletions

View file

@ -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