From 2db6cae67e3cc628ffbf1e7a17e398d6515c0f96 Mon Sep 17 00:00:00 2001 From: Daniel Ponte Date: Mon, 15 Jul 2024 19:03:48 -0400 Subject: [PATCH] clean up SQL --- pkg/gordio/config/config.go | 1 + pkg/gordio/database/models.go | 36 ++++++++++++- .../database/{query.sql.go => users.sql.go} | 44 ++++++++++++++- pkg/gordio/server/routes.go | 7 +++ sql/postgres/migrations/001_initial.up.sql | 54 +++++++++++++++++-- sql/postgres/{query.sql => queries/users.sql} | 10 ++++ sql/sqlc.yaml | 2 +- 7 files changed, 147 insertions(+), 7 deletions(-) rename pkg/gordio/database/{query.sql.go => users.sql.go} (68%) rename sql/postgres/{query.sql => queries/users.sql} (59%) diff --git a/pkg/gordio/config/config.go b/pkg/gordio/config/config.go index 074f39c..e7ef83a 100644 --- a/pkg/gordio/config/config.go +++ b/pkg/gordio/config/config.go @@ -10,6 +10,7 @@ type Config struct { JWTSecret string `yaml:"jwtsecret"` Listen string `yaml:"listen"` Public bool `yaml:"public"` + Domain string `yaml:"domain"` } type DB struct { diff --git a/pkg/gordio/database/models.go b/pkg/gordio/database/models.go index 95cfb8e..724651d 100644 --- a/pkg/gordio/database/models.go +++ b/pkg/gordio/database/models.go @@ -11,11 +11,43 @@ import ( type Apikey struct { ID int32 Owner pgtype.Int4 - Apikey string + ApiKey string } type Call struct { - ID pgtype.UUID + ID pgtype.UUID + Submitter int32 + System int32 + Talkgroup int32 + Date pgtype.Timestamp + AudioName pgtype.Text + AudioBlob []byte + AudioType pgtype.Text + AudioUrl pgtype.Text + Frequency pgtype.Int4 + Frequencies []byte + Patches []byte + TgLabel pgtype.Text + Source pgtype.Text +} + +type Setting struct { + Name string + UpdatedBy pgtype.Int4 + Value []byte +} + +type System struct { + ID int32 + Name string +} + +type Talkgroup struct { + ID int32 + System int32 + Name pgtype.Text + Frequency pgtype.Int4 + GroupID pgtype.Int4 } type User struct { diff --git a/pkg/gordio/database/query.sql.go b/pkg/gordio/database/users.sql.go similarity index 68% rename from pkg/gordio/database/query.sql.go rename to pkg/gordio/database/users.sql.go index 4f8b3b5..1cb4e86 100644 --- a/pkg/gordio/database/query.sql.go +++ b/pkg/gordio/database/users.sql.go @@ -1,7 +1,7 @@ // Code generated by sqlc. DO NOT EDIT. // versions: // sqlc v1.26.0 -// source: query.sql +// source: users.sql package database @@ -47,6 +47,15 @@ func (q *Queries) CreateUser(ctx context.Context, arg CreateUserParams) (User, e return i, err } +const deleteUser = `-- name: DeleteUser :exec +DELETE FROM users WHERE username = $1 +` + +func (q *Queries) DeleteUser(ctx context.Context, username string) error { + _, err := q.db.Exec(ctx, deleteUser, username) + return err +} + const getUserByID = `-- name: GetUserByID :one SELECT id, username, password, email, is_admin, prefs FROM users WHERE id = $1 LIMIT 1 @@ -66,6 +75,25 @@ func (q *Queries) GetUserByID(ctx context.Context, id int32) (User, error) { return i, err } +const getUserByUID = `-- name: GetUserByUID :one +SELECT id, username, password, email, is_admin, prefs FROM users +WHERE id = $1 LIMIT 1 +` + +func (q *Queries) GetUserByUID(ctx context.Context, id int32) (User, error) { + row := q.db.QueryRow(ctx, getUserByUID, id) + var i User + err := row.Scan( + &i.ID, + &i.Username, + &i.Password, + &i.Email, + &i.IsAdmin, + &i.Prefs, + ) + return i, err +} + const getUserByUsername = `-- name: GetUserByUsername :one SELECT id, username, password, email, is_admin, prefs FROM users WHERE username = $1 LIMIT 1 @@ -115,3 +143,17 @@ func (q *Queries) GetUsers(ctx context.Context) ([]User, error) { } return items, nil } + +const updatePassword = `-- name: UpdatePassword :exec +UPDATE users SET password = $2 WHERE username = $1 +` + +type UpdatePasswordParams struct { + Username string + Password string +} + +func (q *Queries) UpdatePassword(ctx context.Context, arg UpdatePasswordParams) error { + _, err := q.db.Exec(ctx, updatePassword, arg.Username, arg.Password) + return err +} diff --git a/pkg/gordio/server/routes.go b/pkg/gordio/server/routes.go index 3acb3ff..d29e1ed 100644 --- a/pkg/gordio/server/routes.go +++ b/pkg/gordio/server/routes.go @@ -67,6 +67,13 @@ func (s *Server) routeAuth(w http.ResponseWriter, r *http.Request) { http.Error(w, err.Error(), http.StatusUnauthorized) return } + http.SetCookie(w, &http.Cookie{ + Name: "jwt", + Value: tok, + HttpOnly: true, + Secure: true, + Domain: s.conf.Domain, + }) jr := struct { JWT string `json:"jwt"` diff --git a/sql/postgres/migrations/001_initial.up.sql b/sql/postgres/migrations/001_initial.up.sql index 05b352b..7863f45 100644 --- a/sql/postgres/migrations/001_initial.up.sql +++ b/sql/postgres/migrations/001_initial.up.sql @@ -1,8 +1,56 @@ CREATE TABLE IF NOT EXISTS users( id SERIAL PRIMARY KEY, username VARCHAR (255) UNIQUE NOT NULL, - password VARCHAR(255) NOT NULL, - email VARCHAR(255) NOT NULL, - is_admin BOOLEAN + password TEXT NOT NULL, + email TEXT NOT NULL, + is_admin BOOLEAN, + prefs JSONB ); +CREATE INDEX IF NOT EXISTS users_username ON users(username); + +CREATE TABLE IF NOT EXISTS apikeys( + id SERIAL PRIMARY KEY, + owner INTEGER REFERENCES users(id), + api_key TEXT UNIQUE NOT NULL +); + +CREATE TABLE IF NOT EXISTS systems( + id INTEGER PRIMARY KEY, + name TEXT NOT NULL +); + +CREATE TABLE IF NOT EXISTS talkgroups( + id INTEGER NOT NULL, + system INTEGER REFERENCES systems(id) NOT NULL, + name TEXT, + frequency INTEGER, + group_id INTEGER, + PRIMARY KEY (tgid, system) +); + +CREATE TABLE IF NOT EXISTS calls( + id UUID PRIMARY KEY, + submitter INTEGER REFERENCES apikeys(id) NOT NULL, + system INTEGER NOT NULL, + talkgroup INTEGER NOT NULL, + date TIMESTAMP NOT NULL, + audio_name TEXT, + audio_blob BYTEA, + audio_type TEXT, + audio_url TEXT, + frequency INTEGER, + frequencies JSONB, + patches JSONB, + tg_label TEXT, + source TEXT, + FOREIGN KEY (system, talkgroup) REFERENCES talkgroups(system, id) +); + +CREATE INDEX IF NOT EXISTS calls_date_tg ON calls(talkgroup, date); + +CREATE TABLE IF NOT EXISTS settings( + name TEXT PRIMARY KEY, + updated_by INTEGER REFERENCES users(id), + value JSONB +); diff --git a/sql/postgres/query.sql b/sql/postgres/queries/users.sql similarity index 59% rename from sql/postgres/query.sql rename to sql/postgres/queries/users.sql index 15323c2..dafd9ad 100644 --- a/sql/postgres/query.sql +++ b/sql/postgres/queries/users.sql @@ -6,6 +6,10 @@ WHERE id = $1 LIMIT 1; SELECT * FROM users WHERE username = $1 LIMIT 1; +-- name: GetUserByUID :one +SELECT * FROM users +WHERE id = $1 LIMIT 1; + -- name: GetUsers :many SELECT * FROM users; @@ -17,3 +21,9 @@ INSERT INTO users ( is_admin ) VALUES ($1, $2, $3, $4) RETURNING *; + +-- name: DeleteUser :exec +DELETE FROM users WHERE username = $1; + +-- name: UpdatePassword :exec +UPDATE users SET password = $2 WHERE username = $1; diff --git a/sql/sqlc.yaml b/sql/sqlc.yaml index 1e347b3..185b5b5 100644 --- a/sql/sqlc.yaml +++ b/sql/sqlc.yaml @@ -1,7 +1,7 @@ version: "2" sql: - engine: "postgresql" - queries: "postgres/query.sql" + queries: "postgres/queries" schema: "postgres/migrations" gen: go: