Adding storage to our data
This commit is contained in:
+14
@@ -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
@@ -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
|
||||||
|
}
|
||||||
@@ -2,13 +2,15 @@ package main
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
todos := Todos{}
|
todos := Todos{}
|
||||||
|
storage := newStorage[Todos]("Tasks.json")
|
||||||
todos.add("Learn more Go")
|
storage.load(&todos)
|
||||||
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)
|
|
||||||
todos.print()
|
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
@@ -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)
|
||||||
|
}
|
||||||
@@ -10,20 +10,20 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Todo struct {
|
type Todo struct {
|
||||||
Title string
|
Title string
|
||||||
Completed bool
|
Status bool
|
||||||
CreatedAt time.Time
|
CreatedAt time.Time
|
||||||
CompletedAt *time.Time
|
StatusAt *time.Time
|
||||||
}
|
}
|
||||||
|
|
||||||
type Todos []Todo
|
type Todos []Todo
|
||||||
|
|
||||||
func (todos *Todos) add(title string) {
|
func (todos *Todos) add(title string) {
|
||||||
todo := Todo{
|
todo := Todo{
|
||||||
Title: title,
|
Title: title,
|
||||||
Completed: false,
|
Status: false,
|
||||||
CompletedAt: nil,
|
StatusAt: nil,
|
||||||
CreatedAt: time.Now(),
|
CreatedAt: time.Now(),
|
||||||
}
|
}
|
||||||
|
|
||||||
*todos = append(*todos, todo)
|
*todos = append(*todos, todo)
|
||||||
@@ -55,12 +55,12 @@ func (todos *Todos) toggle(index int) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
isCompeleted := t[index].Completed
|
isCompeleted := t[index].Status
|
||||||
if !isCompeleted {
|
if !isCompeleted {
|
||||||
completionTime := time.Now()
|
completionTime := time.Now()
|
||||||
t[index].CompletedAt = &completionTime
|
t[index].StatusAt = &completionTime
|
||||||
}
|
}
|
||||||
t[index].Completed = !isCompeleted
|
t[index].Status = !isCompeleted
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -77,15 +77,15 @@ func (todos *Todos) edit(index int, newTitle string) error {
|
|||||||
func (todos *Todos) print() {
|
func (todos *Todos) print() {
|
||||||
table := table.New(os.Stdout)
|
table := table.New(os.Stdout)
|
||||||
table.SetRowLines(false)
|
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 {
|
for index, t := range *todos {
|
||||||
completed := "❌"
|
completed := "❌"
|
||||||
completedAt := ""
|
completedAt := ""
|
||||||
|
|
||||||
if t.Completed {
|
if t.Status {
|
||||||
completed = "✅"
|
completed = "✅"
|
||||||
if t.CompletedAt != nil {
|
if t.StatusAt != nil {
|
||||||
completedAt = t.CompletedAt.Format(time.RFC1123)
|
completedAt = t.StatusAt.Format(time.RFC1123)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
table.AddRow(strconv.Itoa(index), t.Title, completed, t.CreatedAt.Format(time.RFC1123), completedAt)
|
table.AddRow(strconv.Itoa(index), t.Title, completed, t.CreatedAt.Format(time.RFC1123), completedAt)
|
||||||
|
|||||||
Reference in New Issue
Block a user