Skip to content

Commit

Permalink
feat: enable multi records in database file
Browse files Browse the repository at this point in the history
  • Loading branch information
Diegiwg committed Jun 26, 2024
1 parent 38e0069 commit 5995304
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 21 deletions.
7 changes: 5 additions & 2 deletions cmd/pause.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
7 changes: 5 additions & 2 deletions cmd/resume.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
8 changes: 5 additions & 3 deletions cmd/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,24 @@ 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")
}

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
}

Expand Down
5 changes: 4 additions & 1 deletion cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
8 changes: 4 additions & 4 deletions cmd/stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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
}
10 changes: 5 additions & 5 deletions data/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ 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")

_, 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
}
Expand All @@ -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
}
4 changes: 2 additions & 2 deletions data/save.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
18 changes: 16 additions & 2 deletions model/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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
}

Expand Down

0 comments on commit 5995304

Please sign in to comment.