diff options
| author | Grant Ammons <gammons@gmail.com> | 2016-04-26 09:01:40 -0400 |
|---|---|---|
| committer | Grant Ammons <gammons@gmail.com> | 2016-04-26 09:01:40 -0400 |
| commit | e3213b3ef2c84387b66eeb731f5493c5e6da6a5d (patch) | |
| tree | 46b5ee148e7fd578d992f08ab89ba1d24a322d29 | |
| parent | a0f7203996439210b2a40663eeeeb8563ab03d68 (diff) | |
Get adding a todo working
| -rw-r--r-- | todo.go | 2 | ||||
| -rw-r--r-- | todolist/app.go | 16 | ||||
| -rw-r--r-- | todolist/file_store.go | 22 | ||||
| -rw-r--r-- | todolist/file_store_test.go | 13 | ||||
| -rw-r--r-- | todolist/store.go | 7 | ||||
| -rw-r--r-- | todolist/todo_item.go | 16 | ||||
| -rw-r--r-- | todolist/todos.json | 2 |
7 files changed, 64 insertions, 14 deletions
@@ -26,5 +26,7 @@ func routeInput(command string, input string) { switch { case command == "l" || command == "list": app.ListTodos(input) + case command == "a" || command == "add": + app.AddTodo(input) } } diff --git a/todolist/app.go b/todolist/app.go index 6122b6a..00effa7 100644 --- a/todolist/app.go +++ b/todolist/app.go @@ -1,6 +1,9 @@ package todolist -import "regexp" +import ( + "fmt" + "regexp" +) type App struct { TodoStore Store @@ -12,8 +15,17 @@ func NewApp() *App { return app } -func (a *App) ListTodos(input string) { +func (a *App) AddTodo(input string) { + parser := &Parser{} + todo := parser.Parse(input) + + a.TodoStore.Add(todo) + a.TodoStore.Save() + fmt.Println("Todo added.") +} +func (a *App) ListTodos(input string) { + //filtered := NewFilter(a.TodoStore.Todos()).filter() grouped := a.getGroups(input) formatter := NewFormatter(grouped) diff --git a/todolist/file_store.go b/todolist/file_store.go index ac3dff5..3b5fbfe 100644 --- a/todolist/file_store.go +++ b/todolist/file_store.go @@ -18,6 +18,11 @@ func NewFileStore() *FileStore { return &FileStore{FileLocation: usr.HomeDir + "/.todos.json"} } +func (f *FileStore) Add(todo *Todo) { + todo.Id = f.NextId() + f.Data = append(f.Data, *todo) +} + func (f *FileStore) Load() { data, err := ioutil.ReadFile(f.FileLocation) if err != nil { @@ -32,6 +37,23 @@ func (f *FileStore) Load() { } } +func (f *FileStore) Save() { + data, _ := json.Marshal(f.Data) + if err := ioutil.WriteFile(f.FileLocation, []byte(data), 0644); err != nil { + fmt.Println("Error writing json file", err) + } +} + func (f *FileStore) Todos() []Todo { return f.Data } + +func (f *FileStore) NextId() int { + maxId := 0 + for _, todo := range f.Data { + if todo.Id > maxId { + maxId = todo.Id + } + } + return maxId + 1 +} diff --git a/todolist/file_store_test.go b/todolist/file_store_test.go index 8bb23e0..3e10491 100644 --- a/todolist/file_store_test.go +++ b/todolist/file_store_test.go @@ -12,3 +12,16 @@ func TestFileStore(t *testing.T) { store.Load() assert.Equal(store.Data[0].Subject, "this is the first subject", "") } + +func TestSave(t *testing.T) { + store := &FileStore{FileLocation: "todos.json"} + store.Load() + store.Save() +} + +func TestNextId(t *testing.T) { + assert := assert.New(t) + store := &FileStore{FileLocation: "todos.json"} + store.Load() + assert.Equal(3, store.NextId()) +} diff --git a/todolist/store.go b/todolist/store.go index 8b892bf..d3f42d2 100644 --- a/todolist/store.go +++ b/todolist/store.go @@ -3,10 +3,11 @@ package todolist type Store interface { Load() Todos() []Todo - //Save() + + Add(t *Todo) + Save() //Find(id int) Todo - //Add(t *Todo) //Remove(t *Todo) - //NextId() int + NextId() int } diff --git a/todolist/todo_item.go b/todolist/todo_item.go index 1319132..0b6eeac 100644 --- a/todolist/todo_item.go +++ b/todolist/todo_item.go @@ -3,14 +3,14 @@ package todolist import "time" type Todo struct { - Id int - Subject string - Projects []string - Contexts []string - Due string - FormattedDue time.Time - Completed bool - Archived bool + Id int `json:"id"` + Subject string `json:"subject"` + Projects []string `json:"projects"` + Contexts []string `json:"contexts"` + Due string `json:"due"` + FormattedDue time.Time `json:"-"` + Completed bool `json:"completed"` + Archived bool `json:"archived"` } func NewTodo() *Todo { diff --git a/todolist/todos.json b/todolist/todos.json index 8dc268d..d3da483 100644 --- a/todolist/todos.json +++ b/todolist/todos.json @@ -1 +1 @@ -[{"subject":"this is the first subject","projects":["test1"],"contexts":["root"],"due":"2016-04-04","completed":true,"id":1,"archived":true},{"subject":" audit userify for 2FA","projects":["test1"],"contexts":["root","more"],"due":null,"completed":null,"id":2,"archived":false}] +[{"id":1,"subject":"this is the first subject","projects":["test1"],"contexts":["root"],"due":"2016-04-04","completed":true,"archived":true},{"id":2,"subject":" audit userify for 2FA","projects":["test1"],"contexts":["root","more"],"due":"","completed":false,"archived":false}]
\ No newline at end of file |
