planner/handler/handler.go

44 lines
887 B
Go
Raw Normal View History

2024-08-21 16:35:44 +02:00
package handler
import (
"encoding/json"
"fmt"
"net/http"
2024-08-22 07:18:50 +02:00
"time"
2024-08-21 16:35:44 +02:00
"code.ewintr.nl/planner/storage"
)
func Index(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, `{"status":"ok"}`)
}
func NewSyncHandler(mem storage.Repository) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
2024-08-22 07:18:50 +02:00
timestamp := time.Time{}
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)
2024-08-21 16:35:44 +02:00
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
body, err := json.Marshal(items)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
2024-08-22 07:18:50 +02:00
fmt.Fprint(w, string(body))
2024-08-21 16:35:44 +02:00
}
}