@@ -5,7 +5,6 @@ package project
55
66import (
77 "context"
8- "database/sql"
98 "errors"
109 "fmt"
1110 "regexp"
@@ -89,7 +88,7 @@ func (b *Board) GetIssues(ctx context.Context) ([]*ProjectIssue, error) {
8988 issues := make ([]* ProjectIssue , 0 , 5 )
9089 if err := db .GetEngine (ctx ).Where ("project_id=?" , b .ProjectID ).
9190 And ("project_board_id=?" , b .ID ).
92- OrderBy ("sorting" ).
91+ OrderBy ("sorting, id " ).
9392 Find (& issues ); err != nil {
9493 return nil , err
9594 }
@@ -173,29 +172,19 @@ func NewBoard(ctx context.Context, board *Board) error {
173172 if len (board .Color ) != 0 && ! BoardColorPattern .MatchString (board .Color ) {
174173 return fmt .Errorf ("bad color code: %s" , board .Color )
175174 }
176-
177- totalColumns , err := db .GetEngine (ctx ).Table ("project_board" ).
178- Where ("project_id=?" , board .ProjectID ).Count ()
179- if err != nil {
175+ res := struct {
176+ MaxSorting int64
177+ ColumnCount int64
178+ }{}
179+ if _ , err := db .GetEngine (ctx ).Select ("max(sorting) as MaxSorting, count(*) as ColumnCount" ).Table ("project_board" ).
180+ Where ("project_id=?" , board .ProjectID ).Get (& res ); err != nil {
180181 return err
181182 }
182-
183- if totalColumns >= maxProjectColumns {
183+ if res .ColumnCount >= maxProjectColumns {
184184 return fmt .Errorf ("NewBoard: maximum number of columns reached" )
185185 }
186-
187- if totalColumns > 0 {
188- var maxSorting sql.NullByte
189- if _ , err := db .GetEngine (ctx ).Select ("Max(sorting)" ).Table ("project_board" ).
190- Where ("project_id=?" , board .ProjectID ).Get (& maxSorting ); err != nil {
191- return err
192- }
193- if maxSorting .Valid {
194- board .Sorting = int8 (maxSorting .Byte ) + 1
195- }
196- }
197-
198- _ , err = db .GetEngine (ctx ).Insert (board )
186+ board .Sorting = int8 (util .Iif (res .MaxSorting > 0 , res .MaxSorting + 1 , 0 ))
187+ _ , err := db .GetEngine (ctx ).Insert (board )
199188 return err
200189}
201190
@@ -291,7 +280,7 @@ func UpdateBoard(ctx context.Context, board *Board) error {
291280// GetBoards fetches all boards related to a project
292281func (p * Project ) GetBoards (ctx context.Context ) (BoardList , error ) {
293282 boards := make ([]* Board , 0 , 5 )
294- if err := db .GetEngine (ctx ).Where ("project_id=?" , p .ID ).OrderBy ("sorting" ).Find (& boards ); err != nil {
283+ if err := db .GetEngine (ctx ).Where ("project_id=?" , p .ID ).OrderBy ("sorting, id " ).Find (& boards ); err != nil {
295284 return nil , err
296285 }
297286
0 commit comments