Updated sqlc setup for initial user creation.
This commit is contained in:
+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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user