2023-05-06 12:49:40 +01:00
|
|
|
package board
|
|
|
|
|
|
|
|
import (
|
2023-12-12 12:47:58 +00:00
|
|
|
"sort"
|
2023-05-06 12:49:40 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// Status represents the status of the Kanban board.
|
|
|
|
type Status struct {
|
2024-01-23 19:42:35 +00:00
|
|
|
Identity
|
|
|
|
|
2023-05-06 12:49:40 +01:00
|
|
|
Name string
|
|
|
|
CardIds []int
|
|
|
|
Position int
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddCardID adds a card ID to the status' list of card IDs.
|
|
|
|
func (s *Status) AddCardID(cardID int) {
|
|
|
|
// Create a new list if it does not exist
|
|
|
|
// and then return.
|
|
|
|
if s.CardIds == nil {
|
|
|
|
s.CardIds = []int{cardID}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sort list if not sorted.
|
|
|
|
if !sort.IntsAreSorted(s.CardIds) {
|
|
|
|
sort.Ints(s.CardIds)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get index of the card's ID if it already exists in the list.
|
|
|
|
// Return if it already exists in the list
|
|
|
|
ind := sort.SearchInts(s.CardIds, cardID)
|
|
|
|
|
|
|
|
if ind < len(s.CardIds) && cardID == s.CardIds[ind] {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
s.CardIds = append(s.CardIds, cardID)
|
|
|
|
sort.Ints(s.CardIds)
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveCardID removes a card ID from the status' list of card IDs.
|
|
|
|
func (s *Status) RemoveCardID(cardID int) {
|
|
|
|
if s.CardIds == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sort list if not sorted.
|
|
|
|
if !sort.IntsAreSorted(s.CardIds) {
|
|
|
|
sort.Ints(s.CardIds)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get index of id.
|
|
|
|
// If the card ID is somehow not in the list, then ind
|
|
|
|
// will be the index where the id can be inserted.
|
|
|
|
ind := sort.SearchInts(s.CardIds, cardID)
|
|
|
|
|
|
|
|
if ind >= len(s.CardIds) || cardID != s.CardIds[ind] {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(s.CardIds) == 1 {
|
|
|
|
s.CardIds = nil
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// use append to eliminate the id from the new slice
|
|
|
|
s.CardIds = append(s.CardIds[:ind], s.CardIds[ind+1:]...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ByStatusPosition implements sort.Interface for []Status based on the status' position on the Kanban board.
|
|
|
|
type ByStatusPosition []Status
|
|
|
|
|
|
|
|
func (s ByStatusPosition) Len() int {
|
|
|
|
return len(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s ByStatusPosition) Swap(i, j int) {
|
|
|
|
s[i], s[j] = s[j], s[i]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s ByStatusPosition) Less(i, j int) bool {
|
|
|
|
return s[i].Position < s[j].Position
|
|
|
|
}
|
|
|
|
|
|
|
|
// defaultStatusList returns the default list of statuses.
|
|
|
|
func defaultStatusList() []Status {
|
|
|
|
return []Status{
|
|
|
|
{
|
2024-01-23 19:42:35 +00:00
|
|
|
Identity: Identity{-1},
|
2023-05-06 12:49:40 +01:00
|
|
|
Name: "To Do",
|
|
|
|
Position: 1,
|
|
|
|
CardIds: nil,
|
|
|
|
},
|
|
|
|
{
|
2024-01-23 19:42:35 +00:00
|
|
|
Identity: Identity{-1},
|
2023-05-06 12:49:40 +01:00
|
|
|
Name: "Doing",
|
|
|
|
Position: 2,
|
|
|
|
CardIds: nil,
|
|
|
|
},
|
|
|
|
{
|
2024-01-23 19:42:35 +00:00
|
|
|
Identity: Identity{-1},
|
2023-05-06 12:49:40 +01:00
|
|
|
Name: "Done",
|
|
|
|
Position: 3,
|
|
|
|
CardIds: nil,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|