delete
This commit is contained in:
parent
b39a2dee48
commit
662d8de191
|
@ -32,7 +32,7 @@ func NewAdd(localRepo storage.LocalID, eventRepo storage.Event, syncRepo storage
|
|||
}
|
||||
}
|
||||
|
||||
func (add *Add) Parse(main []string, flags map[string]string) error {
|
||||
func (add *Add) Execute(main []string, flags map[string]string) error {
|
||||
if len(main) == 0 || main[0] != "add" {
|
||||
return ErrWrongCommand
|
||||
}
|
||||
|
@ -69,10 +69,10 @@ func (add *Add) Parse(main []string, flags map[string]string) error {
|
|||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
return add.do()
|
||||
}
|
||||
|
||||
func (add *Add) Do() error {
|
||||
func (add *Add) do() error {
|
||||
as := add.argSet
|
||||
start := as.GetTime(FlagOn)
|
||||
if as.IsSet(FlagAt) {
|
||||
|
|
|
@ -22,16 +22,15 @@ func TestAdd(t *testing.T) {
|
|||
aDateAndTime := time.Date(2024, 11, 2, 12, 0, 0, 0, time.UTC)
|
||||
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
main []string
|
||||
flags map[string]string
|
||||
expParseErr bool
|
||||
expEvent item.Event
|
||||
expDoErr bool
|
||||
name string
|
||||
main []string
|
||||
flags map[string]string
|
||||
expErr bool
|
||||
expEvent item.Event
|
||||
}{
|
||||
{
|
||||
name: "empty",
|
||||
expParseErr: true,
|
||||
name: "empty",
|
||||
expErr: true,
|
||||
},
|
||||
{
|
||||
name: "title missing",
|
||||
|
@ -39,12 +38,12 @@ func TestAdd(t *testing.T) {
|
|||
flags: map[string]string{
|
||||
command.FlagOn: aDateStr,
|
||||
},
|
||||
expParseErr: true,
|
||||
expErr: true,
|
||||
},
|
||||
{
|
||||
name: "date missing",
|
||||
main: []string{"add", "some", "title"},
|
||||
expParseErr: true,
|
||||
name: "date missing",
|
||||
main: []string{"add", "some", "title"},
|
||||
expErr: true,
|
||||
},
|
||||
{
|
||||
name: "only date",
|
||||
|
@ -101,7 +100,7 @@ func TestAdd(t *testing.T) {
|
|||
command.FlagOn: aDateStr,
|
||||
command.FlagFor: anHourStr,
|
||||
},
|
||||
expParseErr: true,
|
||||
expErr: true,
|
||||
},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
|
@ -109,21 +108,14 @@ func TestAdd(t *testing.T) {
|
|||
localRepo := memory.NewLocalID()
|
||||
syncRepo := memory.NewSync()
|
||||
cmd := command.NewAdd(localRepo, eventRepo, syncRepo)
|
||||
actParseErr := cmd.Parse(tc.main, tc.flags) != nil
|
||||
if tc.expParseErr != actParseErr {
|
||||
t.Errorf("exp %v, got %v", tc.expParseErr, actParseErr)
|
||||
actParseErr := cmd.Execute(tc.main, tc.flags) != nil
|
||||
if tc.expErr != actParseErr {
|
||||
t.Errorf("exp %v, got %v", tc.expErr, actParseErr)
|
||||
}
|
||||
if tc.expParseErr {
|
||||
if tc.expErr {
|
||||
return
|
||||
}
|
||||
|
||||
actDoErr := cmd.Do() != nil
|
||||
if tc.expDoErr != actDoErr {
|
||||
t.Errorf("exp %v, got %v", tc.expDoErr, actDoErr)
|
||||
}
|
||||
if tc.expDoErr {
|
||||
return
|
||||
}
|
||||
actEvents, err := eventRepo.FindAll()
|
||||
if err != nil {
|
||||
t.Errorf("exp nil, got %v", err)
|
||||
|
|
|
@ -14,8 +14,7 @@ const (
|
|||
)
|
||||
|
||||
type Command interface {
|
||||
Parse([]string, map[string]string) error
|
||||
Do() error
|
||||
Execute([]string, map[string]string) error
|
||||
}
|
||||
|
||||
type CLI struct {
|
||||
|
@ -28,14 +27,12 @@ func (cli *CLI) Run(args []string) error {
|
|||
return err
|
||||
}
|
||||
for _, c := range cli.Commands {
|
||||
err := c.Parse(main, flags)
|
||||
err := c.Execute(main, flags)
|
||||
switch {
|
||||
case errors.Is(err, ErrWrongCommand):
|
||||
continue
|
||||
case err != nil:
|
||||
return err
|
||||
default:
|
||||
return c.Do()
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -2,39 +2,47 @@ package command
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
"github.com/urfave/cli/v2"
|
||||
"go-mod.ewintr.nl/planner/plan/storage"
|
||||
)
|
||||
|
||||
var DeleteCmd = &cli.Command{
|
||||
Name: "delete",
|
||||
Usage: "Delete an event",
|
||||
Flags: []cli.Flag{
|
||||
&cli.IntFlag{
|
||||
Name: "localID",
|
||||
Aliases: []string{"l"},
|
||||
Usage: "The local id of the event",
|
||||
Required: true,
|
||||
},
|
||||
},
|
||||
type Delete struct {
|
||||
localIDRepo storage.LocalID
|
||||
eventRepo storage.Event
|
||||
syncRepo storage.Sync
|
||||
localID int
|
||||
}
|
||||
|
||||
func NewDeleteCmd(localRepo storage.LocalID, eventRepo storage.Event, syncRepo storage.Sync) *cli.Command {
|
||||
DeleteCmd.Action = func(cCtx *cli.Context) error {
|
||||
return Delete(localRepo, eventRepo, syncRepo, cCtx.Int("localID"))
|
||||
func NewDelete(localIDRepo storage.LocalID, eventRepo storage.Event, syncRepo storage.Sync) Command {
|
||||
return &Delete{
|
||||
localIDRepo: localIDRepo,
|
||||
eventRepo: eventRepo,
|
||||
syncRepo: syncRepo,
|
||||
}
|
||||
return DeleteCmd
|
||||
}
|
||||
|
||||
func Delete(localRepo storage.LocalID, eventRepo storage.Event, syncRepo storage.Sync, localID int) error {
|
||||
func (del *Delete) Execute(main []string, flags map[string]string) error {
|
||||
if len(main) < 2 || main[0] != "delete" {
|
||||
return ErrWrongCommand
|
||||
}
|
||||
localID, err := strconv.Atoi(main[1])
|
||||
if err != nil {
|
||||
return fmt.Errorf("not a local id: %v", main[1])
|
||||
}
|
||||
del.localID = localID
|
||||
|
||||
return del.do()
|
||||
}
|
||||
|
||||
func (del *Delete) do() error {
|
||||
var id string
|
||||
idMap, err := localRepo.FindAll()
|
||||
idMap, err := del.localIDRepo.FindAll()
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not get local ids: %v", err)
|
||||
}
|
||||
for eid, lid := range idMap {
|
||||
if localID == lid {
|
||||
if del.localID == lid {
|
||||
id = eid
|
||||
}
|
||||
}
|
||||
|
@ -42,11 +50,11 @@ func Delete(localRepo storage.LocalID, eventRepo storage.Event, syncRepo storage
|
|||
return fmt.Errorf("could not find local id")
|
||||
}
|
||||
|
||||
if err := eventRepo.Delete(id); err != nil {
|
||||
if err := del.eventRepo.Delete(id); err != nil {
|
||||
return fmt.Errorf("could not delete event: %v", err)
|
||||
}
|
||||
|
||||
e, err := eventRepo.Find(id)
|
||||
e, err := del.eventRepo.Find(id)
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not get event: %v", err)
|
||||
}
|
||||
|
@ -55,7 +63,7 @@ func Delete(localRepo storage.LocalID, eventRepo storage.Event, syncRepo storage
|
|||
if err != nil {
|
||||
return fmt.Errorf("could not convert event to sync item: %v", err)
|
||||
}
|
||||
if err := syncRepo.Store(it); err != nil {
|
||||
if err := del.syncRepo.Store(it); err != nil {
|
||||
return fmt.Errorf("could not store sync item: %v", err)
|
||||
}
|
||||
return nil
|
||||
|
|
|
@ -33,7 +33,7 @@ func NewUpdate(localIDRepo storage.LocalID, eventRepo storage.Event, syncRepo st
|
|||
}
|
||||
}
|
||||
|
||||
func (update *Update) Parse(main []string, flags map[string]string) error {
|
||||
func (update *Update) Execute(main []string, flags map[string]string) error {
|
||||
if len(main) < 2 || main[0] != "update" {
|
||||
return ErrWrongCommand
|
||||
}
|
||||
|
@ -57,10 +57,10 @@ func (update *Update) Parse(main []string, flags map[string]string) error {
|
|||
}
|
||||
update.argSet = as
|
||||
|
||||
return nil
|
||||
return update.do()
|
||||
}
|
||||
|
||||
func (update *Update) Do() error {
|
||||
func (update *Update) do() error {
|
||||
as := update.argSet
|
||||
var id string
|
||||
idMap, err := update.localIDRepo.FindAll()
|
||||
|
|
|
@ -11,7 +11,7 @@ import (
|
|||
"go-mod.ewintr.nl/planner/plan/storage/memory"
|
||||
)
|
||||
|
||||
func TestUpdate(t *testing.T) {
|
||||
func TestUpdateExecute(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
eid := "c"
|
||||
|
@ -22,28 +22,27 @@ func TestUpdate(t *testing.T) {
|
|||
}
|
||||
title := "title"
|
||||
start := time.Date(2024, 10, 6, 10, 0, 0, 0, time.UTC)
|
||||
// twoHour, err := time.ParseDuration("2h")
|
||||
twoHour, err := time.ParseDuration("2h")
|
||||
if err != nil {
|
||||
t.Errorf("exp nil, got %v", err)
|
||||
}
|
||||
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
localID int
|
||||
main []string
|
||||
flags map[string]string
|
||||
expEvent item.Event
|
||||
expParseErr bool
|
||||
expDoErr bool
|
||||
name string
|
||||
localID int
|
||||
main []string
|
||||
flags map[string]string
|
||||
expEvent item.Event
|
||||
expErr bool
|
||||
}{
|
||||
{
|
||||
name: "no args",
|
||||
expParseErr: true,
|
||||
name: "no args",
|
||||
expErr: true,
|
||||
},
|
||||
{
|
||||
name: "not found",
|
||||
localID: 1,
|
||||
expParseErr: true,
|
||||
name: "not found",
|
||||
localID: 1,
|
||||
expErr: true,
|
||||
},
|
||||
{
|
||||
name: "name",
|
||||
|
@ -58,91 +57,98 @@ func TestUpdate(t *testing.T) {
|
|||
},
|
||||
},
|
||||
},
|
||||
// {
|
||||
// name: "invalid on",
|
||||
// localID: lid,
|
||||
// flags: map[string]string{
|
||||
// "on": "invalid",
|
||||
// },
|
||||
// expParseErr: true,
|
||||
// },
|
||||
// {
|
||||
// name: "on",
|
||||
// localID: lid,
|
||||
// flags: map[string]string{
|
||||
// "on": "2024-10-02",
|
||||
// },
|
||||
// expEvent: item.Event{
|
||||
// ID: eid,
|
||||
// EventBody: item.EventBody{
|
||||
// Title: title,
|
||||
// Start: time.Date(2024, 10, 2, 10, 0, 0, 0, time.UTC),
|
||||
// Duration: oneHour,
|
||||
// },
|
||||
// },
|
||||
// },
|
||||
// {
|
||||
// name: "invalid at",
|
||||
// localID: lid,
|
||||
// flags: map[string]string{
|
||||
// "at": "invalid",
|
||||
// },
|
||||
// expParseErr: true,
|
||||
// },
|
||||
// {
|
||||
// name: "at",
|
||||
// localID: lid,
|
||||
// flags: map[string]string{
|
||||
// "at": "11:00",
|
||||
// },
|
||||
// expEvent: item.Event{
|
||||
// ID: eid,
|
||||
// EventBody: item.EventBody{
|
||||
// Title: title,
|
||||
// Start: time.Date(2024, 10, 6, 11, 0, 0, 0, time.UTC),
|
||||
// Duration: oneHour,
|
||||
// },
|
||||
// },
|
||||
// },
|
||||
// {
|
||||
// name: "on and at",
|
||||
// localID: lid,
|
||||
// flags: map[string]string{
|
||||
// "on": "2024-10-02",
|
||||
// "at": "11:00",
|
||||
// },
|
||||
// expEvent: item.Event{
|
||||
// ID: eid,
|
||||
// EventBody: item.EventBody{
|
||||
// Title: title,
|
||||
// Start: time.Date(2024, 10, 2, 11, 0, 0, 0, time.UTC),
|
||||
// Duration: oneHour,
|
||||
// },
|
||||
// },
|
||||
// },
|
||||
// {
|
||||
// name: "invalid for",
|
||||
// localID: lid,
|
||||
// flags: map[string]string{
|
||||
// "for": "invalid",
|
||||
// },
|
||||
// expParseErr: true,
|
||||
// },
|
||||
// {
|
||||
// name: "for",
|
||||
// localID: lid,
|
||||
// flags: map[string]string{
|
||||
// "for": "2h",
|
||||
// },
|
||||
// expEvent: item.Event{
|
||||
// ID: eid,
|
||||
// EventBody: item.EventBody{
|
||||
// Title: title,
|
||||
// Start: time.Date(2024, 10, 6, 10, 0, 0, 0, time.UTC),
|
||||
// Duration: twoHour,
|
||||
// },
|
||||
// },
|
||||
// },
|
||||
{
|
||||
name: "invalid on",
|
||||
localID: lid,
|
||||
main: []string{"update", fmt.Sprintf("%d", lid)},
|
||||
flags: map[string]string{
|
||||
"on": "invalid",
|
||||
},
|
||||
expErr: true,
|
||||
},
|
||||
{
|
||||
name: "on",
|
||||
localID: lid,
|
||||
main: []string{"update", fmt.Sprintf("%d", lid)},
|
||||
flags: map[string]string{
|
||||
"on": "2024-10-02",
|
||||
},
|
||||
expEvent: item.Event{
|
||||
ID: eid,
|
||||
EventBody: item.EventBody{
|
||||
Title: title,
|
||||
Start: time.Date(2024, 10, 2, 10, 0, 0, 0, time.UTC),
|
||||
Duration: oneHour,
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "invalid at",
|
||||
localID: lid,
|
||||
main: []string{"update", fmt.Sprintf("%d", lid)},
|
||||
flags: map[string]string{
|
||||
"at": "invalid",
|
||||
},
|
||||
expErr: true,
|
||||
},
|
||||
{
|
||||
name: "at",
|
||||
localID: lid,
|
||||
main: []string{"update", fmt.Sprintf("%d", lid)},
|
||||
flags: map[string]string{
|
||||
"at": "11:00",
|
||||
},
|
||||
expEvent: item.Event{
|
||||
ID: eid,
|
||||
EventBody: item.EventBody{
|
||||
Title: title,
|
||||
Start: time.Date(2024, 10, 6, 11, 0, 0, 0, time.UTC),
|
||||
Duration: oneHour,
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "on and at",
|
||||
localID: lid,
|
||||
main: []string{"update", fmt.Sprintf("%d", lid)},
|
||||
flags: map[string]string{
|
||||
"on": "2024-10-02",
|
||||
"at": "11:00",
|
||||
},
|
||||
expEvent: item.Event{
|
||||
ID: eid,
|
||||
EventBody: item.EventBody{
|
||||
Title: title,
|
||||
Start: time.Date(2024, 10, 2, 11, 0, 0, 0, time.UTC),
|
||||
Duration: oneHour,
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "invalid for",
|
||||
localID: lid,
|
||||
main: []string{"update", fmt.Sprintf("%d", lid)},
|
||||
flags: map[string]string{
|
||||
"for": "invalid",
|
||||
},
|
||||
expErr: true,
|
||||
},
|
||||
{
|
||||
name: "for",
|
||||
localID: lid,
|
||||
main: []string{"update", fmt.Sprintf("%d", lid)},
|
||||
flags: map[string]string{
|
||||
"for": "2h",
|
||||
},
|
||||
expEvent: item.Event{
|
||||
ID: eid,
|
||||
EventBody: item.EventBody{
|
||||
Title: title,
|
||||
Start: time.Date(2024, 10, 6, 10, 0, 0, 0, time.UTC),
|
||||
Duration: twoHour,
|
||||
},
|
||||
},
|
||||
},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
eventRepo := memory.NewEvent()
|
||||
|
@ -163,19 +169,11 @@ func TestUpdate(t *testing.T) {
|
|||
}
|
||||
|
||||
cmd := command.NewUpdate(localIDRepo, eventRepo, syncRepo)
|
||||
actParseErr := cmd.Parse(tc.main, tc.flags) != nil
|
||||
if tc.expParseErr != actParseErr {
|
||||
t.Errorf("exp %v, got %v", tc.expParseErr, actParseErr)
|
||||
actParseErr := cmd.Execute(tc.main, tc.flags) != nil
|
||||
if tc.expErr != actParseErr {
|
||||
t.Errorf("exp %v, got %v", tc.expErr, actParseErr)
|
||||
}
|
||||
if tc.expParseErr {
|
||||
return
|
||||
}
|
||||
|
||||
actDoErr := cmd.Do() != nil
|
||||
if tc.expDoErr != actDoErr {
|
||||
t.Errorf("exp %v, got %v", tc.expDoErr, actDoErr)
|
||||
}
|
||||
if tc.expDoErr {
|
||||
if tc.expErr {
|
||||
return
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue