@@ -157,42 +157,52 @@ func NewColumn(ctx context.Context, column *Column) error {
157157// DeleteColumnByID removes all issues references to the project column.
158158func DeleteColumnByID (ctx context.Context , columnID int64 ) error {
159159 return db .WithTx (ctx , func (ctx context.Context ) error {
160- return deleteColumnByID (ctx , columnID )
161- })
162- }
160+ column , err := GetColumn (ctx , columnID )
161+ if err != nil {
162+ if IsErrProjectColumnNotExist (err ) {
163+ return nil
164+ }
163165
164- func deleteColumnByID (ctx context.Context , columnID int64 ) error {
165- column , err := GetColumn (ctx , columnID )
166- if err != nil {
167- if IsErrProjectColumnNotExist (err ) {
168- return nil
166+ return err
169167 }
170168
171- return err
172- }
169+ if column .Default {
170+ return errors .New ("deleteColumnByID: cannot delete default column" )
171+ }
173172
174- if column .Default {
175- return errors .New ("deleteColumnByID: cannot delete default column" )
176- }
173+ var numColumns int64
174+ if _ , err := db .GetEngine (ctx ).Table ("project_board" ).
175+ Where ("project_id=?" , column .ProjectID ).Count (& numColumns ); err != nil {
176+ return err
177+ }
177178
178- // move all issues to the default column
179- project , err := GetProjectByID (ctx , column .ProjectID )
180- if err != nil {
181- return err
182- }
183- defaultColumn , err := project .MustDefaultColumn (ctx )
184- if err != nil {
185- return err
186- }
179+ // last column with issues cannot be deleted
180+ if column .NumIssues > 0 && numColumns <= 1 {
181+ return errors .New ("deleteColumnByID: cannot delete last column with issues" )
182+ }
187183
188- if err = column .moveIssuesToAnotherColumn (ctx , defaultColumn ); err != nil {
189- return err
190- }
184+ if column .NumIssues > 0 {
185+ project , err := GetProjectByID (ctx , column .ProjectID )
186+ if err != nil {
187+ return err
188+ }
191189
192- if _ , err := db .GetEngine (ctx ).ID (column .ID ).NoAutoCondition ().Delete (column ); err != nil {
193- return err
194- }
195- return nil
190+ defaultColumn , err := project .getDefaultColumn (ctx )
191+ if err != nil {
192+ return err
193+ }
194+
195+ // move all issues to the default column
196+ if err = column .moveIssuesToAnotherColumn (ctx , defaultColumn ); err != nil {
197+ return err
198+ }
199+ }
200+
201+ if _ , err := db .GetEngine (ctx ).ID (column .ID ).NoAutoCondition ().Delete (column ); err != nil {
202+ return err
203+ }
204+ return nil
205+ })
196206}
197207
198208func deleteColumnByProjectID (ctx context.Context , projectID int64 ) error {
@@ -261,6 +271,20 @@ func (p *Project) getDefaultColumn(ctx context.Context) (*Column, error) {
261271 return nil , ErrProjectColumnNotExist {ColumnID : 0 }
262272}
263273
274+ func (p * Project ) createDefaultColumn (ctx context.Context ) (* Column , error ) {
275+ // create a default column if none is found
276+ column := Column {
277+ ProjectID : p .ID ,
278+ Default : true ,
279+ Title : "Uncategorized" ,
280+ CreatorID : p .CreatorID ,
281+ }
282+ if _ , err := db .GetEngine (ctx ).Insert (& column ); err != nil {
283+ return nil , err
284+ }
285+ return & column , nil
286+ }
287+
264288// MustDefaultColumn returns the default column for a project.
265289// If one exists, it is returned
266290// If none exists, the first column will be elevated to the default column of this project
@@ -286,17 +310,7 @@ func (p *Project) MustDefaultColumn(ctx context.Context) (*Column, error) {
286310 return & column , nil
287311 }
288312
289- // create a default column if none is found
290- column = Column {
291- ProjectID : p .ID ,
292- Default : true ,
293- Title : "Uncategorized" ,
294- CreatorID : p .CreatorID ,
295- }
296- if _ , err := db .GetEngine (ctx ).Insert (& column ); err != nil {
297- return nil , err
298- }
299- return & column , nil
313+ return p .createDefaultColumn (ctx )
300314}
301315
302316// SetDefaultColumn represents a column for issues not assigned to one
0 commit comments