Updated sqlc setup for initial user creation.
This commit is contained in:
@@ -4,8 +4,13 @@
|
||||
|
||||
package db
|
||||
|
||||
import (
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
)
|
||||
|
||||
type User struct {
|
||||
ID int64 `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Age int32 `json:"age"`
|
||||
ID int64 `json:"id"`
|
||||
Email string `json:"email"`
|
||||
Password string `json:"password"`
|
||||
CreatedAt pgtype.Timestamptz `json:"created_at"`
|
||||
}
|
||||
|
||||
@@ -10,8 +10,9 @@ import (
|
||||
|
||||
type Querier interface {
|
||||
CreateUser(ctx context.Context, arg CreateUserParams) (User, error)
|
||||
DeleteUser(ctx context.Context, id int64) error
|
||||
GetUser(ctx context.Context, id int64) (User, error)
|
||||
DeleteUser(ctx context.Context, arg DeleteUserParams) error
|
||||
GetUserByEmail(ctx context.Context, dollar_1 string) (User, error)
|
||||
GetUserById(ctx context.Context, id int64) (User, error)
|
||||
ListUsers(ctx context.Context) ([]User, error)
|
||||
UpdateUser(ctx context.Context, arg UpdateUserParams) error
|
||||
}
|
||||
|
||||
+58
-23
@@ -11,50 +11,82 @@ import (
|
||||
|
||||
const createUser = `-- name: CreateUser :one
|
||||
INSERT INTO users (
|
||||
name, age
|
||||
email, password
|
||||
) VALUES (
|
||||
$1, $2
|
||||
)
|
||||
RETURNING id, name, age
|
||||
RETURNING id, email, password, created_at
|
||||
`
|
||||
|
||||
type CreateUserParams struct {
|
||||
Name string `json:"name"`
|
||||
Age int32 `json:"age"`
|
||||
Email string `json:"email"`
|
||||
Password string `json:"password"`
|
||||
}
|
||||
|
||||
func (q *Queries) CreateUser(ctx context.Context, arg CreateUserParams) (User, error) {
|
||||
row := q.db.QueryRow(ctx, createUser, arg.Name, arg.Age)
|
||||
row := q.db.QueryRow(ctx, createUser, arg.Email, arg.Password)
|
||||
var i User
|
||||
err := row.Scan(&i.ID, &i.Name, &i.Age)
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.Email,
|
||||
&i.Password,
|
||||
&i.CreatedAt,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const deleteUser = `-- name: DeleteUser :exec
|
||||
DELETE FROM users
|
||||
WHERE id = $1
|
||||
WHERE id = $1 and email = $2
|
||||
`
|
||||
|
||||
func (q *Queries) DeleteUser(ctx context.Context, id int64) error {
|
||||
_, err := q.db.Exec(ctx, deleteUser, id)
|
||||
type DeleteUserParams struct {
|
||||
ID int64 `json:"id"`
|
||||
Email string `json:"email"`
|
||||
}
|
||||
|
||||
func (q *Queries) DeleteUser(ctx context.Context, arg DeleteUserParams) error {
|
||||
_, err := q.db.Exec(ctx, deleteUser, arg.ID, arg.Email)
|
||||
return err
|
||||
}
|
||||
|
||||
const getUser = `-- name: GetUser :one
|
||||
SELECT id, name, age FROM users
|
||||
const getUserByEmail = `-- name: GetUserByEmail :one
|
||||
SELECT id, email, password, created_at FROM users
|
||||
WHERE email = $1::text LIMIT 1
|
||||
`
|
||||
|
||||
func (q *Queries) GetUserByEmail(ctx context.Context, dollar_1 string) (User, error) {
|
||||
row := q.db.QueryRow(ctx, getUserByEmail, dollar_1)
|
||||
var i User
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.Email,
|
||||
&i.Password,
|
||||
&i.CreatedAt,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const getUserById = `-- name: GetUserById :one
|
||||
SELECT id, email, password, created_at FROM users
|
||||
WHERE id = $1 LIMIT 1
|
||||
`
|
||||
|
||||
func (q *Queries) GetUser(ctx context.Context, id int64) (User, error) {
|
||||
row := q.db.QueryRow(ctx, getUser, id)
|
||||
func (q *Queries) GetUserById(ctx context.Context, id int64) (User, error) {
|
||||
row := q.db.QueryRow(ctx, getUserById, id)
|
||||
var i User
|
||||
err := row.Scan(&i.ID, &i.Name, &i.Age)
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.Email,
|
||||
&i.Password,
|
||||
&i.CreatedAt,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const listUsers = `-- name: ListUsers :many
|
||||
SELECT id, name, age FROM users
|
||||
ORDER BY name
|
||||
SELECT id, email, password, created_at FROM users
|
||||
ORDER BY email
|
||||
`
|
||||
|
||||
func (q *Queries) ListUsers(ctx context.Context) ([]User, error) {
|
||||
@@ -66,7 +98,12 @@ func (q *Queries) ListUsers(ctx context.Context) ([]User, error) {
|
||||
var items []User
|
||||
for rows.Next() {
|
||||
var i User
|
||||
if err := rows.Scan(&i.ID, &i.Name, &i.Age); err != nil {
|
||||
if err := rows.Scan(
|
||||
&i.ID,
|
||||
&i.Email,
|
||||
&i.Password,
|
||||
&i.CreatedAt,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
@@ -79,18 +116,16 @@ func (q *Queries) ListUsers(ctx context.Context) ([]User, error) {
|
||||
|
||||
const updateUser = `-- name: UpdateUser :exec
|
||||
UPDATE users
|
||||
set name = $2,
|
||||
age = $3
|
||||
set email = $2
|
||||
WHERE id = $1
|
||||
`
|
||||
|
||||
type UpdateUserParams struct {
|
||||
ID int64 `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Age int32 `json:"age"`
|
||||
ID int64 `json:"id"`
|
||||
Email string `json:"email"`
|
||||
}
|
||||
|
||||
func (q *Queries) UpdateUser(ctx context.Context, arg UpdateUserParams) error {
|
||||
_, err := q.db.Exec(ctx, updateUser, arg.ID, arg.Name, arg.Age)
|
||||
_, err := q.db.Exec(ctx, updateUser, arg.ID, arg.Email)
|
||||
return err
|
||||
}
|
||||
|
||||
+9
-6
@@ -1,14 +1,18 @@
|
||||
-- name: GetUser :one
|
||||
-- name: GetUserByEmail :one
|
||||
SELECT id, email, password, created_at FROM users
|
||||
WHERE email = $1::text LIMIT 1;
|
||||
|
||||
-- name: GetUserById :one
|
||||
SELECT * FROM users
|
||||
WHERE id = $1 LIMIT 1;
|
||||
|
||||
-- name: ListUsers :many
|
||||
SELECT * FROM users
|
||||
ORDER BY name;
|
||||
ORDER BY email;
|
||||
|
||||
-- name: CreateUser :one
|
||||
INSERT INTO users (
|
||||
name, age
|
||||
email, password
|
||||
) VALUES (
|
||||
$1, $2
|
||||
)
|
||||
@@ -16,10 +20,9 @@ RETURNING *;
|
||||
|
||||
-- name: UpdateUser :exec
|
||||
UPDATE users
|
||||
set name = $2,
|
||||
age = $3
|
||||
set email = $2
|
||||
WHERE id = $1;
|
||||
|
||||
-- name: DeleteUser :exec
|
||||
DELETE FROM users
|
||||
WHERE id = $1;
|
||||
WHERE id = $1 and email = $2;
|
||||
|
||||
+4
-3
@@ -1,6 +1,7 @@
|
||||
CREATE TABLE users (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
name text NOT NULL,
|
||||
age int NOT NULL
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
email TEXT NOT NULL UNIQUE,
|
||||
password TEXT NOT NULL,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
|
||||
);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user