Use uuid.UUID everywhere and have postgres generate ids

This commit is contained in:
2021-11-29 21:49:37 +00:00
parent 5e8a98872f
commit 85ef7557c1
19 changed files with 199 additions and 93 deletions

View File

@ -5,17 +5,19 @@ package postgres
import (
"context"
"github.com/google/uuid"
)
const createUser = `-- name: CreateUser :one
INSERT INTO users
(id, email, name, password)
VALUES ($1, $2, $3, $4)
RETURNING id, email, name, password
RETURNING email, name, password, id
`
type CreateUserParams struct {
ID string
ID uuid.UUID
Email string
Name string
Password string
@ -30,33 +32,33 @@ func (q *Queries) CreateUser(ctx context.Context, arg CreateUserParams) (User, e
)
var i User
err := row.Scan(
&i.ID,
&i.Email,
&i.Name,
&i.Password,
&i.ID,
)
return i, err
}
const getUser = `-- name: GetUser :one
SELECT id, email, name, password FROM users
SELECT email, name, password, id FROM users
WHERE id = $1
`
func (q *Queries) GetUser(ctx context.Context, id string) (User, error) {
func (q *Queries) GetUser(ctx context.Context, id uuid.UUID) (User, error) {
row := q.db.QueryRowContext(ctx, getUser, id)
var i User
err := row.Scan(
&i.ID,
&i.Email,
&i.Name,
&i.Password,
&i.ID,
)
return i, err
}
const getUserByUsername = `-- name: GetUserByUsername :one
SELECT id, email, name, password FROM users
SELECT email, name, password, id FROM users
WHERE email = $1
`
@ -64,10 +66,10 @@ func (q *Queries) GetUserByUsername(ctx context.Context, email string) (User, er
row := q.db.QueryRowContext(ctx, getUserByUsername, email)
var i User
err := row.Scan(
&i.ID,
&i.Email,
&i.Name,
&i.Password,
&i.ID,
)
return i, err
}