diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2022-05-20 22:08:52 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-05-20 22:08:52 +0800 |
commit | fd7d83ace60258acf7139c4c787aa8af75b7ba8c (patch) | |
tree | 50038348ec10485f72344f3ac80324e04abc1283 /models/issue_assignees.go | |
parent | d81e31ad7826a81fc7139f329f250594610a274b (diff) | |
download | gitea-fd7d83ace60258acf7139c4c787aa8af75b7ba8c.tar.gz gitea-fd7d83ace60258acf7139c4c787aa8af75b7ba8c.zip |
Move almost all functions' parameter db.Engine to context.Context (#19748)
* Move almost all functions' parameter db.Engine to context.Context
* remove some unnecessary wrap functions
Diffstat (limited to 'models/issue_assignees.go')
-rw-r--r-- | models/issue_assignees.go | 70 |
1 files changed, 19 insertions, 51 deletions
diff --git a/models/issue_assignees.go b/models/issue_assignees.go index 0f1f7b6576..c6ccb6e9d2 100644 --- a/models/issue_assignees.go +++ b/models/issue_assignees.go @@ -25,20 +25,15 @@ func init() { } // LoadAssignees load assignees of this issue. -func (issue *Issue) LoadAssignees() error { - return issue.loadAssignees(db.GetEngine(db.DefaultContext)) -} - -// This loads all assignees of an issue -func (issue *Issue) loadAssignees(e db.Engine) (err error) { +func (issue *Issue) LoadAssignees(ctx context.Context) (err error) { // Reset maybe preexisting assignees issue.Assignees = []*user_model.User{} + issue.Assignee = nil - err = e.Table("`user`"). + err = db.GetEngine(ctx).Table("`user`"). Join("INNER", "issue_assignees", "assignee_id = `user`.id"). Where("issue_assignees.issue_id = ?", issue.ID). Find(&issue.Assignees) - if err != nil { return err } @@ -47,7 +42,6 @@ func (issue *Issue) loadAssignees(e db.Engine) (err error) { if len(issue.Assignees) > 0 { issue.Assignee = issue.Assignees[0] } - return } @@ -63,33 +57,9 @@ func GetAssigneeIDsByIssue(issueID int64) ([]int64, error) { Find(&userIDs) } -// GetAssigneesByIssue returns everyone assigned to that issue -func GetAssigneesByIssue(issue *Issue) (assignees []*user_model.User, err error) { - return getAssigneesByIssue(db.GetEngine(db.DefaultContext), issue) -} - -func getAssigneesByIssue(e db.Engine, issue *Issue) (assignees []*user_model.User, err error) { - err = issue.loadAssignees(e) - if err != nil { - return assignees, err - } - - return issue.Assignees, nil -} - // IsUserAssignedToIssue returns true when the user is assigned to the issue -func IsUserAssignedToIssue(issue *Issue, user *user_model.User) (isAssigned bool, err error) { - return isUserAssignedToIssue(db.GetEngine(db.DefaultContext), issue, user) -} - -func isUserAssignedToIssue(e db.Engine, issue *Issue, user *user_model.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 db.Engine, userID int64) (err error) { - _, err = sess.Delete(&IssueAssignees{AssigneeID: userID}) - return +func IsUserAssignedToIssue(ctx context.Context, issue *Issue, user *user_model.User) (isAssigned bool, err error) { + return db.GetByBean(ctx, &IssueAssignees{IssueID: issue.ID, AssigneeID: user.ID}) } // ToggleIssueAssignee changes a user between assigned and not assigned for this issue, and make issue comment for it. @@ -113,8 +83,7 @@ func ToggleIssueAssignee(issue *Issue, doer *user_model.User, assigneeID int64) } func toggleIssueAssignee(ctx context.Context, issue *Issue, doer *user_model.User, assigneeID int64, isCreate bool) (removed bool, comment *Comment, err error) { - sess := db.GetEngine(ctx) - removed, err = toggleUserAssignee(sess, issue, assigneeID) + removed, err = toggleUserAssignee(ctx, issue, assigneeID) if err != nil { return false, nil, fmt.Errorf("UpdateIssueUserByAssignee: %v", err) } @@ -147,39 +116,38 @@ func toggleIssueAssignee(ctx context.Context, issue *Issue, doer *user_model.Use } // toggles user assignee state in database -func toggleUserAssignee(e db.Engine, issue *Issue, assigneeID int64) (removed bool, err error) { +func toggleUserAssignee(ctx context.Context, issue *Issue, assigneeID int64) (removed bool, err error) { // Check if the user exists - assignee, err := user_model.GetUserByIDEngine(e, assigneeID) + assignee, err := user_model.GetUserByIDCtx(ctx, assigneeID) if err != nil { return false, err } // Check if the submitted user is already assigned, if yes delete him otherwise add him - var i int - for i = 0; i < len(issue.Assignees); i++ { + found := false + i := 0 + for ; i < len(issue.Assignees); i++ { if issue.Assignees[i].ID == assigneeID { + found = true break } } assigneeIn := IssueAssignees{AssigneeID: assigneeID, IssueID: issue.ID} - - toBeDeleted := i < len(issue.Assignees) - if toBeDeleted { - issue.Assignees = append(issue.Assignees[:i], issue.Assignees[i:]...) - _, err = e.Delete(assigneeIn) + if found { + issue.Assignees = append(issue.Assignees[:i], issue.Assignees[i+1:]...) + _, err = db.DeleteByBean(ctx, &assigneeIn) if err != nil { - return toBeDeleted, err + return found, err } } else { issue.Assignees = append(issue.Assignees, assignee) - _, err = e.Insert(assigneeIn) - if err != nil { - return toBeDeleted, err + if err = db.Insert(ctx, &assigneeIn); err != nil { + return found, err } } - return toBeDeleted, nil + return found, nil } // MakeIDsFromAPIAssigneesToAdd returns an array with all assignee IDs |