2024-11-10 14:44:52 -05:00
|
|
|
package rest
|
2024-11-09 13:14:31 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"dynatron.me/x/stillbox/internal/forms"
|
2024-11-10 10:13:38 -05:00
|
|
|
"dynatron.me/x/stillbox/pkg/database"
|
2024-11-09 13:14:31 -05:00
|
|
|
"dynatron.me/x/stillbox/pkg/talkgroups"
|
2024-11-15 11:15:12 -05:00
|
|
|
"dynatron.me/x/stillbox/pkg/talkgroups/importer"
|
2024-11-09 13:14:31 -05:00
|
|
|
|
|
|
|
"github.com/go-chi/chi/v5"
|
|
|
|
)
|
|
|
|
|
|
|
|
type talkgroupAPI struct {
|
|
|
|
}
|
|
|
|
|
2024-11-10 14:40:50 -05:00
|
|
|
func (tga *talkgroupAPI) Subrouter() http.Handler {
|
2024-11-09 13:14:31 -05:00
|
|
|
r := chi.NewMux()
|
|
|
|
|
2024-11-20 07:26:59 -05:00
|
|
|
r.Get(`/{system:\d+}/{id:\d+}`, tga.get)
|
|
|
|
r.Put(`/{system:\d+}/{id:\d+}`, tga.put)
|
2024-11-20 09:37:57 -05:00
|
|
|
r.Put(`/{system:\d+}`, tga.putTalkgroups)
|
2024-11-20 07:26:59 -05:00
|
|
|
r.Get(`/{system:\d+}/`, tga.get)
|
2024-11-10 14:44:52 -05:00
|
|
|
r.Get("/", tga.get)
|
2024-11-13 09:24:11 -05:00
|
|
|
r.Post("/import", tga.tgImport)
|
2024-11-09 13:14:31 -05:00
|
|
|
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2024-11-09 21:04:16 -05:00
|
|
|
type tgParams struct {
|
|
|
|
System *int `param:"system"`
|
|
|
|
ID *int `param:"id"`
|
|
|
|
}
|
|
|
|
|
2024-11-20 11:07:58 -05:00
|
|
|
func (t tgParams) hasBoth() bool {
|
2024-11-09 21:04:16 -05:00
|
|
|
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),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-10 14:44:52 -05:00
|
|
|
func (tga *talkgroupAPI) get(w http.ResponseWriter, r *http.Request) {
|
2024-11-09 13:14:31 -05:00
|
|
|
ctx := r.Context()
|
|
|
|
tgs := talkgroups.StoreFrom(ctx)
|
2024-11-09 21:04:16 -05:00
|
|
|
|
|
|
|
var p tgParams
|
2024-11-09 13:14:31 -05:00
|
|
|
|
|
|
|
err := decodeParams(&p, r)
|
|
|
|
if err != nil {
|
2024-11-10 14:40:50 -05:00
|
|
|
wErr(w, r, badRequest(err))
|
2024-11-09 13:14:31 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var res interface{}
|
|
|
|
switch {
|
2024-11-20 11:07:58 -05:00
|
|
|
case p.hasBoth():
|
2024-11-09 13:14:31 -05:00
|
|
|
res, err = tgs.TG(ctx, talkgroups.TG(*p.System, *p.ID))
|
|
|
|
case p.System != nil:
|
|
|
|
res, err = tgs.SystemTGs(ctx, int32(*p.System))
|
|
|
|
default:
|
2024-11-20 11:07:58 -05:00
|
|
|
// get all talkgroups
|
2024-11-09 13:14:31 -05:00
|
|
|
res, err = tgs.TGs(ctx, nil)
|
|
|
|
}
|
|
|
|
|
2024-11-10 14:40:50 -05:00
|
|
|
if err != nil {
|
|
|
|
wErr(w, r, autoError(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
respond(w, r, res)
|
2024-11-09 13:14:31 -05:00
|
|
|
}
|
|
|
|
|
2024-11-10 14:44:52 -05:00
|
|
|
func (tga *talkgroupAPI) put(w http.ResponseWriter, r *http.Request) {
|
2024-11-09 21:04:16 -05:00
|
|
|
var id tgParams
|
|
|
|
err := decodeParams(&id, r)
|
|
|
|
if err != nil {
|
2024-11-10 14:40:50 -05:00
|
|
|
wErr(w, r, badRequest(err))
|
2024-11-09 21:04:16 -05:00
|
|
|
return
|
|
|
|
}
|
2024-11-10 10:13:38 -05:00
|
|
|
|
|
|
|
ctx := r.Context()
|
|
|
|
tgs := talkgroups.StoreFrom(ctx)
|
|
|
|
|
|
|
|
input := database.UpdateTalkgroupParams{}
|
|
|
|
|
|
|
|
err = forms.Unmarshal(r, &input, forms.WithTag("json"), forms.WithAcceptBlank(), forms.WithOmitEmpty())
|
2024-11-09 21:04:16 -05:00
|
|
|
if err != nil {
|
2024-11-10 14:40:50 -05:00
|
|
|
wErr(w, r, badRequest(err))
|
2024-11-09 21:04:16 -05:00
|
|
|
return
|
|
|
|
}
|
2024-11-10 10:13:38 -05:00
|
|
|
|
2024-11-20 07:26:59 -05:00
|
|
|
input.Learned = nil // ignore for this call
|
|
|
|
|
2024-11-10 10:13:38 -05:00
|
|
|
record, err := tgs.UpdateTG(ctx, input)
|
|
|
|
if err != nil {
|
2024-11-10 14:40:50 -05:00
|
|
|
wErr(w, r, autoError(err))
|
2024-11-10 10:13:38 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-11-10 14:40:50 -05:00
|
|
|
respond(w, r, record)
|
2024-11-09 13:14:31 -05:00
|
|
|
}
|
2024-11-12 19:20:03 -05:00
|
|
|
|
|
|
|
func (tga *talkgroupAPI) tgImport(w http.ResponseWriter, r *http.Request) {
|
2024-11-15 11:15:12 -05:00
|
|
|
var impJob importer.ImportJob
|
2024-11-12 19:20:03 -05:00
|
|
|
err := forms.Unmarshal(r, &impJob, forms.WithTag("json"), forms.WithAcceptBlank(), forms.WithOmitEmpty())
|
|
|
|
if err != nil {
|
|
|
|
wErr(w, r, badRequest(err))
|
|
|
|
return
|
|
|
|
}
|
2024-11-13 09:24:11 -05:00
|
|
|
recs, err := impJob.Import(r.Context())
|
2024-11-12 19:20:03 -05:00
|
|
|
if err != nil {
|
|
|
|
wErr(w, r, autoError(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
respond(w, r, recs)
|
|
|
|
}
|
2024-11-20 07:26:59 -05:00
|
|
|
|
|
|
|
func (tga *talkgroupAPI) putTalkgroups(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var id tgParams
|
|
|
|
err := decodeParams(&id, r)
|
|
|
|
if err != nil {
|
|
|
|
wErr(w, r, badRequest(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if id.System == nil { // don't think this would ever happen
|
|
|
|
wErr(w, r, badRequest(talkgroups.ErrNoSuchSystem))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := r.Context()
|
|
|
|
tgs := talkgroups.StoreFrom(ctx)
|
|
|
|
|
|
|
|
var input []database.UpsertTalkgroupParams
|
|
|
|
|
|
|
|
err = forms.Unmarshal(r, &input, forms.WithTag("json"), forms.WithAcceptBlank(), forms.WithOmitEmpty())
|
|
|
|
if err != nil {
|
|
|
|
wErr(w, r, badRequest(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
record, err := tgs.UpsertTGs(ctx, *id.System, input)
|
|
|
|
if err != nil {
|
|
|
|
wErr(w, r, autoError(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
respond(w, r, record)
|
|
|
|
}
|