checkpoint: added functionality to delete statuses

This commit is contained in:
Dan Anglin 2024-01-17 20:56:21 +00:00
parent f116562a5f
commit 87afaf0741
Signed by: dananglin
GPG key ID: 0C1D44CFBEE68638
3 changed files with 40 additions and 10 deletions

View file

@ -160,11 +160,11 @@ type UpdateStatusArgs struct {
StatusArgs StatusArgs
} }
// UpdateStatus modifies an existing status in the db. // UpdateStatus modifies an existing status in the database.
func (b *Board) UpdateStatus(args UpdateStatusArgs) error { func (b *Board) UpdateStatus(args UpdateStatusArgs) error {
status, err := b.Status(args.StatusID) status, err := b.Status(args.StatusID)
if err != nil { if err != nil {
return fmt.Errorf("unable to retrieve the status from the db. %w", err) return fmt.Errorf("unable to retrieve the status from the database. %w", err)
} }
if len(args.Name) > 0 { if len(args.Name) > 0 {
@ -182,10 +182,25 @@ func (b *Board) UpdateStatus(args UpdateStatusArgs) error {
return nil return nil
} }
// TODO: Finish implementation. // DeleteStatus deletes a status from the database.
// func (b *Board) DeleteStatus() error { // A status can only be deleted if it does not contain any cards.
// return nil // TODO: Create two test cases for this function.
// } func (b *Board) DeleteStatus(statusID int) error {
status, err := b.Status(statusID)
if err != nil {
return fmt.Errorf("unable to retrieve the status from the database; %w", err)
}
if len(status.CardIds) > 0 {
return StatusNotEmptyError{ID: statusID}
}
if err := db.Delete(b.db, db.StatusBucket, statusID); err != nil {
return fmt.Errorf("unable to delete the status from the database; %w", err)
}
return nil
}
type MoveToStatusArgs struct { type MoveToStatusArgs struct {
CardID int CardID int
@ -346,18 +361,18 @@ type DeleteCardArgs struct {
// DeleteCard deletes a card from the database. // DeleteCard deletes a card from the database.
func (b *Board) DeleteCard(args DeleteCardArgs) error { func (b *Board) DeleteCard(args DeleteCardArgs) error {
if err := db.Delete(b.db, db.CardBucket, args.CardID); err != nil { if err := db.Delete(b.db, db.CardBucket, args.CardID); err != nil {
return fmt.Errorf("unable to delete the card from the database, %w", err) return fmt.Errorf("unable to delete the card from the database; %w", err)
} }
status, err := b.Status(args.StatusID) status, err := b.Status(args.StatusID)
if err != nil { if err != nil {
return fmt.Errorf("unable to read Status '%d' from the database, %w", args.StatusID, err) return fmt.Errorf("unable to read Status '%d' from the database; %w", args.StatusID, err)
} }
status.RemoveCardID(args.CardID) status.RemoveCardID(args.CardID)
if _, err := db.Write(b.db, db.StatusBucket, &status); err != nil { if _, err := db.Write(b.db, db.StatusBucket, &status); err != nil {
return fmt.Errorf("unable to update the status in the database, %w", err) return fmt.Errorf("unable to update the status in the database; %w", err)
} }
return nil return nil

View file

@ -19,6 +19,14 @@ func (e StatusNotExistError) Error() string {
return fmt.Sprintf("status ID '%d' does not exist in the database", e.ID) return fmt.Sprintf("status ID '%d' does not exist in the database", e.ID)
} }
type StatusNotEmptyError struct {
ID int
}
func (e StatusNotEmptyError) Error() string {
return fmt.Sprintf("status ID '%d' must contain no cards before deletion", e.ID)
}
// Status represents the status of the Kanban board. // Status represents the status of the Kanban board.
type Status struct { type Status struct {
ID int ID int

View file

@ -53,7 +53,8 @@ func TestStatusLifecycle(t *testing.T) {
t.Run("Test Status Update (In Progress)", testUpdateStatus(kanban, 2, 2, "In Progress")) t.Run("Test Status Update (In Progress)", testUpdateStatus(kanban, 2, 2, "In Progress"))
t.Run("Test Status Update (Backlog)", testUpdateStatus(kanban, 1, 1, "Backlog")) t.Run("Test Status Update (Backlog)", testUpdateStatus(kanban, 1, 1, "Backlog"))
// (TODO: Rearranging statuses still needs to be implemented) Rearrange the board so the order is To Do, On Hold, In Progress, Done // (TODO: Rearranging statuses still needs to be implemented)
// Rearrange the board so the order is: Backlog, Next, In Progress, On Hold, Done
t.Logf("Let us now try moving a card from one status to another...") t.Logf("Let us now try moving a card from one status to another...")
t.Run("Test Move Card To Status", testMoveCardToStatus(kanban)) t.Run("Test Move Card To Status", testMoveCardToStatus(kanban))
@ -202,3 +203,9 @@ func testMoveCardToStatus(kanban board.Board) func(t *testing.T) {
} }
} }
} }
func testDeleteStatus(kanban board.Board) func(t *testing.T) {
return func(t *testing.T) {
}
}