Updated sqlc setup for initial user creation.

This commit is contained in:
2026-04-25 19:37:17 +02:00
parent a8862721cd
commit 8ec981c860
5 changed files with 82 additions and 37 deletions
+8 -3
View File
@@ -4,8 +4,13 @@
package db package db
import (
"github.com/jackc/pgx/v5/pgtype"
)
type User struct { type User struct {
ID int64 `json:"id"` ID int64 `json:"id"`
Name string `json:"name"` Email string `json:"email"`
Age int32 `json:"age"` Password string `json:"password"`
CreatedAt pgtype.Timestamptz `json:"created_at"`
} }
+3 -2
View File
@@ -10,8 +10,9 @@ import (
type Querier interface { type Querier interface {
CreateUser(ctx context.Context, arg CreateUserParams) (User, error) CreateUser(ctx context.Context, arg CreateUserParams) (User, error)
DeleteUser(ctx context.Context, id int64) error DeleteUser(ctx context.Context, arg DeleteUserParams) error
GetUser(ctx context.Context, id int64) (User, error) GetUserByEmail(ctx context.Context, dollar_1 string) (User, error)
GetUserById(ctx context.Context, id int64) (User, error)
ListUsers(ctx context.Context) ([]User, error) ListUsers(ctx context.Context) ([]User, error)
UpdateUser(ctx context.Context, arg UpdateUserParams) error UpdateUser(ctx context.Context, arg UpdateUserParams) error
} }
+58 -23
View File
@@ -11,50 +11,82 @@ import (
const createUser = `-- name: CreateUser :one const createUser = `-- name: CreateUser :one
INSERT INTO users ( INSERT INTO users (
name, age email, password
) VALUES ( ) VALUES (
$1, $2 $1, $2
) )
RETURNING id, name, age RETURNING id, email, password, created_at
` `
type CreateUserParams struct { type CreateUserParams struct {
Name string `json:"name"` Email string `json:"email"`
Age int32 `json:"age"` Password string `json:"password"`
} }
func (q *Queries) CreateUser(ctx context.Context, arg CreateUserParams) (User, error) { 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 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 return i, err
} }
const deleteUser = `-- name: DeleteUser :exec const deleteUser = `-- name: DeleteUser :exec
DELETE FROM users DELETE FROM users
WHERE id = $1 WHERE id = $1 and email = $2
` `
func (q *Queries) DeleteUser(ctx context.Context, id int64) error { type DeleteUserParams struct {
_, err := q.db.Exec(ctx, deleteUser, id) 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 return err
} }
const getUser = `-- name: GetUser :one const getUserByEmail = `-- name: GetUserByEmail :one
SELECT id, name, age FROM users 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 WHERE id = $1 LIMIT 1
` `
func (q *Queries) GetUser(ctx context.Context, id int64) (User, error) { func (q *Queries) GetUserById(ctx context.Context, id int64) (User, error) {
row := q.db.QueryRow(ctx, getUser, id) row := q.db.QueryRow(ctx, getUserById, id)
var i User 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 return i, err
} }
const listUsers = `-- name: ListUsers :many const listUsers = `-- name: ListUsers :many
SELECT id, name, age FROM users SELECT id, email, password, created_at FROM users
ORDER BY name ORDER BY email
` `
func (q *Queries) ListUsers(ctx context.Context) ([]User, error) { 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 var items []User
for rows.Next() { for rows.Next() {
var i User 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 return nil, err
} }
items = append(items, i) items = append(items, i)
@@ -79,18 +116,16 @@ func (q *Queries) ListUsers(ctx context.Context) ([]User, error) {
const updateUser = `-- name: UpdateUser :exec const updateUser = `-- name: UpdateUser :exec
UPDATE users UPDATE users
set name = $2, set email = $2
age = $3
WHERE id = $1 WHERE id = $1
` `
type UpdateUserParams struct { type UpdateUserParams struct {
ID int64 `json:"id"` ID int64 `json:"id"`
Name string `json:"name"` Email string `json:"email"`
Age int32 `json:"age"`
} }
func (q *Queries) UpdateUser(ctx context.Context, arg UpdateUserParams) error { 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 return err
} }
+9 -6
View File
@@ -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 SELECT * FROM users
WHERE id = $1 LIMIT 1; WHERE id = $1 LIMIT 1;
-- name: ListUsers :many -- name: ListUsers :many
SELECT * FROM users SELECT * FROM users
ORDER BY name; ORDER BY email;
-- name: CreateUser :one -- name: CreateUser :one
INSERT INTO users ( INSERT INTO users (
name, age email, password
) VALUES ( ) VALUES (
$1, $2 $1, $2
) )
@@ -16,10 +20,9 @@ RETURNING *;
-- name: UpdateUser :exec -- name: UpdateUser :exec
UPDATE users UPDATE users
set name = $2, set email = $2
age = $3
WHERE id = $1; WHERE id = $1;
-- name: DeleteUser :exec -- name: DeleteUser :exec
DELETE FROM users DELETE FROM users
WHERE id = $1; WHERE id = $1 and email = $2;
+4 -3
View File
@@ -1,6 +1,7 @@
CREATE TABLE users ( CREATE TABLE users (
id BIGSERIAL PRIMARY KEY, id BIGSERIAL PRIMARY KEY,
name text NOT NULL, email TEXT NOT NULL UNIQUE,
age int NOT NULL password TEXT NOT NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
); );