rework for sqlc
This commit is contained in:
parent
7bd1f5bf9a
commit
05588986c0
12 changed files with 896 additions and 36 deletions
|
@ -91,7 +91,7 @@ func New(config *Config) *App {
|
|||
func initialize(app *App) {
|
||||
var err error
|
||||
|
||||
app.database, err = database.New(app.config.DatabaseDSN)
|
||||
app.database, err = database.Init(app.config.DatabaseDSN)
|
||||
if err != nil {
|
||||
// Since we are not sending any data yet, any error occuring here
|
||||
// is likely result of a missing driver or wrong parameters.
|
||||
|
|
93
internal/database/confirmation.sql.go
Normal file
93
internal/database/confirmation.sql.go
Normal file
|
@ -0,0 +1,93 @@
|
|||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// source: confirmation.sql
|
||||
|
||||
package database
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
)
|
||||
|
||||
const createConfirmation = `-- name: CreateConfirmation :one
|
||||
INSERT INTO "public"."confirmation" (
|
||||
"email_address", "user_id", "selector", "verifier", "expires_at"
|
||||
) VALUES (
|
||||
$1, $2, $3, $4, $5
|
||||
) RETURNING email_address, user_id, selector, verifier, expires_at
|
||||
`
|
||||
|
||||
type CreateConfirmationParams struct {
|
||||
EmailAddress string `json:"email_address"`
|
||||
UserID int64 `json:"user_id"`
|
||||
Selector string `json:"selector"`
|
||||
Verifier []byte `json:"verifier"`
|
||||
ExpiresAt time.Time `json:"expires_at"`
|
||||
}
|
||||
|
||||
func (q *Queries) CreateConfirmation(ctx context.Context, arg CreateConfirmationParams) (Confirmation, error) {
|
||||
row := q.db.QueryRowContext(ctx, createConfirmation,
|
||||
arg.EmailAddress,
|
||||
arg.UserID,
|
||||
arg.Selector,
|
||||
arg.Verifier,
|
||||
arg.ExpiresAt,
|
||||
)
|
||||
var i Confirmation
|
||||
err := row.Scan(
|
||||
&i.EmailAddress,
|
||||
&i.UserID,
|
||||
&i.Selector,
|
||||
&i.Verifier,
|
||||
&i.ExpiresAt,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const destroyConfirmation = `-- name: DestroyConfirmation :exec
|
||||
DELETE FROM "public"."confirmation" WHERE "selector" = $1
|
||||
`
|
||||
|
||||
func (q *Queries) DestroyConfirmation(ctx context.Context, selector string) error {
|
||||
_, err := q.db.ExecContext(ctx, destroyConfirmation, selector)
|
||||
return err
|
||||
}
|
||||
|
||||
const getConfirmationBySelector = `-- name: GetConfirmationBySelector :one
|
||||
SELECT
|
||||
"email_address", "user_id", "selector", "verifier", "expires_at"
|
||||
FROM "public"."confirmation"
|
||||
WHERE "selector" = $1
|
||||
`
|
||||
|
||||
func (q *Queries) GetConfirmationBySelector(ctx context.Context, selector string) (Confirmation, error) {
|
||||
row := q.db.QueryRowContext(ctx, getConfirmationBySelector, selector)
|
||||
var i Confirmation
|
||||
err := row.Scan(
|
||||
&i.EmailAddress,
|
||||
&i.UserID,
|
||||
&i.Selector,
|
||||
&i.Verifier,
|
||||
&i.ExpiresAt,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const getConfirmationByUserID = `-- name: GetConfirmationByUserID :one
|
||||
SELECT
|
||||
"email_address", "user_id", "selector", "verifier", "expires_at"
|
||||
FROM "public"."confirmation"
|
||||
WHERE "user_id" = $1
|
||||
`
|
||||
|
||||
func (q *Queries) GetConfirmationByUserID(ctx context.Context, userID int64) (Confirmation, error) {
|
||||
row := q.db.QueryRowContext(ctx, getConfirmationByUserID, userID)
|
||||
var i Confirmation
|
||||
err := row.Scan(
|
||||
&i.EmailAddress,
|
||||
&i.UserID,
|
||||
&i.Selector,
|
||||
&i.Verifier,
|
||||
&i.ExpiresAt,
|
||||
)
|
||||
return i, err
|
||||
}
|
29
internal/database/db.go
Normal file
29
internal/database/db.go
Normal file
|
@ -0,0 +1,29 @@
|
|||
// Code generated by sqlc. DO NOT EDIT.
|
||||
|
||||
package database
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
)
|
||||
|
||||
type DBTX interface {
|
||||
ExecContext(context.Context, string, ...interface{}) (sql.Result, error)
|
||||
PrepareContext(context.Context, string) (*sql.Stmt, error)
|
||||
QueryContext(context.Context, string, ...interface{}) (*sql.Rows, error)
|
||||
QueryRowContext(context.Context, string, ...interface{}) *sql.Row
|
||||
}
|
||||
|
||||
func New(db DBTX) *Queries {
|
||||
return &Queries{db: db}
|
||||
}
|
||||
|
||||
type Queries struct {
|
||||
db DBTX
|
||||
}
|
||||
|
||||
func (q *Queries) WithTx(tx *sql.Tx) *Queries {
|
||||
return &Queries{
|
||||
db: tx,
|
||||
}
|
||||
}
|
|
@ -16,8 +16,8 @@ import (
|
|||
"github.com/lib/pq"
|
||||
)
|
||||
|
||||
// New initializes a new postgres database connection pool
|
||||
func New(dsn string) (*sqlx.DB, error) {
|
||||
// Init initializes a new postgres database connection pool
|
||||
func Init(dsn string) (*sqlx.DB, error) {
|
||||
conn, err := sqlx.Open("postgres", dsn)
|
||||
return conn, err
|
||||
}
|
||||
|
|
86
internal/database/email.sql.go
Normal file
86
internal/database/email.sql.go
Normal file
|
@ -0,0 +1,86 @@
|
|||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// source: email.sql
|
||||
|
||||
package database
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
)
|
||||
|
||||
const createEmail = `-- name: CreateEmail :one
|
||||
INSERT INTO "email" (
|
||||
"address", "user_id", "created_at"
|
||||
) VALUES (
|
||||
$1, $2, $3
|
||||
) RETURNING address, user_id, created_at
|
||||
`
|
||||
|
||||
type CreateEmailParams struct {
|
||||
Address string `json:"address"`
|
||||
UserID int64 `json:"user_id"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
}
|
||||
|
||||
func (q *Queries) CreateEmail(ctx context.Context, arg CreateEmailParams) (Email, error) {
|
||||
row := q.db.QueryRowContext(ctx, createEmail, arg.Address, arg.UserID, arg.CreatedAt)
|
||||
var i Email
|
||||
err := row.Scan(&i.Address, &i.UserID, &i.CreatedAt)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const destroyEmail = `-- name: DestroyEmail :exec
|
||||
DELETE FROM "email" WHERE "address" = $1
|
||||
`
|
||||
|
||||
func (q *Queries) DestroyEmail(ctx context.Context, address string) error {
|
||||
_, err := q.db.ExecContext(ctx, destroyEmail, address)
|
||||
return err
|
||||
}
|
||||
|
||||
const getEmailByAddress = `-- name: GetEmailByAddress :one
|
||||
SELECT
|
||||
"address", "user_id", "created_at"
|
||||
FROM "public"."email"
|
||||
WHERE "address" = $1
|
||||
`
|
||||
|
||||
func (q *Queries) GetEmailByAddress(ctx context.Context, address string) (Email, error) {
|
||||
row := q.db.QueryRowContext(ctx, getEmailByAddress, address)
|
||||
var i Email
|
||||
err := row.Scan(&i.Address, &i.UserID, &i.CreatedAt)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const getEmailByUserID = `-- name: GetEmailByUserID :one
|
||||
SELECT
|
||||
"address", "user_id", "created_at"
|
||||
FROM "public"."email"
|
||||
WHERE "user_id" = $1
|
||||
`
|
||||
|
||||
func (q *Queries) GetEmailByUserID(ctx context.Context, userID int64) (Email, error) {
|
||||
row := q.db.QueryRowContext(ctx, getEmailByUserID, userID)
|
||||
var i Email
|
||||
err := row.Scan(&i.Address, &i.UserID, &i.CreatedAt)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const updateEmail = `-- name: UpdateEmail :exec
|
||||
UPDATE "email" SET (
|
||||
"user_id", "created_at"
|
||||
) = (
|
||||
$1, $2
|
||||
) WHERE "address" = $3
|
||||
`
|
||||
|
||||
type UpdateEmailParams struct {
|
||||
UserID int64 `json:"user_id"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
Address string `json:"address"`
|
||||
}
|
||||
|
||||
func (q *Queries) UpdateEmail(ctx context.Context, arg UpdateEmailParams) error {
|
||||
_, err := q.db.ExecContext(ctx, updateEmail, arg.UserID, arg.CreatedAt, arg.Address)
|
||||
return err
|
||||
}
|
49
internal/database/models.go
Normal file
49
internal/database/models.go
Normal file
|
@ -0,0 +1,49 @@
|
|||
// Code generated by sqlc. DO NOT EDIT.
|
||||
|
||||
package database
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Confirmation struct {
|
||||
EmailAddress string `json:"email_address"`
|
||||
UserID int64 `json:"user_id"`
|
||||
Selector string `json:"selector"`
|
||||
Verifier []byte `json:"verifier"`
|
||||
ExpiresAt time.Time `json:"expires_at"`
|
||||
}
|
||||
|
||||
type Email struct {
|
||||
Address string `json:"address"`
|
||||
UserID int64 `json:"user_id"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
}
|
||||
|
||||
type ExternalAuth struct {
|
||||
ID int64 `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Type string `json:"type"`
|
||||
Config json.RawMessage `json:"config"`
|
||||
}
|
||||
|
||||
type ExternalUser struct {
|
||||
ExternalAuthID int64 `json:"external_auth_id"`
|
||||
ForeignID string `json:"foreign_id"`
|
||||
UserID int64 `json:"user_id"`
|
||||
}
|
||||
|
||||
type Reset struct {
|
||||
UserID int64 `json:"user_id"`
|
||||
Selector string `json:"selector"`
|
||||
Verifier []byte `json:"verifier"`
|
||||
ExpiresAt time.Time `json:"expires_at"`
|
||||
}
|
||||
|
||||
type User struct {
|
||||
ID int64 `json:"id"`
|
||||
IsAdmin bool `json:"is_admin"`
|
||||
Password []byte `json:"password"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
}
|
28
internal/database/querier.go
Normal file
28
internal/database/querier.go
Normal file
|
@ -0,0 +1,28 @@
|
|||
// Code generated by sqlc. DO NOT EDIT.
|
||||
|
||||
package database
|
||||
|
||||
import (
|
||||
"context"
|
||||
)
|
||||
|
||||
type Querier interface {
|
||||
CreateConfirmation(ctx context.Context, arg CreateConfirmationParams) (Confirmation, error)
|
||||
CreateEmail(ctx context.Context, arg CreateEmailParams) (Email, error)
|
||||
CreateReset(ctx context.Context, arg CreateResetParams) (Reset, error)
|
||||
CreateUser(ctx context.Context, arg CreateUserParams) (User, error)
|
||||
DestroyConfirmation(ctx context.Context, selector string) error
|
||||
DestroyEmail(ctx context.Context, address string) error
|
||||
DestroyReset(ctx context.Context, selector string) error
|
||||
GetConfirmationBySelector(ctx context.Context, selector string) (Confirmation, error)
|
||||
GetConfirmationByUserID(ctx context.Context, userID int64) (Confirmation, error)
|
||||
GetEmailByAddress(ctx context.Context, address string) (Email, error)
|
||||
GetEmailByUserID(ctx context.Context, userID int64) (Email, error)
|
||||
GetResetBySelector(ctx context.Context, selector string) (Reset, error)
|
||||
GetResetByUserID(ctx context.Context, userID int64) (Reset, error)
|
||||
GetUserByID(ctx context.Context, id int64) (User, error)
|
||||
UpdateEmail(ctx context.Context, arg UpdateEmailParams) error
|
||||
UpdateUser(ctx context.Context, arg UpdateUserParams) error
|
||||
}
|
||||
|
||||
var _ Querier = (*Queries)(nil)
|
88
internal/database/reset.sql.go
Normal file
88
internal/database/reset.sql.go
Normal file
|
@ -0,0 +1,88 @@
|
|||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// source: reset.sql
|
||||
|
||||
package database
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
)
|
||||
|
||||
const createReset = `-- name: CreateReset :one
|
||||
INSERT INTO "reset" (
|
||||
"user_id", "selector", "verifier", "expires_at"
|
||||
) VALUES (
|
||||
$1, $2, $3, $4
|
||||
) RETURNING user_id, selector, verifier, expires_at
|
||||
`
|
||||
|
||||
type CreateResetParams struct {
|
||||
UserID int64 `json:"user_id"`
|
||||
Selector string `json:"selector"`
|
||||
Verifier []byte `json:"verifier"`
|
||||
ExpiresAt time.Time `json:"expires_at"`
|
||||
}
|
||||
|
||||
func (q *Queries) CreateReset(ctx context.Context, arg CreateResetParams) (Reset, error) {
|
||||
row := q.db.QueryRowContext(ctx, createReset,
|
||||
arg.UserID,
|
||||
arg.Selector,
|
||||
arg.Verifier,
|
||||
arg.ExpiresAt,
|
||||
)
|
||||
var i Reset
|
||||
err := row.Scan(
|
||||
&i.UserID,
|
||||
&i.Selector,
|
||||
&i.Verifier,
|
||||
&i.ExpiresAt,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const destroyReset = `-- name: DestroyReset :exec
|
||||
DELETE FROM "reset" WHERE "selector" = $1
|
||||
`
|
||||
|
||||
func (q *Queries) DestroyReset(ctx context.Context, selector string) error {
|
||||
_, err := q.db.ExecContext(ctx, destroyReset, selector)
|
||||
return err
|
||||
}
|
||||
|
||||
const getResetBySelector = `-- name: GetResetBySelector :one
|
||||
SELECT
|
||||
"user_id", "selector", "verifier", "expires_at"
|
||||
FROM "reset"
|
||||
WHERE "selector" = $1
|
||||
`
|
||||
|
||||
func (q *Queries) GetResetBySelector(ctx context.Context, selector string) (Reset, error) {
|
||||
row := q.db.QueryRowContext(ctx, getResetBySelector, selector)
|
||||
var i Reset
|
||||
err := row.Scan(
|
||||
&i.UserID,
|
||||
&i.Selector,
|
||||
&i.Verifier,
|
||||
&i.ExpiresAt,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const getResetByUserID = `-- name: GetResetByUserID :one
|
||||
SELECT
|
||||
"user_id", "selector", "verifier", "expires_at"
|
||||
FROM "reset"
|
||||
WHERE "user_id" = $1
|
||||
`
|
||||
|
||||
func (q *Queries) GetResetByUserID(ctx context.Context, userID int64) (Reset, error) {
|
||||
row := q.db.QueryRowContext(ctx, getResetByUserID, userID)
|
||||
var i Reset
|
||||
err := row.Scan(
|
||||
&i.UserID,
|
||||
&i.Selector,
|
||||
&i.Verifier,
|
||||
&i.ExpiresAt,
|
||||
)
|
||||
return i, err
|
||||
}
|
79
internal/database/user.sql.go
Normal file
79
internal/database/user.sql.go
Normal file
|
@ -0,0 +1,79 @@
|
|||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// source: user.sql
|
||||
|
||||
package database
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
)
|
||||
|
||||
const createUser = `-- name: CreateUser :one
|
||||
INSERT INTO "user" (
|
||||
"is_admin", "password", "created_at"
|
||||
) VALUES (
|
||||
$1, $2, $3
|
||||
) RETURNING id, is_admin, password, created_at
|
||||
`
|
||||
|
||||
type CreateUserParams struct {
|
||||
IsAdmin bool `json:"is_admin"`
|
||||
Password []byte `json:"password"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
}
|
||||
|
||||
func (q *Queries) CreateUser(ctx context.Context, arg CreateUserParams) (User, error) {
|
||||
row := q.db.QueryRowContext(ctx, createUser, arg.IsAdmin, arg.Password, arg.CreatedAt)
|
||||
var i User
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.IsAdmin,
|
||||
&i.Password,
|
||||
&i.CreatedAt,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const getUserByID = `-- name: GetUserByID :one
|
||||
SELECT
|
||||
"id", "is_admin", "password", "created_at"
|
||||
FROM "user"
|
||||
WHERE "id" = $1
|
||||
`
|
||||
|
||||
func (q *Queries) GetUserByID(ctx context.Context, id int64) (User, error) {
|
||||
row := q.db.QueryRowContext(ctx, getUserByID, id)
|
||||
var i User
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.IsAdmin,
|
||||
&i.Password,
|
||||
&i.CreatedAt,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const updateUser = `-- name: UpdateUser :exec
|
||||
UPDATE "user" SET (
|
||||
"is_admin", "password", "created_at"
|
||||
) = (
|
||||
$1, $2, $3
|
||||
) WHERE "id" = $4
|
||||
`
|
||||
|
||||
type UpdateUserParams struct {
|
||||
IsAdmin bool `json:"is_admin"`
|
||||
Password []byte `json:"password"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
ID int64 `json:"id"`
|
||||
}
|
||||
|
||||
func (q *Queries) UpdateUser(ctx context.Context, arg UpdateUserParams) error {
|
||||
_, err := q.db.ExecContext(ctx, updateUser,
|
||||
arg.IsAdmin,
|
||||
arg.Password,
|
||||
arg.CreatedAt,
|
||||
arg.ID,
|
||||
)
|
||||
return err
|
||||
}
|
|
@ -1,6 +1,7 @@
|
|||
package web
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
|
@ -48,12 +49,13 @@ func NewLDAPAuthenticator(lc *ldap.Conn) func(user, pass string) (User, error) {
|
|||
func NewSQLAuthenticator(db *sqlx.DB) func(user, pass string) (User, error) {
|
||||
return func(user, pass string) (User, error) {
|
||||
// Fetch email used for login
|
||||
email, err := database.EmailByAddress(db, user)
|
||||
q := database.New(db)
|
||||
email, err := q.GetEmailByAddress(context.TODO(), user)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
row, err := email.User(db)
|
||||
row, err := q.GetUserByID(context.TODO(), email.UserID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -64,7 +66,7 @@ func NewSQLAuthenticator(db *sqlx.DB) func(user, pass string) (User, error) {
|
|||
return nil, err
|
||||
}
|
||||
|
||||
u := UserRow{row}
|
||||
u := UserRow{&row}
|
||||
|
||||
return u, nil
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue