diff --git a/internal/board/board.go b/internal/board/board.go index da580c3..a4cc16e 100644 --- a/internal/board/board.go +++ b/internal/board/board.go @@ -141,7 +141,7 @@ func (b *Board) CreateStatus(args StatusArgs) error { } status := Status{ - ID: -1, + Identity: Identity{ID: -1}, Name: name, Position: pos, CardIds: nil, @@ -280,7 +280,7 @@ func (b *Board) CreateCard(args CardArgs) (int, error) { boltItems := []db.BoltItem{ &Card{ - ID: -1, + Identity: Identity{ID: -1}, Title: args.NewTitle, Description: args.NewDescription, Created: timestamp, diff --git a/internal/board/card.go b/internal/board/card.go index 9913489..5b941a6 100644 --- a/internal/board/card.go +++ b/internal/board/card.go @@ -2,29 +2,9 @@ package board // Card represents a card on a Kanban board. type Card struct { - ID int + Identity + Title string Description string Created string } - -// SetID updates the ID of the Card value only if -// the ID is < 1 (i.e. unset). -func (c *Card) SetID(id int) error { - if id < 1 { - return InvalidIDError{id} - } - - if c.ID > 0 { - return IDAlreadySetError{} - } - - c.ID = id - - return nil -} - -// GetID returns the ID of the Card value. -func (c *Card) GetID() int { - return c.ID -} diff --git a/internal/board/card_lifecycle_test.go b/internal/board/card_lifecycle_test.go index db6bde3..0e554d1 100644 --- a/internal/board/card_lifecycle_test.go +++ b/internal/board/card_lifecycle_test.go @@ -227,7 +227,7 @@ func testUpdateCard(kanban board.Board, modifiedCardArgs board.UpdateCardArgs, t } want := board.Card{ - ID: modifiedCardArgs.CardID, + Identity: board.Identity{ID: modifiedCardArgs.CardID}, Title: modifiedCardArgs.NewTitle, Description: modifiedCardArgs.NewDescription, Created: timestamp, @@ -255,7 +255,7 @@ func testUpdateCardContent(kanban board.Board, modifiedCardArgs board.UpdateCard } want := board.Card{ - ID: modifiedCardArgs.CardID, + Identity: board.Identity{ID: modifiedCardArgs.CardID}, Title: expectedTitle, Description: expectedDescription, Created: timestamp, diff --git a/internal/board/identity.go b/internal/board/identity.go new file mode 100644 index 0000000..4f26a63 --- /dev/null +++ b/internal/board/identity.go @@ -0,0 +1,26 @@ +package board + +type Identity struct { + ID int +} + +// SetID updates the ID value with a valid ID value +// only if the ID is unset (i.e. < 1). +func (i *Identity) SetID(id int) error { + if id < 1 { + return InvalidIDError{id} + } + + if i.ID > 0 { + return IDAlreadySetError{} + } + + i.ID = id + + return nil +} + +// GetID returns the ID value. +func (i *Identity) GetID() int { + return i.ID +} diff --git a/internal/board/card_test.go b/internal/board/identity_test.go similarity index 72% rename from internal/board/card_test.go rename to internal/board/identity_test.go index c2171c4..10b15cc 100644 --- a/internal/board/card_test.go +++ b/internal/board/identity_test.go @@ -3,20 +3,16 @@ package board_test import ( "errors" "testing" - "time" "codeflow.dananglin.me.uk/apollo/pelican/internal/board" ) -func TestCardSetInvalidID(t *testing.T) { - card := board.Card{ - ID: -1, - Title: "Title", - Description: "Description", - Created: time.Now().Format(time.DateTime), +func TestSetInvalidID(t *testing.T) { + identity := board.Identity{ + ID: -1, } - err := card.SetID(-1000) + err := identity.SetID(-1000) switch { case err == nil: @@ -29,14 +25,11 @@ func TestCardSetInvalidID(t *testing.T) { } func TestCardSetExistingID(t *testing.T) { - card := board.Card{ - ID: 5, - Title: "Title", - Description: "Description", - Created: time.Now().Format(time.DateTime), + identity := board.Identity{ + ID: 5, } - err := card.SetID(10) + err := identity.SetID(10) switch { case err == nil: diff --git a/internal/board/status.go b/internal/board/status.go index 2a38fc0..4939ed1 100644 --- a/internal/board/status.go +++ b/internal/board/status.go @@ -6,32 +6,13 @@ import ( // Status represents the status of the Kanban board. type Status struct { - ID int + Identity + Name string CardIds []int Position int } -// UpdateID updates the ID of the Status value. -func (s *Status) SetID(id int) error { - if id < 1 { - return InvalidIDError{id} - } - - if s.ID > 0 { - return IDAlreadySetError{} - } - - s.ID = id - - return nil -} - -// GetID returns the ID of the Status value. -func (s *Status) GetID() int { - return s.ID -} - // 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 @@ -108,19 +89,19 @@ func (s ByStatusPosition) Less(i, j int) bool { func defaultStatusList() []Status { return []Status{ { - ID: -1, + Identity: Identity{-1}, Name: "To Do", Position: 1, CardIds: nil, }, { - ID: -1, + Identity: Identity{-1}, Name: "Doing", Position: 2, CardIds: nil, }, { - ID: -1, + Identity: Identity{-1}, Name: "Done", Position: 3, CardIds: nil, diff --git a/internal/board/status_lifecycle_test.go b/internal/board/status_lifecycle_test.go index 4ba0b52..691b3ed 100644 --- a/internal/board/status_lifecycle_test.go +++ b/internal/board/status_lifecycle_test.go @@ -143,7 +143,7 @@ func testUpdateStatus(kanban board.Board, statusID, expectedPosition int, newNam } want := board.Status{ - ID: statusID, + Identity: board.Identity{ID: statusID}, Name: newName, CardIds: nil, Position: expectedPosition, diff --git a/internal/db/database_test.go b/internal/db/database_test.go index 32d8361..83c598b 100644 --- a/internal/db/database_test.go +++ b/internal/db/database_test.go @@ -45,25 +45,25 @@ func testCreateStatusList(t *testing.T, database *bolt.DB) { newStatusList := []board.Status{ { - ID: -1, + Identity: board.Identity{ID: -1}, Name: "Backlog", CardIds: []int{1, 14, 9, 10}, Position: 1, }, { - ID: -1, + Identity: board.Identity{ID: -1}, Name: "Next", CardIds: []int{2, 5, 12}, Position: 2, }, { - ID: -1, + Identity: board.Identity{ID: -1}, Name: "In progress", CardIds: []int{3, 14}, Position: 3, }, { - ID: -1, + Identity: board.Identity{ID: -1}, Name: "Finished!", CardIds: []int{4, 6, 7, 8, 11, 13}, Position: 4, @@ -107,25 +107,25 @@ func testReadStatusList(t *testing.T, database *bolt.DB) { want := []board.Status{ { - ID: 1, + Identity: board.Identity{ID: 1}, Name: "Backlog", CardIds: []int{1, 14, 9, 10}, Position: 1, }, { - ID: 2, + Identity: board.Identity{ID: 2}, Name: "Next", CardIds: []int{2, 5, 12}, Position: 2, }, { - ID: 3, + Identity: board.Identity{ID: 3}, Name: "In progress", CardIds: []int{3, 14}, Position: 3, }, { - ID: 4, + Identity: board.Identity{ID: 4}, Name: "Finished!", CardIds: []int{4, 6, 7, 8, 11, 13}, Position: 4, @@ -163,7 +163,7 @@ func TestReadAndWriteCards(t *testing.T) { }() singleCard := board.Card{ - ID: -1, + Identity: board.Identity{ID: -1}, Title: "A test task.", Description: "This task should be completed.", } @@ -173,17 +173,17 @@ func TestReadAndWriteCards(t *testing.T) { manyCards := []board.Card{ { - ID: -1, + Identity: board.Identity{ID: -1}, Title: "Test card A.", Description: "This is test card A.", }, { - ID: -1, + Identity: board.Identity{ID: -1}, Title: "Test card B.", Description: "This is test card B.", }, { - ID: -1, + Identity: board.Identity{ID: -1}, Title: "Test card C.", Description: "This is test card C.", }, @@ -227,7 +227,7 @@ func testReadOneCard(t *testing.T, database *bolt.DB, cardID int) { } want := board.Card{ - ID: 1, + Identity: board.Identity{ID: 1}, Title: "A test task.", Description: "This task should be completed.", } @@ -282,17 +282,17 @@ func testReadManyCards(t *testing.T, database *bolt.DB, cardIDs []int) { want := []board.Card{ { - ID: 2, + Identity: board.Identity{ID: 2}, Title: "Test card A.", Description: "This is test card A.", }, { - ID: 3, + Identity: board.Identity{ID: 3}, Title: "Test card B.", Description: "This is test card B.", }, { - ID: 4, + Identity: board.Identity{ID: 4}, Title: "Test card C.", Description: "This is test card C.", }, @@ -330,7 +330,7 @@ func TestDeleteOneCard(t *testing.T) { // Create one card, get card ID. card := board.Card{ - ID: -1, + Identity: board.Identity{ID: -1}, Title: "Test card", Description: "", }