From 671deb355a328f0cca3a7de1ee1bc4fe77110f78 Mon Sep 17 00:00:00 2001 From: Erik Winter Date: Wed, 18 Sep 2024 07:55:14 +0200 Subject: [PATCH] single module --- Makefile | 4 +-- cal/go.mod | 5 ---- cal/main.go | 12 ++++---- cal/planner.go | 2 +- go.mod | 20 +++++++++++++ go.sum | 29 ++++++++++++++++++ sync-client/go.mod | 5 ---- sync-client/go.sum | 2 -- sync-service/go.mod | 3 -- sync-service/storage.go | 15 ---------- {sync-client => sync/client}/client.go | 8 +++-- sync-client/planner.go => sync/item/item.go | 2 +- {sync-service => sync/service}/handler.go | 30 ++++++++++--------- .../service}/handler_test.go | 30 ++++++++++--------- {sync-service => sync/service}/main.go | 6 ++-- {sync-service => sync/service}/memory.go | 12 ++++---- {sync-service => sync/service}/memory_test.go | 18 ++++++----- {sync-service => sync/service}/sqlite.go | 9 +++--- sync/service/storage.go | 17 +++++++++++ 19 files changed, 136 insertions(+), 93 deletions(-) delete mode 100644 cal/go.mod create mode 100644 go.mod create mode 100644 go.sum delete mode 100644 sync-client/go.mod delete mode 100644 sync-client/go.sum delete mode 100644 sync-service/go.mod delete mode 100644 sync-service/storage.go rename {sync-client => sync/client}/client.go (90%) rename sync-client/planner.go => sync/item/item.go (97%) rename {sync-service => sync/service}/handler.go (87%) rename {sync-service => sync/service}/handler_test.go (86%) rename {sync-service => sync/service}/main.go (88%) rename {sync-service => sync/service}/memory.go (61%) rename {sync-service => sync/service}/memory_test.go (79%) rename {sync-service => sync/service}/sqlite.go (94%) create mode 100644 sync/service/storage.go diff --git a/Makefile b/Makefile index 4777859..5eefc71 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,4 @@ sync-run: - cd sync && PLANNER_DB_PATH=test.db PLANNER_PORT=8092 PLANNER_API_KEY=testKey go run . + cd sync/service && PLANNER_DB_PATH=test.db PLANNER_PORT=8092 PLANNER_API_KEY=testKey go run . -sync-build-and-push: - cd sync && docker build . -t codeberg.org/ewintr/syncservice && docker push codeberg.org/ewintr/syncservice diff --git a/cal/go.mod b/cal/go.mod deleted file mode 100644 index f72abd1..0000000 --- a/cal/go.mod +++ /dev/null @@ -1,5 +0,0 @@ -module go-mod.ewintr.nl/planner/cal - -go 1.21.5 - -require github.com/google/uuid v1.6.0 diff --git a/cal/main.go b/cal/main.go index c43acd7..8b59277 100644 --- a/cal/main.go +++ b/cal/main.go @@ -6,14 +6,14 @@ import ( "time" "go-mod.ewintr.nl/planner/sync/client" - "go-mod.ewintr.nl/planner/sync/planner" + "go-mod.ewintr.nl/planner/sync/item" ) func main() { fmt.Println("cal") c := client.NewClient("http://localhost:8092", "testKey") - items, err := c.Updated([]planner.Kind{planner.KindEvent}, time.Time{}) + items, err := c.Updated([]item.Kind{item.KindEvent}, time.Time{}) if err != nil { fmt.Println(err) os.Exit(1) @@ -21,18 +21,18 @@ func main() { fmt.Printf("%+v\n", items) - i := planner.Item{ + i := item.Item{ ID: "id-1", - Kind: planner.KindEvent, + Kind: item.KindEvent, Updated: time.Now(), Body: "body", } - if err := c.Update([]planner.Item{i}); err != nil { + if err := c.Update([]item.Item{i}); err != nil { fmt.Println(err) os.Exit(1) } - items, err = c.Updated([]planner.Kind{planner.KindEvent}, time.Time{}) + items, err = c.Updated([]item.Kind{item.KindEvent}, time.Time{}) if err != nil { fmt.Println(err) os.Exit(1) diff --git a/cal/planner.go b/cal/planner.go index e3a05ab..903562a 100644 --- a/cal/planner.go +++ b/cal/planner.go @@ -1,4 +1,4 @@ -package service +package main import ( "time" diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..a5d95fa --- /dev/null +++ b/go.mod @@ -0,0 +1,20 @@ +module go-mod.ewintr.nl/planner + +go 1.21.5 + +require ( + github.com/dustin/go-humanize v1.0.1 // indirect + github.com/google/uuid v1.6.0 // indirect + github.com/hashicorp/golang-lru/v2 v2.0.7 // indirect + github.com/mattn/go-isatty v0.0.20 // indirect + github.com/ncruces/go-strftime v0.1.9 // indirect + github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect + golang.org/x/sys v0.22.0 // indirect + modernc.org/gc/v3 v3.0.0-20240107210532-573471604cb6 // indirect + modernc.org/libc v1.55.3 // indirect + modernc.org/mathutil v1.6.0 // indirect + modernc.org/memory v1.8.0 // indirect + modernc.org/sqlite v1.33.1 // indirect + modernc.org/strutil v1.2.0 // indirect + modernc.org/token v1.1.0 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..db23520 --- /dev/null +++ b/go.sum @@ -0,0 +1,29 @@ +github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY= +github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto= +github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= +github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFs4luLUK2k= +github.com/hashicorp/golang-lru/v2 v2.0.7/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM= +github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= +github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +github.com/ncruces/go-strftime v0.1.9 h1:bY0MQC28UADQmHmaF5dgpLmImcShSi2kHU9XLdhx/f4= +github.com/ncruces/go-strftime v0.1.9/go.mod h1:Fwc5htZGVVkseilnfgOVb9mKy6w1naJmn9CehxcKcls= +github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE= +github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI= +golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +modernc.org/gc/v3 v3.0.0-20240107210532-573471604cb6 h1:5D53IMaUuA5InSeMu9eJtlQXS2NxAhyWQvkKEgXZhHI= +modernc.org/gc/v3 v3.0.0-20240107210532-573471604cb6/go.mod h1:Qz0X07sNOR1jWYCrJMEnbW/X55x206Q7Vt4mz6/wHp4= +modernc.org/libc v1.55.3 h1:AzcW1mhlPNrRtjS5sS+eW2ISCgSOLLNyFzRh/V3Qj/U= +modernc.org/libc v1.55.3/go.mod h1:qFXepLhz+JjFThQ4kzwzOjA/y/artDeg+pcYnY+Q83w= +modernc.org/mathutil v1.6.0 h1:fRe9+AmYlaej+64JsEEhoWuAYBkOtQiMEU7n/XgfYi4= +modernc.org/mathutil v1.6.0/go.mod h1:Ui5Q9q1TR2gFm0AQRqQUaBWFLAhQpCwNcuhBOSedWPo= +modernc.org/memory v1.8.0 h1:IqGTL6eFMaDZZhEWwcREgeMXYwmW83LYW8cROZYkg+E= +modernc.org/memory v1.8.0/go.mod h1:XPZ936zp5OMKGWPqbD3JShgd/ZoQ7899TUuQqxY+peU= +modernc.org/sqlite v1.33.1 h1:trb6Z3YYoeM9eDL1O8do81kP+0ejv+YzgyFo+Gwy0nM= +modernc.org/sqlite v1.33.1/go.mod h1:pXV2xHxhzXZsgT/RtTFAPY6JJDEvOTcTdwADQCCWD4k= +modernc.org/strutil v1.2.0 h1:agBi9dp1I+eOnxXeiZawM8F4LawKv4NzGWSaLfyeNZA= +modernc.org/strutil v1.2.0/go.mod h1:/mdcBmfOibveCTBxUl5B5l6W+TTH1FXPLHZE6bTosX0= +modernc.org/token v1.1.0 h1:Xl7Ap9dKaEs5kLoOQeQmPWevfnk/DM5qcLcYlA8ys6Y= +modernc.org/token v1.1.0/go.mod h1:UGzOrNV1mAFSEB63lOFHIpNRUVMvYTc6yu1SMY/XTDM= diff --git a/sync-client/go.mod b/sync-client/go.mod deleted file mode 100644 index 85fe3ef..0000000 --- a/sync-client/go.mod +++ /dev/null @@ -1,5 +0,0 @@ -module go-mod.ewintr.nl/planner/sync-client - -go 1.21.5 - -require github.com/google/uuid v1.6.0 diff --git a/sync-client/go.sum b/sync-client/go.sum deleted file mode 100644 index 7790d7c..0000000 --- a/sync-client/go.sum +++ /dev/null @@ -1,2 +0,0 @@ -github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= -github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= diff --git a/sync-service/go.mod b/sync-service/go.mod deleted file mode 100644 index 9c499bb..0000000 --- a/sync-service/go.mod +++ /dev/null @@ -1,3 +0,0 @@ -module go-mod.ewintr.nl/planner/sync-service - -go 1.21.5 diff --git a/sync-service/storage.go b/sync-service/storage.go deleted file mode 100644 index 62152ad..0000000 --- a/sync-service/storage.go +++ /dev/null @@ -1,15 +0,0 @@ -package main - -import ( - "errors" - "time" -) - -var ( - ErrNotFound = errors.New("not found") -) - -type Syncer interface { - Update(item Item) error - Updated(kind []Kind, t time.Time) ([]Item, error) -} diff --git a/sync-client/client.go b/sync/client/client.go similarity index 90% rename from sync-client/client.go rename to sync/client/client.go index d2a2c51..b060fdc 100644 --- a/sync-client/client.go +++ b/sync/client/client.go @@ -9,6 +9,8 @@ import ( "net/url" "strings" "time" + + "go-mod.ewintr.nl/planner/sync/item" ) type Client struct { @@ -27,7 +29,7 @@ func NewClient(url, apiKey string) *Client { } } -func (c *Client) Update(items []Item) error { +func (c *Client) Update(items []item.Item) error { body, err := json.Marshal(items) if err != nil { return fmt.Errorf("could not marhal body: %v", err) @@ -49,7 +51,7 @@ func (c *Client) Update(items []Item) error { return nil } -func (c *Client) Updated(ks []Kind, ts time.Time) ([]Item, error) { +func (c *Client) Updated(ks []item.Kind, ts time.Time) ([]item.Item, error) { ksStr := make([]string, 0, len(ks)) for _, k := range ks { ksStr = append(ksStr, string(k)) @@ -78,7 +80,7 @@ func (c *Client) Updated(ks []Kind, ts time.Time) ([]Item, error) { return nil, fmt.Errorf("could not read response body: %v", err) } - var items []Item + var items []item.Item if err := json.Unmarshal(body, &items); err != nil { return nil, fmt.Errorf("could not unmarshal response body: %v", err) } diff --git a/sync-client/planner.go b/sync/item/item.go similarity index 97% rename from sync-client/planner.go rename to sync/item/item.go index dad9153..8d5d5ca 100644 --- a/sync-client/planner.go +++ b/sync/item/item.go @@ -1,4 +1,4 @@ -package client +package item import ( "time" diff --git a/sync-service/handler.go b/sync/service/handler.go similarity index 87% rename from sync-service/handler.go rename to sync/service/handler.go index b7a862c..f06ca88 100644 --- a/sync-service/handler.go +++ b/sync/service/handler.go @@ -10,6 +10,8 @@ import ( "slices" "strings" "time" + + "go-mod.ewintr.nl/planner/sync/item" ) type Server struct { @@ -67,17 +69,17 @@ func (s *Server) SyncGet(w http.ResponseWriter, r *http.Request) { return } } - ks := make([]Kind, 0) + ks := make([]item.Kind, 0) ksStr := r.URL.Query().Get("ks") if ksStr != "" { for _, k := range strings.Split(ksStr, ",") { - if !slices.Contains(KnownKinds, Kind(k)) { + if !slices.Contains(item.KnownKinds, item.Kind(k)) { msg := fmt.Sprintf("unknown kind: %s", k) http.Error(w, fmtError(msg), http.StatusBadRequest) s.logger.Info(msg) return } - ks = append(ks, Kind(k)) + ks = append(ks, item.Kind(k)) } } @@ -111,7 +113,7 @@ func (s *Server) SyncPost(w http.ResponseWriter, r *http.Request) { } defer r.Body.Close() - var items []Item + var items []item.Item if err := json.Unmarshal(body, &items); err != nil { msg := err.Error() http.Error(w, fmtError(msg), http.StatusBadRequest) @@ -119,33 +121,33 @@ func (s *Server) SyncPost(w http.ResponseWriter, r *http.Request) { return } - for _, item := range items { - if item.ID == "" { + for _, it := range items { + if it.ID == "" { msg := "item without an id" http.Error(w, fmtError(msg), http.StatusBadRequest) s.logger.Info(msg) return } - if item.Kind == "" { - msg := fmt.Sprintf("item %s does not have a kind", item.ID) + if it.Kind == "" { + msg := fmt.Sprintf("item %s does not have a kind", it.ID) http.Error(w, fmtError(msg), http.StatusBadRequest) s.logger.Info(msg) return } - if !slices.Contains(KnownKinds, item.Kind) { - msg := fmt.Sprintf("items %s does not have a know kind", item.ID) + if !slices.Contains(item.KnownKinds, it.Kind) { + msg := fmt.Sprintf("items %s does not have a know kind", it.ID) http.Error(w, fmtError(msg), http.StatusBadRequest) s.logger.Info(msg) return } - if item.Body == "" { - msg := fmt.Sprintf(`{"error":"item %s does not have a body"}`, item.ID) + if it.Body == "" { + msg := fmt.Sprintf(`{"error":"item %s does not have a body"}`, it.ID) http.Error(w, msg, http.StatusBadRequest) s.logger.Info(msg) return } - item.Updated = time.Now() - if err := s.syncer.Update(item); err != nil { + it.Updated = time.Now() + if err := s.syncer.Update(it); err != nil { msg := err.Error() http.Error(w, fmtError(msg), http.StatusInternalServerError) s.logger.Error(msg) diff --git a/sync-service/handler_test.go b/sync/service/handler_test.go similarity index 86% rename from sync-service/handler_test.go rename to sync/service/handler_test.go index 6cb17a4..c64bac8 100644 --- a/sync-service/handler_test.go +++ b/sync/service/handler_test.go @@ -14,6 +14,8 @@ import ( "strings" "testing" "time" + + "go-mod.ewintr.nl/planner/sync/item" ) func TestServerServeHTTP(t *testing.T) { @@ -56,10 +58,10 @@ func TestSyncGet(t *testing.T) { now := time.Now() mem := NewMemory() - items := []Item{ - {ID: "id-0", Kind: KindEvent, Updated: now.Add(-10 * time.Minute)}, - {ID: "id-1", Kind: KindEvent, Updated: now.Add(-5 * time.Minute)}, - {ID: "id-2", Kind: KindTask, Updated: now.Add(time.Minute)}, + items := []item.Item{ + {ID: "id-0", Kind: item.KindEvent, Updated: now.Add(-10 * time.Minute)}, + {ID: "id-1", Kind: item.KindEvent, Updated: now.Add(-5 * time.Minute)}, + {ID: "id-2", Kind: item.KindTask, Updated: now.Add(time.Minute)}, } for _, item := range items { @@ -76,7 +78,7 @@ func TestSyncGet(t *testing.T) { ts time.Time ks []string expStatus int - expItems []Item + expItems []item.Item }{ { name: "full", @@ -87,13 +89,13 @@ func TestSyncGet(t *testing.T) { name: "new", ts: now.Add(-6 * time.Minute), expStatus: http.StatusOK, - expItems: []Item{items[1], items[2]}, + expItems: []item.Item{items[1], items[2]}, }, { name: "kind", - ks: []string{string(KindTask)}, + ks: []string{string(item.KindTask)}, expStatus: http.StatusOK, - expItems: []Item{items[2]}, + expItems: []item.Item{items[2]}, }, { name: "unknown kind", @@ -121,7 +123,7 @@ func TestSyncGet(t *testing.T) { return } - var actItems []Item + var actItems []item.Item actBody, err := io.ReadAll(res.Result().Body) if err != nil { t.Errorf("exp nil, got %v", err) @@ -155,7 +157,7 @@ func TestSyncPost(t *testing.T) { name string reqBody []byte expStatus int - expItems []Item + expItems []item.Item }{ { name: "empty", @@ -180,9 +182,9 @@ func TestSyncPost(t *testing.T) { {"id":"id-2","kind":"event","updated":"2024-09-06T08:12:00Z","deleted":false,"body":"item2"} ]`), expStatus: http.StatusNoContent, - expItems: []Item{ - {ID: "id-1", Kind: KindEvent, Updated: time.Date(2024, 9, 6, 8, 0, 0, 0, time.UTC)}, - {ID: "id-2", Kind: KindEvent, Updated: time.Date(2024, 9, 6, 12, 0, 0, 0, time.UTC)}, + expItems: []item.Item{ + {ID: "id-1", Kind: item.KindEvent, Updated: time.Date(2024, 9, 6, 8, 0, 0, 0, time.UTC)}, + {ID: "id-2", Kind: item.KindEvent, Updated: time.Date(2024, 9, 6, 12, 0, 0, 0, time.UTC)}, }, }, } { @@ -201,7 +203,7 @@ func TestSyncPost(t *testing.T) { t.Errorf("exp %v, got %v", tc.expStatus, res.Result().StatusCode) } - actItems, err := mem.Updated([]Kind{}, time.Time{}) + actItems, err := mem.Updated([]item.Kind{}, time.Time{}) if err != nil { t.Errorf("exp nil, git %v", err) } diff --git a/sync-service/main.go b/sync/service/main.go similarity index 88% rename from sync-service/main.go rename to sync/service/main.go index a304745..963cf5e 100644 --- a/sync-service/main.go +++ b/sync/service/main.go @@ -8,8 +8,6 @@ import ( "os/signal" "strconv" "syscall" - - "go-mod.ewintr.nl/planner/sync/planner" ) func main() { @@ -29,7 +27,7 @@ func main() { os.Exit(1) } - repo, err := planner.NewSqlite(dbPath) + repo, err := NewSqlite(dbPath) if err != nil { fmt.Printf("could not open sqlite db: %s", err.Error()) os.Exit(1) @@ -43,7 +41,7 @@ func main() { }) address := fmt.Sprintf(":%d", port) - srv := planner.NewServer(repo, apiKey, logger) + srv := NewServer(repo, apiKey, logger) go http.ListenAndServe(address, srv) logger.Info("service started") diff --git a/sync-service/memory.go b/sync/service/memory.go similarity index 61% rename from sync-service/memory.go rename to sync/service/memory.go index b6ae65c..743b20b 100644 --- a/sync-service/memory.go +++ b/sync/service/memory.go @@ -3,26 +3,28 @@ package main import ( "slices" "time" + + "go-mod.ewintr.nl/planner/sync/item" ) type Memory struct { - items map[string]Item + items map[string]item.Item } func NewMemory() *Memory { return &Memory{ - items: make(map[string]Item), + items: make(map[string]item.Item), } } -func (m *Memory) Update(item Item) error { +func (m *Memory) Update(item item.Item) error { m.items[item.ID] = item return nil } -func (m *Memory) Updated(kinds []Kind, timestamp time.Time) ([]Item, error) { - result := make([]Item, 0) +func (m *Memory) Updated(kinds []item.Kind, timestamp time.Time) ([]item.Item, error) { + result := make([]item.Item, 0) for _, i := range m.items { timeOK := timestamp.IsZero() || i.Updated.Equal(timestamp) || i.Updated.After(timestamp) diff --git a/sync-service/memory_test.go b/sync/service/memory_test.go similarity index 79% rename from sync-service/memory_test.go rename to sync/service/memory_test.go index 717161d..17f05eb 100644 --- a/sync-service/memory_test.go +++ b/sync/service/memory_test.go @@ -3,6 +3,8 @@ package main import ( "testing" "time" + + "go-mod.ewintr.nl/planner/sync/item" ) func TestMemoryItem(t *testing.T) { @@ -11,7 +13,7 @@ func TestMemoryItem(t *testing.T) { mem := NewMemory() t.Log("start empty") - actItems, actErr := mem.Updated([]Kind{}, time.Time{}) + actItems, actErr := mem.Updated([]item.Kind{}, time.Time{}) if actErr != nil { t.Errorf("exp nil, got %v", actErr) } @@ -20,11 +22,11 @@ func TestMemoryItem(t *testing.T) { } t.Log("add one") - t1 := NewItem(Kind("kinda"), "test") + t1 := item.NewItem(item.Kind("kinda"), "test") if actErr := mem.Update(t1); actErr != nil { t.Errorf("exp nil, got %v", actErr) } - actItems, actErr = mem.Updated([]Kind{}, time.Time{}) + actItems, actErr = mem.Updated([]item.Kind{}, time.Time{}) if actErr != nil { t.Errorf("exp nil, got %v", actErr) } @@ -38,11 +40,11 @@ func TestMemoryItem(t *testing.T) { before := time.Now() t.Log("add second") - t2 := NewItem(Kind("kindb"), "test 2") + t2 := item.NewItem(item.Kind("kindb"), "test 2") if actErr := mem.Update(t2); actErr != nil { t.Errorf("exp nil, got %v", actErr) } - actItems, actErr = mem.Updated([]Kind{}, time.Time{}) + actItems, actErr = mem.Updated([]item.Kind{}, time.Time{}) if actErr != nil { t.Errorf("exp nil, got %v", actErr) } @@ -56,7 +58,7 @@ func TestMemoryItem(t *testing.T) { t.Errorf("exp %v, got %v", actItems[1].ID, t2.ID) } - actItems, actErr = mem.Updated([]Kind{}, before) + actItems, actErr = mem.Updated([]item.Kind{}, before) if actErr != nil { t.Errorf("exp nil, got %v", actErr) } @@ -72,7 +74,7 @@ func TestMemoryItem(t *testing.T) { if actErr := mem.Update(t1); actErr != nil { t.Errorf("exp nil, got %v", actErr) } - actItems, actErr = mem.Updated([]Kind{}, before) + actItems, actErr = mem.Updated([]item.Kind{}, before) if actErr != nil { t.Errorf("exp nil, got %v", actErr) } @@ -87,7 +89,7 @@ func TestMemoryItem(t *testing.T) { } t.Log("select kind") - actItems, actErr = mem.Updated([]Kind{"kinda"}, time.Time{}) + actItems, actErr = mem.Updated([]item.Kind{"kinda"}, time.Time{}) if actErr != nil { t.Errorf("exp nil, got %v", actErr) } diff --git a/sync-service/sqlite.go b/sync/service/sqlite.go similarity index 94% rename from sync-service/sqlite.go rename to sync/service/sqlite.go index 50ecc38..3359748 100644 --- a/sync-service/sqlite.go +++ b/sync/service/sqlite.go @@ -7,6 +7,7 @@ import ( "strings" "time" + "go-mod.ewintr.nl/planner/sync/item" _ "modernc.org/sqlite" ) @@ -49,7 +50,7 @@ func NewSqlite(dbPath string) (*Sqlite, error) { return s, nil } -func (s *Sqlite) Update(item Item) error { +func (s *Sqlite) Update(item item.Item) error { if _, err := s.db.Exec(` INSERT INTO items (id, kind, updated, deleted, body) @@ -67,7 +68,7 @@ body=excluded.body`, return nil } -func (s *Sqlite) Updated(ks []Kind, t time.Time) ([]Item, error) { +func (s *Sqlite) Updated(ks []item.Kind, t time.Time) ([]item.Item, error) { query := ` SELECT id, kind, updated, deleted, body FROM items @@ -93,10 +94,10 @@ WHERE updated > ?` } } - result := make([]Item, 0) + result := make([]item.Item, 0) defer rows.Close() for rows.Next() { - var item Item + var item item.Item if err := rows.Scan(&item.ID, &item.Kind, &item.Updated, &item.Deleted, &item.Body); err != nil { return nil, fmt.Errorf("%w: %v", ErrSqliteFailure, err) } diff --git a/sync/service/storage.go b/sync/service/storage.go new file mode 100644 index 0000000..5672052 --- /dev/null +++ b/sync/service/storage.go @@ -0,0 +1,17 @@ +package main + +import ( + "errors" + "time" + + "go-mod.ewintr.nl/planner/sync/item" +) + +var ( + ErrNotFound = errors.New("not found") +) + +type Syncer interface { + Update(item item.Item) error + Updated(kind []item.Kind, t time.Time) ([]item.Item, error) +}