@@ -147,24 +147,38 @@ func MoveIssuesOnProjectColumn(ctx context.Context, column *Column, sortedIssueI
147147}
148148
149149func MoveIssueToColumnTail (ctx context.Context , issue * ProjectIssue , toColumn * Column ) error {
150- ctx , committer , err := db . TxContext (ctx )
150+ nextSorting , err := toColumn . getNextSorting (ctx )
151151 if err != nil {
152152 return err
153153 }
154- defer committer .Close ()
155154
156- num , err := toColumn .NumIssues (ctx )
157- if err != nil {
155+ return db .WithTx (ctx , func (ctx context.Context ) error {
156+ _ , err = db .GetEngine (ctx ).Exec ("UPDATE `project_issue` SET project_board_id=?, sorting=? WHERE issue_id=?" ,
157+ toColumn .ID , nextSorting , issue .IssueID )
158+
158159 return err
160+ })
161+ }
162+
163+ func (c * Column ) getNextSorting (ctx context.Context ) (int64 , error ) {
164+ res := struct {
165+ MaxSorting int64
166+ IssueCount int64
167+ }{}
168+
169+ if _ , err := db .GetEngine (ctx ).Select ("max(sorting) as max_sorting, count(*) as issue_count" ).
170+ Table ("project_issue" ).
171+ Where ("project_id=?" , c .ProjectID ).
172+ And ("project_board_id=?" , c .ID ).
173+ Get (& res ); err != nil {
174+ return 0 , err
159175 }
160176
161- _ , err = db .GetEngine (ctx ).Exec ("UPDATE `project_issue` SET project_board_id=?, sorting=? WHERE issue_id=?" ,
162- toColumn .ID , num , issue .IssueID )
163- if err != nil {
164- return err
177+ if res .IssueCount > 0 {
178+ return res .MaxSorting + 1 , nil
165179 }
166180
167- return committer . Commit ()
181+ return 0 , nil
168182}
169183
170184func (c * Column ) moveIssuesToAnotherColumn (ctx context.Context , newColumn * Column ) error {
@@ -176,15 +190,8 @@ func (c *Column) moveIssuesToAnotherColumn(ctx context.Context, newColumn *Colum
176190 return nil
177191 }
178192
179- res := struct {
180- MaxSorting int64
181- IssueCount int64
182- }{}
183- if _ , err := db .GetEngine (ctx ).Select ("max(sorting) as max_sorting, count(*) as issue_count" ).
184- Table ("project_issue" ).
185- Where ("project_id=?" , newColumn .ProjectID ).
186- And ("project_board_id=?" , newColumn .ID ).
187- Get (& res ); err != nil {
193+ nextSorting , err := newColumn .getNextSorting (ctx )
194+ if err != nil {
188195 return err
189196 }
190197
@@ -196,7 +203,6 @@ func (c *Column) moveIssuesToAnotherColumn(ctx context.Context, newColumn *Colum
196203 return nil
197204 }
198205
199- nextSorting := util .Iif (res .IssueCount > 0 , res .MaxSorting + 1 , 0 )
200206 return db .WithTx (ctx , func (ctx context.Context ) error {
201207 for i , issue := range issues {
202208 issue .ProjectColumnID = newColumn .ID
0 commit comments