@@ -5,7 +5,6 @@ package project
5
5
6
6
import (
7
7
"context"
8
- "database/sql"
9
8
"errors"
10
9
"fmt"
11
10
"regexp"
@@ -89,7 +88,7 @@ func (b *Board) GetIssues(ctx context.Context) ([]*ProjectIssue, error) {
89
88
issues := make ([]* ProjectIssue , 0 , 5 )
90
89
if err := db .GetEngine (ctx ).Where ("project_id=?" , b .ProjectID ).
91
90
And ("project_board_id=?" , b .ID ).
92
- OrderBy ("sorting" ).
91
+ OrderBy ("sorting, id " ).
93
92
Find (& issues ); err != nil {
94
93
return nil , err
95
94
}
@@ -173,29 +172,19 @@ func NewBoard(ctx context.Context, board *Board) error {
173
172
if len (board .Color ) != 0 && ! BoardColorPattern .MatchString (board .Color ) {
174
173
return fmt .Errorf ("bad color code: %s" , board .Color )
175
174
}
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 {
180
181
return err
181
182
}
182
-
183
- if totalColumns >= maxProjectColumns {
183
+ if res .ColumnCount >= maxProjectColumns {
184
184
return fmt .Errorf ("NewBoard: maximum number of columns reached" )
185
185
}
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 )
199
188
return err
200
189
}
201
190
@@ -291,7 +280,7 @@ func UpdateBoard(ctx context.Context, board *Board) error {
291
280
// GetBoards fetches all boards related to a project
292
281
func (p * Project ) GetBoards (ctx context.Context ) (BoardList , error ) {
293
282
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 {
295
284
return nil , err
296
285
}
297
286
0 commit comments