sync handler
This commit is contained in:
parent
99007ae0d1
commit
5f060e0470
|
@ -3,9 +3,11 @@ package handler
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"code.ewintr.nl/planner/planner"
|
||||||
"code.ewintr.nl/planner/storage"
|
"code.ewintr.nl/planner/storage"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -13,36 +15,76 @@ func Index(w http.ResponseWriter, r *http.Request) {
|
||||||
fmt.Fprint(w, `{"status":"ok"}`)
|
fmt.Fprint(w, `{"status":"ok"}`)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type ChangeSummary struct {
|
||||||
|
Updated []planner.Syncable
|
||||||
|
Deleted []string
|
||||||
|
}
|
||||||
|
|
||||||
func NewSyncHandler(mem storage.Syncer) func(w http.ResponseWriter, r *http.Request) {
|
func NewSyncHandler(mem storage.Syncer) func(w http.ResponseWriter, r *http.Request) {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
timestamp := time.Time{}
|
switch r.Method {
|
||||||
tsStr := r.URL.Query().Get("ts")
|
case http.MethodGet:
|
||||||
if tsStr != "" {
|
timestamp := time.Time{}
|
||||||
var err error
|
tsStr := r.URL.Query().Get("ts")
|
||||||
if timestamp, err = time.Parse(time.RFC3339, tsStr); err != nil {
|
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.Updated(timestamp)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
deleted, err := mem.Deleted(timestamp)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
result := ChangeSummary{
|
||||||
|
Updated: items,
|
||||||
|
Deleted: deleted,
|
||||||
|
}
|
||||||
|
|
||||||
|
body, err := json.Marshal(result)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Fprint(w, string(body))
|
||||||
|
|
||||||
|
case http.MethodPost:
|
||||||
|
body, err := io.ReadAll(r.Body)
|
||||||
|
if err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
defer r.Body.Close()
|
||||||
|
|
||||||
items, err := mem.Updated(timestamp)
|
var changes ChangeSummary
|
||||||
if err != nil {
|
if err := json.Unmarshal(body, changes); err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||||
return
|
}
|
||||||
}
|
|
||||||
|
|
||||||
body, err := json.Marshal(items)
|
for _, updated := range changes.Updated {
|
||||||
if err != nil {
|
if err := mem.Update(updated); err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for _, deleted := range changes.Deleted {
|
||||||
|
if err := mem.Delete(deleted); err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
w.WriteHeader(http.StatusNoContent)
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Fprint(w, string(body))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewStoreHandler(mem storage.Syncer) func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue