Follow delvh's suggestion

This commit is contained in:
Lunny Xiao 2024-04-29 13:51:45 +08:00
parent 235c9d4fd0
commit cca2e9523a
No known key found for this signature in database
GPG Key ID: C3B7C91B632F738A
4 changed files with 41 additions and 40 deletions

View File

@ -208,7 +208,7 @@ func deleteBoardByID(ctx context.Context, boardID int64) error {
return err
}
if err = board.moveIssuesToAnotherColumn(ctx, defaultColumn.ID); err != nil {
if err = board.moveIssuesToAnotherColumn(ctx, defaultColumn); err != nil {
return err
}
@ -330,11 +330,13 @@ func UpdateBoardSorting(ctx context.Context, bs BoardList) error {
})
}
func GetColumnsByIDs(ctx context.Context, columnsIDs []int64) (BoardList, error) {
func GetColumnsByIDs(ctx context.Context, projectID int64, columnsIDs []int64) (BoardList, error) {
columns := make([]*Board, 0, 5)
if err := db.GetEngine(ctx).In("id", columnsIDs).OrderBy("sorting").Find(&columns); err != nil {
if err := db.GetEngine(ctx).
Where("project_id =?", projectID).
In("id", columnsIDs).
OrderBy("sorting").Find(&columns); err != nil {
return nil, err
}
return columns, nil
}

View File

@ -5,9 +5,11 @@ package project
import (
"context"
"errors"
"fmt"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/modules/container"
"code.gitea.io/gitea/modules/log"
)
@ -102,34 +104,38 @@ func MoveIssuesOnProjectBoard(ctx context.Context, board *Board, sortedIssueIDs
})
}
func (b *Board) moveIssuesToAnotherColumn(ctx context.Context, columnID int64) error {
if b.ID == columnID {
func (b *Board) moveIssuesToAnotherColumn(ctx context.Context, newColumn *Board) error {
if b.ID == newColumn.ID {
return nil
}
_, err := db.GetEngine(ctx).Exec("UPDATE `project_issue` SET project_board_id = ? WHERE project_board_id = ? ", columnID, b.ID)
if b.ProjectID != newColumn.ProjectID {
return fmt.Errorf("columns have to be in the same project")
}
_, err := db.GetEngine(ctx).Exec("UPDATE `project_issue` SET project_board_id = ? WHERE project_board_id = ? ", newColumn.ID, b.ID)
return err
}
// MoveColumnsOnProject moves or keeps issues in a column and sorts them inside that column
// MoveColumnsOnProject moves or keeps column in a project and sorts them inside that column
func MoveColumnsOnProject(ctx context.Context, project *Project, sortedColumnIDs map[int64]int64) error {
return db.WithTx(ctx, func(ctx context.Context) error {
sess := db.GetEngine(ctx)
columnIDs := make([]int64, 0, len(sortedColumnIDs))
for _, columnID := range sortedColumnIDs {
columnIDs = append(columnIDs, columnID)
}
count, err := sess.Table(new(Board)).Where("project_id=?", project.ID).In("id", columnIDs).Count()
columnIDs := container.MapValues(sortedColumnIDs)
movedColumns, err := GetColumnsByIDs(ctx, project.ID, columnIDs)
if err != nil {
return err
}
if int(count) != len(sortedColumnIDs) {
return fmt.Errorf("all issues have to be added to a project first")
if len(movedColumns) != len(sortedColumnIDs) {
return errors.New("some columns do not exist")
}
for _, column := range movedColumns {
if column.ProjectID != project.ID {
return errors.New("Some column's projectID is not equal to project's ID")
}
}
for sorting, columnID := range sortedColumnIDs {
_, err = sess.Exec("UPDATE `project_board` SET sorting=? WHERE id=?", sorting, columnID)
if err != nil {
if _, err := sess.Exec("UPDATE `project_board` SET sorting=? WHERE id=?", sorting, columnID); err != nil {
return err
}
}

14
modules/container/map.go Normal file
View File

@ -0,0 +1,14 @@
// Copyright 2024 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package container
// MapValues returns a slice of all values in a map.
// TODO: remove this after Golang std lib has similar function but not golang.org/x/exp
func MapValues[K comparable, V any](m map[K]V) []V {
values := make([]V, 0, len(m))
for _, v := range m {
values = append(values, v)
}
return values
}

View File

@ -4,8 +4,6 @@
package project
import (
"errors"
issues_model "code.gitea.io/gitea/models/issues"
project_model "code.gitea.io/gitea/models/project"
"code.gitea.io/gitea/modules/json"
@ -41,32 +39,13 @@ func MoveColumns(ctx *context.Context) {
return
}
columnIDs := make([]int64, 0, len(form.Columns))
sortedColumnIDs := make(map[int64]int64)
for _, column := range form.Columns {
columnIDs = append(columnIDs, column.ColumnID)
sortedColumnIDs[column.Sorting] = column.ColumnID
}
movedColumns, err := project_model.GetColumnsByIDs(ctx, columnIDs)
if err != nil {
ctx.NotFoundOrServerError("GetColumnsByIDs", issues_model.IsErrIssueNotExist, err)
return
}
if len(movedColumns) != len(form.Columns) {
ctx.ServerError("some columns do not exist", errors.New("some columns do not exist"))
return
}
for _, column := range movedColumns {
if column.ProjectID != project.ID {
ctx.ServerError("Some column's projectID is not equal to project's ID", errors.New("Some column's projectID is not equal to project's ID"))
return
}
}
if err = project_model.MoveColumnsOnProject(ctx, project, sortedColumnIDs); err != nil {
ctx.ServerError("MoveColumnsOnProject", err)
ctx.NotFoundOrServerError("MoveColumnsOnProject", issues_model.IsErrIssueNotExist, err)
return
}