55package repo
66
77import (
8+ "encoding/json"
89 "fmt"
910 "net/http"
1011 "net/url"
@@ -299,7 +300,6 @@ func ViewProject(ctx *context.Context) {
299300 ctx .ServerError ("LoadIssuesOfBoards" , err )
300301 return
301302 }
302- ctx .Data ["Issues" ] = issueList
303303
304304 linkedPrsMap := make (map [int64 ][]* models.Issue )
305305 for _ , issue := range issueList {
@@ -547,9 +547,8 @@ func SetDefaultProjectBoard(ctx *context.Context) {
547547 })
548548}
549549
550- // MoveIssueAcrossBoards move a card from one board to another in a project
551- func MoveIssueAcrossBoards (ctx * context.Context ) {
552-
550+ // MoveIssues moves or keeps issues in a column and sorts them inside that column
551+ func MoveIssues (ctx * context.Context ) {
553552 if ctx .User == nil {
554553 ctx .JSON (http .StatusForbidden , map [string ]string {
555554 "message" : "Only signed in users are allowed to perform this action." ,
@@ -564,59 +563,80 @@ func MoveIssueAcrossBoards(ctx *context.Context) {
564563 return
565564 }
566565
567- p , err := models .GetProjectByID (ctx .ParamsInt64 (":id" ))
566+ project , err := models .GetProjectByID (ctx .ParamsInt64 (":id" ))
568567 if err != nil {
569568 if models .IsErrProjectNotExist (err ) {
570- ctx .NotFound ("" , nil )
569+ ctx .NotFound ("ProjectNotExist " , nil )
571570 } else {
572571 ctx .ServerError ("GetProjectByID" , err )
573572 }
574573 return
575574 }
576- if p .RepoID != ctx .Repo .Repository .ID {
577- ctx .NotFound ("" , nil )
575+ if project .RepoID != ctx .Repo .Repository .ID {
576+ ctx .NotFound ("InvalidRepoID " , nil )
578577 return
579578 }
580579
581580 var board * models.ProjectBoard
582581
583582 if ctx .ParamsInt64 (":boardID" ) == 0 {
584-
585583 board = & models.ProjectBoard {
586584 ID : 0 ,
587- ProjectID : 0 ,
585+ ProjectID : project . ID ,
588586 Title : ctx .Tr ("repo.projects.type.uncategorized" ),
589587 }
590-
591588 } else {
589+ // column
592590 board , err = models .GetProjectBoard (ctx .ParamsInt64 (":boardID" ))
593591 if err != nil {
594592 if models .IsErrProjectBoardNotExist (err ) {
595- ctx .NotFound ("" , nil )
593+ ctx .NotFound ("ProjectBoardNotExist " , nil )
596594 } else {
597595 ctx .ServerError ("GetProjectBoard" , err )
598596 }
599597 return
600598 }
601- if board .ProjectID != p .ID {
602- ctx .NotFound ("" , nil )
599+ if board .ProjectID != project .ID {
600+ ctx .NotFound ("BoardNotInProject " , nil )
603601 return
604602 }
605603 }
606604
607- issue , err := models .GetIssueByID (ctx .ParamsInt64 (":index" ))
605+ type movedIssuesForm struct {
606+ Issues []struct {
607+ IssueID int64 `json:"issueID"`
608+ Sorting int64 `json:"sorting"`
609+ } `json:"issues"`
610+ }
611+
612+ form := & movedIssuesForm {}
613+ if err = json .NewDecoder (ctx .Req .Body ).Decode (& form ); err != nil {
614+ ctx .ServerError ("DecodeMovedIssuesForm" , err )
615+ }
616+
617+ issueIDs := make ([]int64 , 0 , len (form .Issues ))
618+ sortedIssueIDs := make (map [int64 ]int64 )
619+ for _ , issue := range form .Issues {
620+ issueIDs = append (issueIDs , issue .IssueID )
621+ sortedIssueIDs [issue .Sorting ] = issue .IssueID
622+ }
623+ movedIssues , err := models .GetIssuesByIDs (issueIDs )
608624 if err != nil {
609625 if models .IsErrIssueNotExist (err ) {
610- ctx .NotFound ("" , nil )
626+ ctx .NotFound ("IssueNotExisting " , nil )
611627 } else {
612628 ctx .ServerError ("GetIssueByID" , err )
613629 }
630+ return
631+ }
614632
633+ if len (movedIssues ) != len (form .Issues ) {
634+ ctx .ServerError ("IssuesNotFound" , err )
615635 return
616636 }
617637
618- if err : = models .MoveIssueAcrossProjectBoards ( issue , board ); err != nil {
619- ctx .ServerError ("MoveIssueAcrossProjectBoards " , err )
638+ if err = models .MoveIssuesOnProjectBoard ( board , sortedIssueIDs ); err != nil {
639+ ctx .ServerError ("MoveIssuesOnProjectBoard " , err )
620640 return
621641 }
622642
0 commit comments