sync endpoint
This commit is contained in:
parent
45004829c7
commit
a1ee001bba
|
@ -4,6 +4,7 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"time"
|
||||||
|
|
||||||
"code.ewintr.nl/planner/storage"
|
"code.ewintr.nl/planner/storage"
|
||||||
)
|
)
|
||||||
|
@ -14,8 +15,17 @@ func Index(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
func NewSyncHandler(mem storage.Repository) func(w http.ResponseWriter, r *http.Request) {
|
func NewSyncHandler(mem storage.Repository) func(w http.ResponseWriter, r *http.Request) {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
token := r.URL.Query().Get("token")
|
timestamp := time.Time{}
|
||||||
items, err := mem.NewSince(token)
|
tsStr := r.URL.Query().Get("ts")
|
||||||
|
if tsStr != "" {
|
||||||
|
var err error
|
||||||
|
if timestamp, err = time.Parse(time.RFC3339, tsStr); err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
items, err := mem.NewSince(timestamp)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
|
@ -27,7 +37,7 @@ func NewSyncHandler(mem storage.Repository) func(w http.ResponseWriter, r *http.
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Fprint(w, body)
|
fmt.Fprint(w, string(body))
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
2
main.go
2
main.go
|
@ -13,5 +13,5 @@ func main() {
|
||||||
http.HandleFunc("/", handler.Index)
|
http.HandleFunc("/", handler.Index)
|
||||||
http.HandleFunc("/sync", handler.NewSyncHandler(mem))
|
http.HandleFunc("/sync", handler.NewSyncHandler(mem))
|
||||||
|
|
||||||
http.ListenAndServer(":8092", nil)
|
http.ListenAndServe(":8092", nil)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,10 @@
|
||||||
package storage
|
package storage
|
||||||
|
|
||||||
import "code.ewintr.nl/planner/planner"
|
import (
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"code.ewintr.nl/planner/planner"
|
||||||
|
)
|
||||||
|
|
||||||
type Memory struct {
|
type Memory struct {
|
||||||
items map[string]planner.Syncable
|
items map[string]planner.Syncable
|
||||||
|
@ -12,7 +16,19 @@ func NewMemory() *Memory {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Memory) StoreProject(item planner.Syncable) error {
|
func (m *Memory) NewSince(timestamp time.Time) ([]planner.Syncable, error) {
|
||||||
|
result := make([]planner.Syncable, 0)
|
||||||
|
|
||||||
|
for _, i := range m.items {
|
||||||
|
if timestamp.IsZero() || i.Updated().After(timestamp) {
|
||||||
|
result = append(result, i)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Memory) Store(item planner.Syncable) error {
|
||||||
m.items[item.ID()] = item
|
m.items[item.ID()] = item
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|
Loading…
Reference in New Issue