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{
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,

View file

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

View file

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

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 (
"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:

View file

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

View file

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

View file

@ -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: "",
}