diff --git a/pkg/api/api.go b/pkg/api/api.go index 580da85..4e22860 100644 --- a/pkg/api/api.go +++ b/pkg/api/api.go @@ -71,6 +71,10 @@ func writeResponse(w http.ResponseWriter, r *http.Request, data interface{}, err } } +func reqErr(w http.ResponseWriter, err error, code int) { + http.Error(w, err.Error(), code) +} + func decodeParams(d interface{}, r *http.Request) error { params := chi.RouteContext(r.Context()).URLParams m := make(map[string]string, len(params.Keys)) @@ -93,5 +97,5 @@ func decodeParams(d interface{}, r *http.Request) error { } func badReq(w http.ResponseWriter, err error) { - http.Error(w, err.Error(), http.StatusBadRequest) + reqErr(w, err, http.StatusBadRequest) } diff --git a/pkg/api/talkgroups.go b/pkg/api/talkgroups.go index 9051539..e16f8f3 100644 --- a/pkg/api/talkgroups.go +++ b/pkg/api/talkgroups.go @@ -1,6 +1,7 @@ package api import ( + "fmt" "net/http" "dynatron.me/x/stillbox/internal/forms" @@ -23,13 +24,35 @@ func (tga *talkgroupAPI) routes() http.Handler { return r } +type tgParams struct { + System *int `param:"system"` + ID *int `param:"id"` +} + +func (t tgParams) haveBoth() bool { + return t.System != nil && t.ID != nil +} + +func (t tgParams) ToID() talkgroups.ID { + nilOr := func(i *int) uint32 { + if i == nil { + return 0 + } + + return uint32(*i) + } + + return talkgroups.ID{ + System: nilOr(t.System), + Talkgroup: nilOr(t.ID), + } +} + func (tga *talkgroupAPI) talkgroup(w http.ResponseWriter, r *http.Request) { ctx := r.Context() tgs := talkgroups.StoreFrom(ctx) - p := struct { - System *int `param:"system"` - ID *int `param:"id"` - }{} + + var p tgParams err := decodeParams(&p, r) if err != nil { @@ -51,4 +74,43 @@ func (tga *talkgroupAPI) talkgroup(w http.ResponseWriter, r *http.Request) { } func (tga *talkgroupAPI) putTalkgroup(w http.ResponseWriter, r *http.Request) { + var id tgParams + err := decodeParams(&id, r) + if err != nil { + badReq(w, err) + return + } + /* + ctx := r.Context() + tgs := talkgroups.StoreFrom(ctx) + + tg, err := tgs.TG(ctx, id.ToID()) + switch err { + case nil: + case talkgroups.ErrNotFound: + reqErr(w, err, http.StatusNotFound) + return + default: + reqErr(w, err, http.StatusInternalServerError) + } + */ + + input := struct { + Name *string `form:"name"` + AlphaTag *string `form:"alpha_tag"` + TgGroup *string `form:"tg_group"` + Frequency *int32 `form:"frequency"` + Metadata []byte `form:"metadata"` + Tags []string `form:"tags"` + Alert *bool `form:"alert"` + AlertConfig []byte `form:"alert_config"` + Weight *float32 `form:"weight"` + }{} + + err = forms.Unmarshal(r, &input, forms.WithAcceptBlank(), forms.WithOmitEmpty()) + if err != nil { + reqErr(w, err, http.StatusBadRequest) + return + } + fmt.Fprintf(w, "%+v\n", input) } diff --git a/pkg/server/server.go b/pkg/server/server.go index ed11996..366bd6c 100644 --- a/pkg/server/server.go +++ b/pkg/server/server.go @@ -61,7 +61,7 @@ func New(ctx context.Context, cfg *config.Config) (*Server, error) { } tgCache := talkgroups.NewCache() - api := api.New(tgCache) + api := api.New() srv := &Server{ auth: authenticator,