From 97ef735fc8dee3e9dce9a99515da6b0aaddbe428 Mon Sep 17 00:00:00 2001 From: Zakaria Date: Mon, 14 Sep 2026 10:58:24 -0400 Subject: [PATCH] Adding storage to our data --- Tasks.json | 14 ++++++++++++++ commands.go | 22 ++++++++++++++++++++++ main.go | 18 ++++++++++-------- storage.go | 32 ++++++++++++++++++++++++++++++++ todo.go | 30 +++++++++++++++--------------- 5 files changed, 93 insertions(+), 23 deletions(-) create mode 100644 Tasks.json create mode 100644 commands.go create mode 100644 storage.go diff --git a/Tasks.json b/Tasks.json new file mode 100644 index 0000000..56b39a6 --- /dev/null +++ b/Tasks.json @@ -0,0 +1,14 @@ +[ + { + "Title": "yeah yeah, do better", + "Status": true, + "CreatedAt": "2026-09-13T17:04:15.15332711-04:00", + "StatusAt": "2026-09-13T17:06:20.812974703-04:00" + }, + { + "Title": "Do better", + "Status": true, + "CreatedAt": "2026-09-13T17:04:15.153327306-04:00", + "StatusAt": "2026-09-13T17:06:20.812571404-04:00" + } +] \ No newline at end of file diff --git a/commands.go b/commands.go new file mode 100644 index 0000000..ab6c2d5 --- /dev/null +++ b/commands.go @@ -0,0 +1,22 @@ +package main + +import "flag" + +type cmdflags struct { + add string + del int + edit string + toggle int + list bool +} + +func newcmdflags() *cmdflags { + cf := cmdflags{} + + flag.StringVar(&cf.add, "add", "", "add a new title") + flag.StringVar(&cf.add, "edit", "", "add a new title") + flag.StringVar(&cf.add, "del", "", "add a new title") + flag.StringVar(&cf.add, "toggle", "", "add a new title") + flag.StringVar(&cf.add, "list", "", "add a new title") + return nil +} diff --git a/main.go b/main.go index 0c8a96f..6f67450 100644 --- a/main.go +++ b/main.go @@ -2,13 +2,15 @@ package main func main() { todos := Todos{} - - todos.add("Learn more Go") - todos.add("You have to understand the code you are writing") - todos.add("You have to understand the code you are writing") - - todos.toggle(0) - - todos.delete(2) + storage := newStorage[Todos]("Tasks.json") + storage.load(&todos) todos.print() + todos.toggle(1) + todos.print() + todos.delete(2) + todos.edit(0, "yeah yeah, do better") + todos.toggle(0) + todos.toggle(2) + todos.print() + storage.save(todos) } diff --git a/storage.go b/storage.go new file mode 100644 index 0000000..87e14de --- /dev/null +++ b/storage.go @@ -0,0 +1,32 @@ +package main + +import ( + "encoding/json" + "os" +) + +type storage[T any] struct { + FileName string +} + +func newStorage[T any](filename string) *storage[T] { + return &storage[T]{FileName: filename} +} + +func (s *storage[T]) save(data T) error { + fileData, err := json.MarshalIndent(data, "", " ") + if err != nil { + return err + } + + return os.WriteFile(s.FileName, fileData, 0o644) +} + +func (s *storage[T]) load(data *T) error { + fileData, err := os.ReadFile(s.FileName) + if err != nil { + return err + } + + return json.Unmarshal(fileData, data) +} diff --git a/todo.go b/todo.go index df9f617..c0a620d 100644 --- a/todo.go +++ b/todo.go @@ -10,20 +10,20 @@ import ( ) type Todo struct { - Title string - Completed bool - CreatedAt time.Time - CompletedAt *time.Time + Title string + Status bool + CreatedAt time.Time + StatusAt *time.Time } type Todos []Todo func (todos *Todos) add(title string) { todo := Todo{ - Title: title, - Completed: false, - CompletedAt: nil, - CreatedAt: time.Now(), + Title: title, + Status: false, + StatusAt: nil, + CreatedAt: time.Now(), } *todos = append(*todos, todo) @@ -55,12 +55,12 @@ func (todos *Todos) toggle(index int) error { return err } - isCompeleted := t[index].Completed + isCompeleted := t[index].Status if !isCompeleted { completionTime := time.Now() - t[index].CompletedAt = &completionTime + t[index].StatusAt = &completionTime } - t[index].Completed = !isCompeleted + t[index].Status = !isCompeleted return nil } @@ -77,15 +77,15 @@ func (todos *Todos) edit(index int, newTitle string) error { func (todos *Todos) print() { table := table.New(os.Stdout) table.SetRowLines(false) - table.SetHeaders("Index", "Titile", "Completed", "Created at", "Completed at") + table.SetHeaders("Index", "Title", "Status", "Created at", "Status at") for index, t := range *todos { completed := "❌" completedAt := "" - if t.Completed { + if t.Status { completed = "✅" - if t.CompletedAt != nil { - completedAt = t.CompletedAt.Format(time.RFC1123) + if t.StatusAt != nil { + completedAt = t.StatusAt.Format(time.RFC1123) } } table.AddRow(strconv.Itoa(index), t.Title, completed, t.CreatedAt.Format(time.RFC1123), completedAt)