sync handler
This commit is contained in:
parent
99007ae0d1
commit
5f060e0470
|
@ -3,9 +3,11 @@ package handler
|
|||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"code.ewintr.nl/planner/planner"
|
||||
"code.ewintr.nl/planner/storage"
|
||||
)
|
||||
|
||||
|
@ -13,8 +15,15 @@ func Index(w http.ResponseWriter, r *http.Request) {
|
|||
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) {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
switch r.Method {
|
||||
case http.MethodGet:
|
||||
timestamp := time.Time{}
|
||||
tsStr := r.URL.Query().Get("ts")
|
||||
if tsStr != "" {
|
||||
|
@ -31,18 +40,51 @@ func NewSyncHandler(mem storage.Syncer) func(w http.ResponseWriter, r *http.Requ
|
|||
return
|
||||
}
|
||||
|
||||
body, err := json.Marshal(items)
|
||||
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)
|
||||
return
|
||||
}
|
||||
defer r.Body.Close()
|
||||
|
||||
var changes ChangeSummary
|
||||
if err := json.Unmarshal(body, changes); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
}
|
||||
|
||||
func NewStoreHandler(mem storage.Syncer) func(w http.ResponseWriter, r *http.Request) {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
for _, updated := range changes.Updated {
|
||||
if err := mem.Update(updated); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue