diff options
author | 6543 <6543@obermui.de> | 2021-01-15 21:29:32 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-01-15 22:29:32 +0200 |
commit | 3091600cc866bb5236be991e764ad113b8f542a1 (patch) | |
tree | 815ee7accac74290faeb8a24f9b08e5bab2f8920 /models | |
parent | c09e11d018c4a92dc83f58e6a6eacd6b085d3328 (diff) | |
download | gitea-3091600cc866bb5236be991e764ad113b8f542a1.tar.gz gitea-3091600cc866bb5236be991e764ad113b8f542a1.zip |
KanBan: be able to set default board (#14147)
Co-authored-by: silverwind <me@silverwind.io>
Co-authored-by: zeripath <art27@cantab.net>
Diffstat (limited to 'models')
-rw-r--r-- | models/project_board.go | 95 |
1 files changed, 75 insertions, 20 deletions
diff --git a/models/project_board.go b/models/project_board.go index 260fc8304b..8ffa218377 100644 --- a/models/project_board.go +++ b/models/project_board.go @@ -8,6 +8,7 @@ import ( "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/timeutil" + "xorm.io/builder" "xorm.io/xorm" ) @@ -164,22 +165,43 @@ func UpdateProjectBoard(board *ProjectBoard) error { func updateProjectBoard(e Engine, board *ProjectBoard) error { _, err := e.ID(board.ID).Cols( "title", - "default", ).Update(board) return err } // GetProjectBoards fetches all boards related to a project -func GetProjectBoards(projectID int64) ([]*ProjectBoard, error) { +// if no default board set, first board is a temporary "Uncategorized" board +func GetProjectBoards(projectID int64) (ProjectBoardList, error) { + return getProjectBoards(x, projectID) +} +func getProjectBoards(e Engine, projectID int64) ([]*ProjectBoard, error) { var boards = make([]*ProjectBoard, 0, 5) - sess := x.Where("project_id=?", projectID) - return boards, sess.Find(&boards) + if err := e.Where("project_id=? AND `default`=?", projectID, false).Find(&boards); err != nil { + return nil, err + } + + defaultB, err := getDefaultBoard(e, projectID) + if err != nil { + return nil, err + } + + return append([]*ProjectBoard{defaultB}, boards...), nil } -// GetUncategorizedBoard represents a board for issues not assigned to one -func GetUncategorizedBoard(projectID int64) (*ProjectBoard, error) { +// getDefaultBoard return default board and create a dummy if none exist +func getDefaultBoard(e Engine, projectID int64) (*ProjectBoard, error) { + var board ProjectBoard + exist, err := e.Where("project_id=? AND `default`=?", projectID, true).Get(&board) + if err != nil { + return nil, err + } + if exist { + return &board, nil + } + + // represents a board for issues not assigned to one return &ProjectBoard{ ProjectID: projectID, Title: "Uncategorized", @@ -187,22 +209,55 @@ func GetUncategorizedBoard(projectID int64) (*ProjectBoard, error) { }, nil } +// SetDefaultBoard represents a board for issues not assigned to one +// if boardID is 0 unset default +func SetDefaultBoard(projectID, boardID int64) error { + sess := x + + _, err := sess.Where(builder.Eq{ + "project_id": projectID, + "`default`": true, + }).Cols("`default`").Update(&ProjectBoard{Default: false}) + if err != nil { + return err + } + + if boardID > 0 { + _, err = sess.ID(boardID).Where(builder.Eq{"project_id": projectID}). + Cols("`default`").Update(&ProjectBoard{Default: true}) + } + + return err +} + // LoadIssues load issues assigned to this board func (b *ProjectBoard) LoadIssues() (IssueList, error) { - var boardID int64 - if !b.Default { - boardID = b.ID - - } else { - // Issues without ProjectBoardID - boardID = -1 - } - issues, err := Issues(&IssuesOptions{ - ProjectBoardID: boardID, - ProjectID: b.ProjectID, - }) - b.Issues = issues - return issues, err + issueList := make([]*Issue, 0, 10) + + if b.ID != 0 { + issues, err := Issues(&IssuesOptions{ + ProjectBoardID: b.ID, + ProjectID: b.ProjectID, + }) + if err != nil { + return nil, err + } + issueList = issues + } + + if b.Default { + issues, err := Issues(&IssuesOptions{ + ProjectBoardID: -1, // Issues without ProjectBoardID + ProjectID: b.ProjectID, + }) + if err != nil { + return nil, err + } + issueList = append(issueList, issues...) + } + + b.Issues = issueList + return issueList, nil } // LoadIssues load issues assigned to the boards |