refactor: add the Identity type
All checks were successful
/ test (pull_request) Successful in 32s
/ lint (pull_request) Successful in 36s

Add a new Identity type with the required methods so that now the board
types (Card, Status and soon Tag) can automatically satisfy the BoltItem
interface.
This commit is contained in:
Dan Anglin 2024-01-23 19:42:35 +00:00
parent 5189ebe7bb
commit 73547c49c6
Signed by: dananglin
GPG key ID: 0C1D44CFBEE68638
8 changed files with 62 additions and 82 deletions

View file

@ -141,7 +141,7 @@ func (b *Board) CreateStatus(args StatusArgs) error {
} }
status := Status{ status := Status{
ID: -1, Identity: Identity{ID: -1},
Name: name, Name: name,
Position: pos, Position: pos,
CardIds: nil, CardIds: nil,
@ -280,7 +280,7 @@ func (b *Board) CreateCard(args CardArgs) (int, error) {
boltItems := []db.BoltItem{ boltItems := []db.BoltItem{
&Card{ &Card{
ID: -1, Identity: Identity{ID: -1},
Title: args.NewTitle, Title: args.NewTitle,
Description: args.NewDescription, Description: args.NewDescription,
Created: timestamp, Created: timestamp,

View file

@ -2,29 +2,9 @@ package board
// Card represents a card on a Kanban board. // Card represents a card on a Kanban board.
type Card struct { type Card struct {
ID int Identity
Title string Title string
Description string Description string
Created 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
}

View file

@ -227,7 +227,7 @@ func testUpdateCard(kanban board.Board, modifiedCardArgs board.UpdateCardArgs, t
} }
want := board.Card{ want := board.Card{
ID: modifiedCardArgs.CardID, Identity: board.Identity{ID: modifiedCardArgs.CardID},
Title: modifiedCardArgs.NewTitle, Title: modifiedCardArgs.NewTitle,
Description: modifiedCardArgs.NewDescription, Description: modifiedCardArgs.NewDescription,
Created: timestamp, Created: timestamp,
@ -255,7 +255,7 @@ func testUpdateCardContent(kanban board.Board, modifiedCardArgs board.UpdateCard
} }
want := board.Card{ want := board.Card{
ID: modifiedCardArgs.CardID, Identity: board.Identity{ID: modifiedCardArgs.CardID},
Title: expectedTitle, Title: expectedTitle,
Description: expectedDescription, Description: expectedDescription,
Created: timestamp, Created: timestamp,

View file

@ -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
}

View file

@ -3,20 +3,16 @@ package board_test
import ( import (
"errors" "errors"
"testing" "testing"
"time"
"codeflow.dananglin.me.uk/apollo/pelican/internal/board" "codeflow.dananglin.me.uk/apollo/pelican/internal/board"
) )
func TestCardSetInvalidID(t *testing.T) { func TestSetInvalidID(t *testing.T) {
card := board.Card{ identity := board.Identity{
ID: -1, ID: -1,
Title: "Title",
Description: "Description",
Created: time.Now().Format(time.DateTime),
} }
err := card.SetID(-1000) err := identity.SetID(-1000)
switch { switch {
case err == nil: case err == nil:
@ -29,14 +25,11 @@ func TestCardSetInvalidID(t *testing.T) {
} }
func TestCardSetExistingID(t *testing.T) { func TestCardSetExistingID(t *testing.T) {
card := board.Card{ identity := board.Identity{
ID: 5, ID: 5,
Title: "Title",
Description: "Description",
Created: time.Now().Format(time.DateTime),
} }
err := card.SetID(10) err := identity.SetID(10)
switch { switch {
case err == nil: case err == nil:

View file

@ -6,32 +6,13 @@ import (
// Status represents the status of the Kanban board. // Status represents the status of the Kanban board.
type Status struct { type Status struct {
ID int Identity
Name string Name string
CardIds []int CardIds []int
Position 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. // AddCardID adds a card ID to the status' list of card IDs.
func (s *Status) AddCardID(cardID int) { func (s *Status) AddCardID(cardID int) {
// Create a new list if it does not exist // Create a new list if it does not exist
@ -108,19 +89,19 @@ func (s ByStatusPosition) Less(i, j int) bool {
func defaultStatusList() []Status { func defaultStatusList() []Status {
return []Status{ return []Status{
{ {
ID: -1, Identity: Identity{-1},
Name: "To Do", Name: "To Do",
Position: 1, Position: 1,
CardIds: nil, CardIds: nil,
}, },
{ {
ID: -1, Identity: Identity{-1},
Name: "Doing", Name: "Doing",
Position: 2, Position: 2,
CardIds: nil, CardIds: nil,
}, },
{ {
ID: -1, Identity: Identity{-1},
Name: "Done", Name: "Done",
Position: 3, Position: 3,
CardIds: nil, CardIds: nil,

View file

@ -143,7 +143,7 @@ func testUpdateStatus(kanban board.Board, statusID, expectedPosition int, newNam
} }
want := board.Status{ want := board.Status{
ID: statusID, Identity: board.Identity{ID: statusID},
Name: newName, Name: newName,
CardIds: nil, CardIds: nil,
Position: expectedPosition, Position: expectedPosition,

View file

@ -45,25 +45,25 @@ func testCreateStatusList(t *testing.T, database *bolt.DB) {
newStatusList := []board.Status{ newStatusList := []board.Status{
{ {
ID: -1, Identity: board.Identity{ID: -1},
Name: "Backlog", Name: "Backlog",
CardIds: []int{1, 14, 9, 10}, CardIds: []int{1, 14, 9, 10},
Position: 1, Position: 1,
}, },
{ {
ID: -1, Identity: board.Identity{ID: -1},
Name: "Next", Name: "Next",
CardIds: []int{2, 5, 12}, CardIds: []int{2, 5, 12},
Position: 2, Position: 2,
}, },
{ {
ID: -1, Identity: board.Identity{ID: -1},
Name: "In progress", Name: "In progress",
CardIds: []int{3, 14}, CardIds: []int{3, 14},
Position: 3, Position: 3,
}, },
{ {
ID: -1, Identity: board.Identity{ID: -1},
Name: "Finished!", Name: "Finished!",
CardIds: []int{4, 6, 7, 8, 11, 13}, CardIds: []int{4, 6, 7, 8, 11, 13},
Position: 4, Position: 4,
@ -107,25 +107,25 @@ func testReadStatusList(t *testing.T, database *bolt.DB) {
want := []board.Status{ want := []board.Status{
{ {
ID: 1, Identity: board.Identity{ID: 1},
Name: "Backlog", Name: "Backlog",
CardIds: []int{1, 14, 9, 10}, CardIds: []int{1, 14, 9, 10},
Position: 1, Position: 1,
}, },
{ {
ID: 2, Identity: board.Identity{ID: 2},
Name: "Next", Name: "Next",
CardIds: []int{2, 5, 12}, CardIds: []int{2, 5, 12},
Position: 2, Position: 2,
}, },
{ {
ID: 3, Identity: board.Identity{ID: 3},
Name: "In progress", Name: "In progress",
CardIds: []int{3, 14}, CardIds: []int{3, 14},
Position: 3, Position: 3,
}, },
{ {
ID: 4, Identity: board.Identity{ID: 4},
Name: "Finished!", Name: "Finished!",
CardIds: []int{4, 6, 7, 8, 11, 13}, CardIds: []int{4, 6, 7, 8, 11, 13},
Position: 4, Position: 4,
@ -163,7 +163,7 @@ func TestReadAndWriteCards(t *testing.T) {
}() }()
singleCard := board.Card{ singleCard := board.Card{
ID: -1, Identity: board.Identity{ID: -1},
Title: "A test task.", Title: "A test task.",
Description: "This task should be completed.", Description: "This task should be completed.",
} }
@ -173,17 +173,17 @@ func TestReadAndWriteCards(t *testing.T) {
manyCards := []board.Card{ manyCards := []board.Card{
{ {
ID: -1, Identity: board.Identity{ID: -1},
Title: "Test card A.", Title: "Test card A.",
Description: "This is test card A.", Description: "This is test card A.",
}, },
{ {
ID: -1, Identity: board.Identity{ID: -1},
Title: "Test card B.", Title: "Test card B.",
Description: "This is test card B.", Description: "This is test card B.",
}, },
{ {
ID: -1, Identity: board.Identity{ID: -1},
Title: "Test card C.", Title: "Test card C.",
Description: "This is 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{ want := board.Card{
ID: 1, Identity: board.Identity{ID: 1},
Title: "A test task.", Title: "A test task.",
Description: "This task should be completed.", Description: "This task should be completed.",
} }
@ -282,17 +282,17 @@ func testReadManyCards(t *testing.T, database *bolt.DB, cardIDs []int) {
want := []board.Card{ want := []board.Card{
{ {
ID: 2, Identity: board.Identity{ID: 2},
Title: "Test card A.", Title: "Test card A.",
Description: "This is test card A.", Description: "This is test card A.",
}, },
{ {
ID: 3, Identity: board.Identity{ID: 3},
Title: "Test card B.", Title: "Test card B.",
Description: "This is test card B.", Description: "This is test card B.",
}, },
{ {
ID: 4, Identity: board.Identity{ID: 4},
Title: "Test card C.", Title: "Test card C.",
Description: "This is test card C.", Description: "This is test card C.",
}, },
@ -330,7 +330,7 @@ func TestDeleteOneCard(t *testing.T) {
// Create one card, get card ID. // Create one card, get card ID.
card := board.Card{ card := board.Card{
ID: -1, Identity: board.Identity{ID: -1},
Title: "Test card", Title: "Test card",
Description: "", Description: "",
} }