fix(projects): recalculate project's position after dragging when position would be 0
This commit is contained in:
parent
a8b76772ff
commit
35964ce4a6
@ -17,6 +17,7 @@
|
|||||||
package models
|
package models
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"math"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
@ -771,6 +772,13 @@ func UpdateProject(s *xorm.Session, project *Project, auth web.Auth, updateProje
|
|||||||
colsToUpdate = append(colsToUpdate, "background_file_id", "background_blur_hash")
|
colsToUpdate = append(colsToUpdate, "background_file_id", "background_blur_hash")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if project.Position < 0.1 {
|
||||||
|
err = recalculateProjectPositions(s, project.ParentProjectID)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
wasFavorite, err := isFavorite(s, project.ID, auth, FavoriteKindProject)
|
wasFavorite, err := isFavorite(s, project.ID, auth, FavoriteKindProject)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -813,6 +821,34 @@ func UpdateProject(s *xorm.Session, project *Project, auth web.Auth, updateProje
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func recalculateProjectPositions(s *xorm.Session, parentProjectID int64) (err error) {
|
||||||
|
|
||||||
|
allProjects := []*Project{}
|
||||||
|
err = s.
|
||||||
|
Where("parent_project_id = ?", parentProjectID).
|
||||||
|
OrderBy("position asc").
|
||||||
|
Find(&allProjects)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
maxPosition := math.Pow(2, 32)
|
||||||
|
|
||||||
|
for i, project := range allProjects {
|
||||||
|
|
||||||
|
currentPosition := maxPosition / float64(len(allProjects)) * (float64(i + 1))
|
||||||
|
|
||||||
|
_, err = s.Cols("position").
|
||||||
|
Where("id = ?", project.ID).
|
||||||
|
Update(&Project{Position: currentPosition})
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// Update implements the update method of CRUDable
|
// Update implements the update method of CRUDable
|
||||||
// @Summary Updates a project
|
// @Summary Updates a project
|
||||||
// @Description Updates a project. This does not include adding a task (see below).
|
// @Description Updates a project. This does not include adding a task (see below).
|
||||||
|
Loading…
x
Reference in New Issue
Block a user