mirror of
				https://gitee.com/gitea/gitea
				synced 2025-11-04 08:30:25 +08:00 
			
		
		
		
	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:
		@@ -7,6 +7,7 @@ package models
 | 
			
		||||
import (
 | 
			
		||||
	"fmt"
 | 
			
		||||
 | 
			
		||||
	"code.gitea.io/gitea/models/db"
 | 
			
		||||
	"code.gitea.io/gitea/modules/util"
 | 
			
		||||
 | 
			
		||||
	"xorm.io/xorm"
 | 
			
		||||
@@ -19,13 +20,17 @@ type IssueAssignees struct {
 | 
			
		||||
	IssueID    int64 `xorm:"INDEX"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func init() {
 | 
			
		||||
	db.RegisterModel(new(IssueAssignees))
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// LoadAssignees load assignees of this issue.
 | 
			
		||||
func (issue *Issue) LoadAssignees() error {
 | 
			
		||||
	return issue.loadAssignees(x)
 | 
			
		||||
	return issue.loadAssignees(db.DefaultContext().Engine())
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// This loads all assignees of an issue
 | 
			
		||||
func (issue *Issue) loadAssignees(e Engine) (err error) {
 | 
			
		||||
func (issue *Issue) loadAssignees(e db.Engine) (err error) {
 | 
			
		||||
	// Reset maybe preexisting assignees
 | 
			
		||||
	issue.Assignees = []*User{}
 | 
			
		||||
 | 
			
		||||
@@ -51,7 +56,7 @@ func (issue *Issue) loadAssignees(e Engine) (err error) {
 | 
			
		||||
// User permissions must be verified elsewhere if required.
 | 
			
		||||
func GetAssigneeIDsByIssue(issueID int64) ([]int64, error) {
 | 
			
		||||
	userIDs := make([]int64, 0, 5)
 | 
			
		||||
	return userIDs, x.Table("issue_assignees").
 | 
			
		||||
	return userIDs, db.DefaultContext().Engine().Table("issue_assignees").
 | 
			
		||||
		Cols("assignee_id").
 | 
			
		||||
		Where("issue_id = ?", issueID).
 | 
			
		||||
		Distinct("assignee_id").
 | 
			
		||||
@@ -60,10 +65,10 @@ func GetAssigneeIDsByIssue(issueID int64) ([]int64, error) {
 | 
			
		||||
 | 
			
		||||
// GetAssigneesByIssue returns everyone assigned to that issue
 | 
			
		||||
func GetAssigneesByIssue(issue *Issue) (assignees []*User, err error) {
 | 
			
		||||
	return getAssigneesByIssue(x, issue)
 | 
			
		||||
	return getAssigneesByIssue(db.DefaultContext().Engine(), issue)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func getAssigneesByIssue(e Engine, issue *Issue) (assignees []*User, err error) {
 | 
			
		||||
func getAssigneesByIssue(e db.Engine, issue *Issue) (assignees []*User, err error) {
 | 
			
		||||
	err = issue.loadAssignees(e)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return assignees, err
 | 
			
		||||
@@ -74,22 +79,22 @@ func getAssigneesByIssue(e Engine, issue *Issue) (assignees []*User, err error)
 | 
			
		||||
 | 
			
		||||
// IsUserAssignedToIssue returns true when the user is assigned to the issue
 | 
			
		||||
func IsUserAssignedToIssue(issue *Issue, user *User) (isAssigned bool, err error) {
 | 
			
		||||
	return isUserAssignedToIssue(x, issue, user)
 | 
			
		||||
	return isUserAssignedToIssue(db.DefaultContext().Engine(), issue, user)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func isUserAssignedToIssue(e Engine, issue *Issue, user *User) (isAssigned bool, err error) {
 | 
			
		||||
func isUserAssignedToIssue(e db.Engine, issue *Issue, user *User) (isAssigned bool, err error) {
 | 
			
		||||
	return e.Get(&IssueAssignees{IssueID: issue.ID, AssigneeID: user.ID})
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// ClearAssigneeByUserID deletes all assignments of an user
 | 
			
		||||
func clearAssigneeByUserID(sess Engine, userID int64) (err error) {
 | 
			
		||||
func clearAssigneeByUserID(sess db.Engine, userID int64) (err error) {
 | 
			
		||||
	_, err = sess.Delete(&IssueAssignees{AssigneeID: userID})
 | 
			
		||||
	return
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// ToggleAssignee changes a user between assigned and not assigned for this issue, and make issue comment for it.
 | 
			
		||||
func (issue *Issue) ToggleAssignee(doer *User, assigneeID int64) (removed bool, comment *Comment, err error) {
 | 
			
		||||
	sess := x.NewSession()
 | 
			
		||||
	sess := db.DefaultContext().NewSession()
 | 
			
		||||
	defer sess.Close()
 | 
			
		||||
 | 
			
		||||
	if err := sess.Begin(); err != nil {
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user