Adding storage to our data

This commit is contained in:
2026-09-14 10:58:24 -04:00
parent b85c4c805a
commit 97ef735fc8
5 changed files with 93 additions and 23 deletions
+14
View File
@@ -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"
}
]
+22
View File
@@ -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
}
+10 -8
View File
@@ -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)
}
+32
View File
@@ -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)
}
+15 -15
View File
@@ -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)