From 5995304aceb037a954ed5b954ded44eae13afc53 Mon Sep 17 00:00:00 2001 From: Diegiwg Date: Tue, 25 Jun 2024 23:35:55 -0300 Subject: [PATCH] feat: enable multi records in database file --- cmd/pause.go | 7 +++++-- cmd/resume.go | 7 +++++-- cmd/show.go | 8 +++++--- cmd/start.go | 5 ++++- cmd/stop.go | 8 ++++---- data/load.go | 10 +++++----- data/save.go | 4 ++-- model/record.go | 18 ++++++++++++++++-- 8 files changed, 46 insertions(+), 21 deletions(-) diff --git a/cmd/pause.go b/cmd/pause.go index 2becbf3..82c06e7 100644 --- a/cmd/pause.go +++ b/cmd/pause.go @@ -6,9 +6,12 @@ import ( ) func Pause(ctx *cli.Context) error { - r := data.ReadOrCreateRecord(ctx) + table := data.ReadOrCreateRecord(ctx) + + r := table.GetLast() r.Stop() - data.SaveRecordToFile(ctx, &r) + + data.SaveRecordToFile(ctx, &table) return nil } diff --git a/cmd/resume.go b/cmd/resume.go index b75fe96..d18d6ae 100644 --- a/cmd/resume.go +++ b/cmd/resume.go @@ -6,9 +6,12 @@ import ( ) func Resume(ctx *cli.Context) error { - r := data.ReadOrCreateRecord(ctx) + table := data.ReadOrCreateRecord(ctx) + + r := table.GetLast() r.Start() - data.SaveRecordToFile(ctx, &r) + + data.SaveRecordToFile(ctx, &table) return nil } diff --git a/cmd/show.go b/cmd/show.go index a7b701e..8616701 100644 --- a/cmd/show.go +++ b/cmd/show.go @@ -10,7 +10,9 @@ import ( ) func Show(ctx *cli.Context) error { - r := data.ReadOrCreateRecord(ctx) + table := data.ReadOrCreateRecord(ctx) + + r := table.GetLast() if len(r.Items) == 0 { return errors.New("no items found in record") @@ -18,14 +20,14 @@ func Show(ctx *cli.Context) error { var acc time.Duration - item_count := len(r.Items) + count := len(r.Items) for index, item := range r.Items { start := item.Start end := item.End if end == (time.Time{}) { - if index != item_count-1 { + if index != count-1 { continue } diff --git a/cmd/start.go b/cmd/start.go index 7864e04..98f0c7c 100644 --- a/cmd/start.go +++ b/cmd/start.go @@ -10,7 +10,10 @@ func Start(ctx *cli.Context) error { r := model.NewRecord() r.Start() - data.SaveRecordToFile(ctx, &r) + table := data.ReadOrCreateRecord(ctx) + table.Add(r) + + data.SaveRecordToFile(ctx, &table) return nil } diff --git a/cmd/stop.go b/cmd/stop.go index ebc6161..927f188 100644 --- a/cmd/stop.go +++ b/cmd/stop.go @@ -5,11 +5,12 @@ import ( "github.com/Diegiwg/cli" "github.com/Diegiwg/tt/data" - "github.com/Diegiwg/tt/model" ) func Stop(ctx *cli.Context) error { - r := data.ReadOrCreateRecord(ctx) + table := data.ReadOrCreateRecord(ctx) + + r := table.GetLast() if r.CurrentItem != -1 && r.Stop() != nil { return errors.New("no current item to stop") @@ -18,8 +19,7 @@ func Stop(ctx *cli.Context) error { time := r.TotalTime() println("Total time:", time) - r = model.NewRecord() - data.SaveRecordToFile(ctx, &r) + data.SaveRecordToFile(ctx, &table) return nil } diff --git a/data/load.go b/data/load.go index 033191a..09badff 100644 --- a/data/load.go +++ b/data/load.go @@ -9,7 +9,7 @@ import ( "github.com/Diegiwg/tt/model" ) -func ReadOrCreateRecord(ctx *cli.Context) model.Record { +func ReadOrCreateRecord(ctx *cli.Context) model.RecordTable { USER_HOME, _ := os.UserHomeDir() dbPath := filepath.Join(USER_HOME, "tt.db") @@ -17,7 +17,7 @@ func ReadOrCreateRecord(ctx *cli.Context) model.Record { _, err := os.Stat(dbPath) if err != nil { println("record not found, creating new one") - r := model.NewRecord() + r := model.NewRecordTable() SaveRecordToFile(ctx, &r) return r } @@ -27,12 +27,12 @@ func ReadOrCreateRecord(ctx *cli.Context) model.Record { panic(err) } - var record model.Record + var table model.RecordTable - err = json.Unmarshal(fileContent, &record) + err = json.Unmarshal(fileContent, &table) if err != nil { panic(err) } - return record + return table } diff --git a/data/save.go b/data/save.go index c9ac71f..54d5664 100644 --- a/data/save.go +++ b/data/save.go @@ -9,12 +9,12 @@ import ( "github.com/Diegiwg/tt/model" ) -func SaveRecordToFile(ctx *cli.Context, record *model.Record) { +func SaveRecordToFile(ctx *cli.Context, table *model.RecordTable) { USER_HOME, _ := os.UserHomeDir() dbPath := filepath.Join(USER_HOME, "tt.db") - data, err := json.Marshal(record) + data, err := json.Marshal(table) if err != nil { panic(err) } diff --git a/model/record.go b/model/record.go index 653cc72..e9168b2 100644 --- a/model/record.go +++ b/model/record.go @@ -33,6 +33,20 @@ func NewRecord() Record { } } +func NewRecordTable() RecordTable { + return RecordTable{ + Records: []Record{}, + } +} + +func (table *RecordTable) Add(record Record) { + table.Records = append(table.Records, record) +} + +func (table *RecordTable) GetLast() Record { + return table.Records[len(table.Records)-1] +} + func (record *Record) Start() error { if record.CurrentItem != -1 { return errors.New("already have a current item") @@ -59,11 +73,11 @@ func (record *Record) Stop() error { func (record *Record) TotalTime() string { var total time.Duration - item_count := len(record.Items) + count := len(record.Items) for index, item := range record.Items { if item.End == (time.Time{}) { - if index != item_count-1 { + if index != count-1 { continue }