Compare commits
4 commits
6e1640e4b4
...
36c982473c
Author | SHA1 | Date | |
---|---|---|---|
36c982473c | |||
bb9ff7234c | |||
1729caf06a | |||
95193685c2 |
9 changed files with 41 additions and 150 deletions
|
@ -34,36 +34,30 @@ func (a *api) Subrouter() http.Handler {
|
||||||
return r
|
return r
|
||||||
}
|
}
|
||||||
|
|
||||||
type errResponse struct {
|
var statusMapping = map[error]int{
|
||||||
text string
|
talkgroups.ErrNotFound: http.StatusNotFound,
|
||||||
code int
|
pgx.ErrNoRows: http.StatusNotFound,
|
||||||
}
|
}
|
||||||
|
|
||||||
var statusMapping = map[error]errResponse{
|
func httpCode(err error) int {
|
||||||
talkgroups.ErrNotFound: {talkgroups.ErrNotFound.Error(), http.StatusNotFound},
|
|
||||||
pgx.ErrNoRows: {"no such record", http.StatusNotFound},
|
|
||||||
}
|
|
||||||
|
|
||||||
func httpCode(err error) (string, int) {
|
|
||||||
c, ok := statusMapping[err]
|
c, ok := statusMapping[err]
|
||||||
if ok {
|
if ok {
|
||||||
return c.text, c.code
|
return c
|
||||||
}
|
}
|
||||||
|
|
||||||
for e, c := range statusMapping { // check if err wraps an error we know about
|
for e, c := range statusMapping { // check if err wraps an error we know about
|
||||||
if errors.Is(err, e) {
|
if errors.Is(err, e) {
|
||||||
return c.text, c.code
|
return c
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return err.Error(), http.StatusInternalServerError
|
return http.StatusInternalServerError
|
||||||
}
|
}
|
||||||
|
|
||||||
func writeResponse(w http.ResponseWriter, r *http.Request, data interface{}, err error) {
|
func writeResponse(w http.ResponseWriter, r *http.Request, data interface{}, err error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error().Str("path", r.URL.Path).Err(err).Msg("request failed")
|
log.Error().Str("path", r.URL.Path).Err(err).Msg("request failed")
|
||||||
text, code := httpCode(err)
|
http.Error(w, err.Error(), httpCode(err))
|
||||||
http.Error(w, text, code)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -72,8 +66,7 @@ func writeResponse(w http.ResponseWriter, r *http.Request, data interface{}, err
|
||||||
err = enc.Encode(data)
|
err = enc.Encode(data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error().Str("path", r.URL.Path).Err(err).Msg("response marshal failed")
|
log.Error().Str("path", r.URL.Path).Err(err).Msg("response marshal failed")
|
||||||
text, code := httpCode(err)
|
http.Error(w, err.Error(), httpCode(err))
|
||||||
http.Error(w, text, code)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,11 +1,10 @@
|
||||||
package api
|
package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"dynatron.me/x/stillbox/internal/forms"
|
"dynatron.me/x/stillbox/internal/forms"
|
||||||
"dynatron.me/x/stillbox/pkg/database"
|
|
||||||
"dynatron.me/x/stillbox/pkg/talkgroups"
|
"dynatron.me/x/stillbox/pkg/talkgroups"
|
||||||
|
|
||||||
"github.com/go-chi/chi/v5"
|
"github.com/go-chi/chi/v5"
|
||||||
|
@ -81,27 +80,37 @@ func (tga *talkgroupAPI) putTalkgroup(w http.ResponseWriter, r *http.Request) {
|
||||||
badReq(w, err)
|
badReq(w, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
/*
|
||||||
ctx := r.Context()
|
ctx := r.Context()
|
||||||
tgs := talkgroups.StoreFrom(ctx)
|
tgs := talkgroups.StoreFrom(ctx)
|
||||||
|
|
||||||
input := database.UpdateTalkgroupParams{}
|
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)
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
err = forms.Unmarshal(r, &input, forms.WithTag("json"), forms.WithAcceptBlank(), forms.WithOmitEmpty())
|
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 {
|
if err != nil {
|
||||||
writeResponse(w, r, nil, err)
|
reqErr(w, err, http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
input.ID = id.ToID().Pack()
|
fmt.Fprintf(w, "%+v\n", input)
|
||||||
|
|
||||||
record, err := tgs.UpdateTG(ctx, input)
|
|
||||||
if err != nil {
|
|
||||||
writeResponse(w, r, nil, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
err = json.NewEncoder(w).Encode(record)
|
|
||||||
if err != nil {
|
|
||||||
writeResponse(w, r, nil, err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,7 +5,7 @@
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<div>
|
<div>
|
||||||
<form action="/api/login" method="POST">
|
<form action="/login" method="POST">
|
||||||
<label for="username">Username: </label>
|
<label for="username">Username: </label>
|
||||||
<input type="text" name="username" />
|
<input type="text" name="username" />
|
||||||
<label for="password">Password: </label>
|
<label for="password">Password: </label>
|
||||||
|
|
|
@ -9,6 +9,3 @@ func (g GetTalkgroupsWithLearnedRow) GetLearned() bool { retur
|
||||||
func (g GetTalkgroupsWithLearnedBySystemRow) GetTalkgroup() Talkgroup { return g.Talkgroup }
|
func (g GetTalkgroupsWithLearnedBySystemRow) GetTalkgroup() Talkgroup { return g.Talkgroup }
|
||||||
func (g GetTalkgroupsWithLearnedBySystemRow) GetSystem() System { return g.System }
|
func (g GetTalkgroupsWithLearnedBySystemRow) GetSystem() System { return g.System }
|
||||||
func (g GetTalkgroupsWithLearnedBySystemRow) GetLearned() bool { return g.Learned }
|
func (g GetTalkgroupsWithLearnedBySystemRow) GetLearned() bool { return g.Learned }
|
||||||
func (g Talkgroup) GetTalkgroup() Talkgroup { return g }
|
|
||||||
func (g Talkgroup) GetSystem() System { return System{ID: int(g.SystemID)} }
|
|
||||||
func (g Talkgroup) GetLearned() bool { return false }
|
|
||||||
|
|
|
@ -39,7 +39,6 @@ type Querier interface {
|
||||||
SetCallTranscript(ctx context.Context, iD uuid.UUID, transcript *string) error
|
SetCallTranscript(ctx context.Context, iD uuid.UUID, transcript *string) error
|
||||||
SetTalkgroupTags(ctx context.Context, sys int, tg int, tags []string) error
|
SetTalkgroupTags(ctx context.Context, sys int, tg int, tags []string) error
|
||||||
UpdatePassword(ctx context.Context, username string, password string) error
|
UpdatePassword(ctx context.Context, username string, password string) error
|
||||||
UpdateTalkgroup(ctx context.Context, arg UpdateTalkgroupParams) (Talkgroup, error)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var _ Querier = (*Queries)(nil)
|
var _ Querier = (*Queries)(nil)
|
||||||
|
|
|
@ -472,63 +472,3 @@ func (q *Queries) SetTalkgroupTags(ctx context.Context, sys int, tg int, tags []
|
||||||
_, err := q.db.Exec(ctx, setTalkgroupTags, sys, tg, tags)
|
_, err := q.db.Exec(ctx, setTalkgroupTags, sys, tg, tags)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
const updateTalkgroup = `-- name: UpdateTalkgroup :one
|
|
||||||
UPDATE talkgroups
|
|
||||||
SET
|
|
||||||
name = COALESCE($1, name),
|
|
||||||
alpha_tag = COALESCE($2, alpha_tag),
|
|
||||||
tg_group = COALESCE($3, tg_group),
|
|
||||||
frequency = COALESCE($4, frequency),
|
|
||||||
metadata = COALESCE($5, metadata),
|
|
||||||
tags = COALESCE($6, tags),
|
|
||||||
alert = COALESCE($7, alert),
|
|
||||||
alert_config = COALESCE($8, alert_config),
|
|
||||||
weight = COALESCE($9, weight)
|
|
||||||
WHERE id = $10
|
|
||||||
RETURNING id, system_id, tgid, name, alpha_tag, tg_group, frequency, metadata, tags, alert, alert_config, weight
|
|
||||||
`
|
|
||||||
|
|
||||||
type UpdateTalkgroupParams struct {
|
|
||||||
Name *string `json:"name"`
|
|
||||||
AlphaTag *string `json:"alpha_tag"`
|
|
||||||
TgGroup *string `json:"tg_group"`
|
|
||||||
Frequency *int32 `json:"frequency"`
|
|
||||||
Metadata []byte `json:"metadata"`
|
|
||||||
Tags []string `json:"tags"`
|
|
||||||
Alert *bool `json:"alert"`
|
|
||||||
AlertConfig []byte `json:"alert_config"`
|
|
||||||
Weight *float32 `json:"weight"`
|
|
||||||
ID int64 `json:"id"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func (q *Queries) UpdateTalkgroup(ctx context.Context, arg UpdateTalkgroupParams) (Talkgroup, error) {
|
|
||||||
row := q.db.QueryRow(ctx, updateTalkgroup,
|
|
||||||
arg.Name,
|
|
||||||
arg.AlphaTag,
|
|
||||||
arg.TgGroup,
|
|
||||||
arg.Frequency,
|
|
||||||
arg.Metadata,
|
|
||||||
arg.Tags,
|
|
||||||
arg.Alert,
|
|
||||||
arg.AlertConfig,
|
|
||||||
arg.Weight,
|
|
||||||
arg.ID,
|
|
||||||
)
|
|
||||||
var i Talkgroup
|
|
||||||
err := row.Scan(
|
|
||||||
&i.ID,
|
|
||||||
&i.SystemID,
|
|
||||||
&i.Tgid,
|
|
||||||
&i.Name,
|
|
||||||
&i.AlphaTag,
|
|
||||||
&i.TgGroup,
|
|
||||||
&i.Frequency,
|
|
||||||
&i.Metadata,
|
|
||||||
&i.Tags,
|
|
||||||
&i.Alert,
|
|
||||||
&i.AlertConfig,
|
|
||||||
&i.Weight,
|
|
||||||
)
|
|
||||||
return i, err
|
|
||||||
}
|
|
||||||
|
|
|
@ -17,15 +17,7 @@ import (
|
||||||
|
|
||||||
type tgMap map[ID]*Talkgroup
|
type tgMap map[ID]*Talkgroup
|
||||||
|
|
||||||
var (
|
|
||||||
ErrNotFound = errors.New("talkgroup not found")
|
|
||||||
ErrNoSuchSystem = errors.New("no such system")
|
|
||||||
)
|
|
||||||
|
|
||||||
type Store interface {
|
type Store interface {
|
||||||
// UpdateTG updates a talkgroup record.
|
|
||||||
UpdateTG(ctx context.Context, input database.UpdateTalkgroupParams) (*Talkgroup, error)
|
|
||||||
|
|
||||||
// TG retrieves a Talkgroup from the Store.
|
// TG retrieves a Talkgroup from the Store.
|
||||||
TG(ctx context.Context, tg ID) (*Talkgroup, error)
|
TG(ctx context.Context, tg ID) (*Talkgroup, error)
|
||||||
|
|
||||||
|
@ -221,6 +213,8 @@ func (t *cache) Load(ctx context.Context, tgs []int64) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var ErrNotFound = errors.New("talkgroup not found")
|
||||||
|
|
||||||
func (t *cache) Weight(ctx context.Context, id ID, tm time.Time) float64 {
|
func (t *cache) Weight(ctx context.Context, id ID, tm time.Time) float64 {
|
||||||
tg, err := t.TG(ctx, id)
|
tg, err := t.TG(ctx, id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -296,23 +290,3 @@ func (t *cache) SystemName(ctx context.Context, id int) (name string, has bool)
|
||||||
|
|
||||||
return n, has
|
return n, has
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *cache) UpdateTG(ctx context.Context, input database.UpdateTalkgroupParams) (*Talkgroup, error) {
|
|
||||||
sysName, has := t.SystemName(ctx, int(Unpack(input.ID).System))
|
|
||||||
if !has {
|
|
||||||
return nil, ErrNoSuchSystem
|
|
||||||
}
|
|
||||||
|
|
||||||
tg, err := database.FromCtx(ctx).UpdateTalkgroup(ctx, input)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
record := &Talkgroup{
|
|
||||||
Talkgroup: tg,
|
|
||||||
System: database.System{ID: int(tg.SystemID), Name: sysName},
|
|
||||||
}
|
|
||||||
t.add(record)
|
|
||||||
|
|
||||||
return record, nil
|
|
||||||
}
|
|
||||||
|
|
|
@ -49,13 +49,6 @@ func (t ID) Pack() int64 {
|
||||||
return int64((int64(t.System) << 32) | int64(t.Talkgroup))
|
return int64((int64(t.System) << 32) | int64(t.Talkgroup))
|
||||||
}
|
}
|
||||||
|
|
||||||
func Unpack(id int64) ID {
|
|
||||||
return ID{
|
|
||||||
System: uint32(id >> 32),
|
|
||||||
Talkgroup: uint32(id & 0xffffffff),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (t ID) String() string {
|
func (t ID) String() string {
|
||||||
return fmt.Sprintf("%d:%d", t.System, t.Talkgroup)
|
return fmt.Sprintf("%d:%d", t.System, t.Talkgroup)
|
||||||
|
|
||||||
|
|
|
@ -104,20 +104,6 @@ FROM talkgroups_learned tgl
|
||||||
JOIN systems sys ON tgl.system_id = sys.id
|
JOIN systems sys ON tgl.system_id = sys.id
|
||||||
WHERE ignored IS NOT TRUE;
|
WHERE ignored IS NOT TRUE;
|
||||||
|
|
||||||
|
|
||||||
-- name: GetSystemName :one
|
-- name: GetSystemName :one
|
||||||
SELECT name FROM systems WHERE id = sqlc.arg(system_id);
|
SELECT name FROM systems WHERE id = sqlc.arg(system_id);
|
||||||
|
|
||||||
-- name: UpdateTalkgroup :one
|
|
||||||
UPDATE talkgroups
|
|
||||||
SET
|
|
||||||
name = COALESCE(sqlc.narg('name'), name),
|
|
||||||
alpha_tag = COALESCE(sqlc.narg('alpha_tag'), alpha_tag),
|
|
||||||
tg_group = COALESCE(sqlc.narg('tg_group'), tg_group),
|
|
||||||
frequency = COALESCE(sqlc.narg('frequency'), frequency),
|
|
||||||
metadata = COALESCE(sqlc.narg('metadata'), metadata),
|
|
||||||
tags = COALESCE(sqlc.narg('tags'), tags),
|
|
||||||
alert = COALESCE(sqlc.narg('alert'), alert),
|
|
||||||
alert_config = COALESCE(sqlc.narg('alert_config'), alert_config),
|
|
||||||
weight = COALESCE(sqlc.narg('weight'), weight)
|
|
||||||
WHERE id = @id
|
|
||||||
RETURNING *;
|
|
||||||
|
|
Loading…
Reference in a new issue