update getNextSorting logic

Signed-off-by: a1012112796 <1012112796@qq.com>
This commit is contained in:
a1012112796 2024-06-04 08:48:53 +00:00
parent ae9d6292f1
commit a824db0d8b
No known key found for this signature in database
GPG Key ID: E5FB19032C2C2A64
2 changed files with 30 additions and 24 deletions

View File

@ -58,7 +58,7 @@ func (Column) TableName() string {
}
// NumIssues return counter of all issues assigned to the column
func (c *Column) NumIssues(ctx context.Context) (int64, error) {
func (c *Column) NumIssues(ctx context.Context) int {
total, err := db.GetEngine(ctx).Table("project_issue").
Where("project_id=?", c.ProjectID).
And("project_board_id=?", c.ID).
@ -66,9 +66,9 @@ func (c *Column) NumIssues(ctx context.Context) (int64, error) {
Cols("issue_id").
Count()
if err != nil {
return 0, err
return 0
}
return total, nil
return int(total)
}
func (c *Column) GetIssues(ctx context.Context) ([]*ProjectIssue, error) {

View File

@ -147,24 +147,38 @@ func MoveIssuesOnProjectColumn(ctx context.Context, column *Column, sortedIssueI
}
func MoveIssueToColumnTail(ctx context.Context, issue *ProjectIssue, toColumn *Column) error {
ctx, committer, err := db.TxContext(ctx)
if err != nil {
return err
}
defer committer.Close()
num, err := toColumn.NumIssues(ctx)
nextSorting, err := toColumn.getNextSorting(ctx)
if err != nil {
return err
}
_, err = db.GetEngine(ctx).Exec("UPDATE `project_issue` SET project_board_id=?, sorting=? WHERE issue_id=?",
toColumn.ID, num, issue.IssueID)
if err != nil {
return db.WithTx(ctx, func(ctx context.Context) error {
_, err = db.GetEngine(ctx).Exec("UPDATE `project_issue` SET project_board_id=?, sorting=? WHERE issue_id=?",
toColumn.ID, nextSorting, issue.IssueID)
return err
})
}
func (c *Column) getNextSorting(ctx context.Context) (int64, error) {
res := struct {
MaxSorting int64
IssueCount int64
}{}
if _, err := db.GetEngine(ctx).Select("max(sorting) as max_sorting, count(*) as issue_count").
Table("project_issue").
Where("project_id=?", c.ProjectID).
And("project_board_id=?", c.ID).
Get(&res); err != nil {
return 0, err
}
return committer.Commit()
if res.IssueCount > 0 {
return res.MaxSorting + 1, nil
}
return 0, nil
}
func (c *Column) moveIssuesToAnotherColumn(ctx context.Context, newColumn *Column) error {
@ -176,15 +190,8 @@ func (c *Column) moveIssuesToAnotherColumn(ctx context.Context, newColumn *Colum
return nil
}
res := struct {
MaxSorting int64
IssueCount int64
}{}
if _, err := db.GetEngine(ctx).Select("max(sorting) as max_sorting, count(*) as issue_count").
Table("project_issue").
Where("project_id=?", newColumn.ProjectID).
And("project_board_id=?", newColumn.ID).
Get(&res); err != nil {
nextSorting, err := newColumn.getNextSorting(ctx)
if err != nil {
return err
}
@ -196,7 +203,6 @@ func (c *Column) moveIssuesToAnotherColumn(ctx context.Context, newColumn *Colum
return nil
}
nextSorting := util.Iif(res.IssueCount > 0, res.MaxSorting+1, 0)
return db.WithTx(ctx, func(ctx context.Context) error {
for i, issue := range issues {
issue.ProjectColumnID = newColumn.ID