From af8b1814aa3f5a63e913923449b29cac748fba57 Mon Sep 17 00:00:00 2001 From: Grant Ammons Date: Sun, 24 Apr 2016 09:55:07 -0400 Subject: WIP on getting stores implemented --- file_store.go | 29 +++++++++++++++++++++++++++++ file_store_test.go | 9 +++++++++ store.go | 11 +++++++++++ 3 files changed, 49 insertions(+) create mode 100644 file_store.go create mode 100644 file_store_test.go create mode 100644 store.go diff --git a/file_store.go b/file_store.go new file mode 100644 index 0000000..03704f9 --- /dev/null +++ b/file_store.go @@ -0,0 +1,29 @@ +package todolist + +import ( + "encoding/json" + "fmt" + "io/ioutil" +) + +type FileStore struct { + FileLocation string + Data []Todo +} + +func NewFileStore() *FileStore { + return &FileStore{FileLocation: "todos.json"} +} + +func (f *FileStore) Load() { + data, err := ioutil.ReadFile(f.FileLocation) + if err != nil { + fmt.Println("Error reading file", err) + } + + jerr := json.Unmarshal(data, &f.Data) + if jerr != nil { + fmt.Println("Error reading json data", jerr) + } + +} diff --git a/file_store_test.go b/file_store_test.go new file mode 100644 index 0000000..b7132a8 --- /dev/null +++ b/file_store_test.go @@ -0,0 +1,9 @@ +package todolist + +import "testing" + +func TestFileStore(t *testing.T) { + store := &FileStore{FileLocation: "todos.json"} + store.Load() + store.Data[0] +} diff --git a/store.go b/store.go new file mode 100644 index 0000000..006583a --- /dev/null +++ b/store.go @@ -0,0 +1,11 @@ +package todolist + +type Store interface { + Load() + Save() + + Find(id int) Todo + Add(t *Todo) + Remove(t *Todo) + NextId() int +} -- cgit v1.3