diff --git a/cmd/gordio/main.go b/cmd/gordio/main.go index f5be99a..d0ea913 100644 --- a/cmd/gordio/main.go +++ b/cmd/gordio/main.go @@ -20,7 +20,7 @@ func main() { Use: gordio.AppName, } rootCmd.PersistentFlags().StringP("config", "c", "config.yaml", "config file") - + err := rootCmd.ParseFlags(os.Args) if err != nil { log.Fatal().Err(err).Msg("parsing flags") diff --git a/pkg/gordio/database/models.go b/pkg/gordio/database/models.go index 7ddba7a..4ebd199 100644 --- a/pkg/gordio/database/models.go +++ b/pkg/gordio/database/models.go @@ -67,11 +67,16 @@ type Talkgroup struct { System int32 Name pgtype.Text Frequency pgtype.Int4 - GroupID pgtype.Int4 + Tgid int32 AutoCreated pgtype.Bool Metadata []byte } +type TalkgroupsTag struct { + TalkgroupID int32 + Tags []string +} + type User struct { ID int32 Username string diff --git a/pkg/gordio/database/talkgroups.sql.go b/pkg/gordio/database/talkgroups.sql.go new file mode 100644 index 0000000..851046f --- /dev/null +++ b/pkg/gordio/database/talkgroups.sql.go @@ -0,0 +1,88 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.26.0 +// source: talkgroups.sql + +package database + +import ( + "context" +) + +const getTalkgroupTags = `-- name: GetTalkgroupTags :one +SELECT tags FROM talkgroups_tags +WHERE talkgroup_id = $1 +` + +func (q *Queries) GetTalkgroupTags(ctx context.Context, talkgroupID int32) ([]string, error) { + row := q.db.QueryRow(ctx, getTalkgroupTags, talkgroupID) + var tags []string + err := row.Scan(&tags) + return tags, err +} + +const getTalkgroupsWithAllTags = `-- name: GetTalkgroupsWithAllTags :many +SELECT talkgroup_id, tags FROM talkgroups_tags +WHERE tags && ARRAY[$1] +` + +func (q *Queries) GetTalkgroupsWithAllTags(ctx context.Context, tags []string) ([]TalkgroupsTag, error) { + rows, err := q.db.Query(ctx, getTalkgroupsWithAllTags, tags) + if err != nil { + return nil, err + } + defer rows.Close() + var items []TalkgroupsTag + for rows.Next() { + var i TalkgroupsTag + if err := rows.Scan(&i.TalkgroupID, &i.Tags); err != nil { + return nil, err + } + items = append(items, i) + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} + +const getTalkgroupsWithAnyTags = `-- name: GetTalkgroupsWithAnyTags :many +SELECT talkgroup_id, tags FROM talkgroups_tags +WHERE tags @> ARRAY[$1] +` + +func (q *Queries) GetTalkgroupsWithAnyTags(ctx context.Context, tags []string) ([]TalkgroupsTag, error) { + rows, err := q.db.Query(ctx, getTalkgroupsWithAnyTags, tags) + if err != nil { + return nil, err + } + defer rows.Close() + var items []TalkgroupsTag + for rows.Next() { + var i TalkgroupsTag + if err := rows.Scan(&i.TalkgroupID, &i.Tags); err != nil { + return nil, err + } + items = append(items, i) + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} + +const setTalkgroupTags = `-- name: SetTalkgroupTags :exec +INSERT INTO talkgroups_tags(talkgroup_id, tags) VALUES($1, $2) +ON CONFLICT (talkgroup_id) DO UPDATE +SET tags = $2 +` + +type SetTalkgroupTagsParams struct { + TalkgroupID int32 + Tags []string +} + +func (q *Queries) SetTalkgroupTags(ctx context.Context, arg SetTalkgroupTagsParams) error { + _, err := q.db.Exec(ctx, setTalkgroupTags, arg.TalkgroupID, arg.Tags) + return err +} diff --git a/pkg/gordio/server/routes.go b/pkg/gordio/server/routes.go index d29e1ed..e6a229d 100644 --- a/pkg/gordio/server/routes.go +++ b/pkg/gordio/server/routes.go @@ -68,11 +68,11 @@ func (s *Server) routeAuth(w http.ResponseWriter, r *http.Request) { return } http.SetCookie(w, &http.Cookie{ - Name: "jwt", - Value: tok, + Name: "jwt", + Value: tok, HttpOnly: true, - Secure: true, - Domain: s.conf.Domain, + Secure: true, + Domain: s.conf.Domain, }) jr := struct { diff --git a/sql/postgres/migrations/001_initial.up.sql b/sql/postgres/migrations/001_initial.up.sql index 13af2e0..ef2ecc6 100644 --- a/sql/postgres/migrations/001_initial.up.sql +++ b/sql/postgres/migrations/001_initial.up.sql @@ -28,12 +28,19 @@ CREATE TABLE IF NOT EXISTS talkgroups( system INTEGER REFERENCES systems(id) NOT NULL, name TEXT, frequency INTEGER, - group_id INTEGER, + tgid INTEGER, auto_created BOOLEAN, metadata JSONB, PRIMARY KEY (tgid, system) ); +CREATE TABLE IF NOT EXISTS talkgroups_tags( + talkgroup_id INTEGER NOT NULL REFERENCES talkgroups(id), + tags TEXT[] NOT NULL DEFAULT '{}' +); +CREATE UNIQUE INDEX IF NOT EXISTS talkgroups_tags_id_talkgroup_id ON talkgroups_tags(talkgroup_id); +CREATE INDEX IF NOT EXISTS talkgroup_tags_id_tags ON talkgroups_tags USING GIN (tags); + CREATE TABLE IF NOT EXISTS calls( id UUID PRIMARY KEY, submitter INTEGER REFERENCES api_keys(id) ON DELETE SET NULL, diff --git a/sql/postgres/queries/talkgroups.sql b/sql/postgres/queries/talkgroups.sql new file mode 100644 index 0000000..05e245f --- /dev/null +++ b/sql/postgres/queries/talkgroups.sql @@ -0,0 +1,16 @@ +-- name: GetTalkgroupsWithAnyTags :many +SELECT * FROM talkgroups_tags +WHERE tags @> ARRAY[$1]; + +-- name: GetTalkgroupsWithAllTags :many +SELECT * FROM talkgroups_tags +WHERE tags && ARRAY[$1]; + +-- name: GetTalkgroupTags :one +SELECT tags FROM talkgroups_tags +WHERE talkgroup_id = $1; + +-- name: SetTalkgroupTags :exec +INSERT INTO talkgroups_tags(talkgroup_id, tags) VALUES($1, $2) +ON CONFLICT (talkgroup_id) DO UPDATE SET tags = $2; +