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