开发...

This commit is contained in:
kenneth
2023-11-29 09:46:09 +00:00
parent 6b8eafe5f5
commit d07d13151a
31 changed files with 920 additions and 665 deletions

View File

@@ -12,6 +12,7 @@ type Querier interface {
CreateUser(ctx context.Context, arg CreateUserParams) (User, error)
DeleteUser(ctx context.Context, id string) error
GetUser(ctx context.Context, id string) (User, error)
GetUserByEmail(ctx context.Context, email string) (User, error)
GetUserByName(ctx context.Context, username string) (User, error)
ListUsers(ctx context.Context, arg ListUsersParams) ([]User, error)
UpdateUser(ctx context.Context, arg UpdateUserParams) (User, error)

View File

@@ -1,8 +1,8 @@
-- name: CreateUser :one
INSERT INTO users (
username, hashed_password, email
id, username, hashed_password, email
) VALUES (
$1, $2, $3
$1, $2, $3, $4
)
RETURNING *;
@@ -25,6 +25,10 @@ WHERE id = $1 LIMIT 1;
SELECT * FROM users
WHERE username = $1 LIMIT 1;
-- name: GetUserByEmail :one
SELECT * FROM users
WHERE email = $1 LIMIT 1;
-- name: ListUsers :many
SELECT * FROM users
ORDER BY id

View File

@@ -1,7 +1,7 @@
CREATE TABLE "users" (
"id" varchar NOT NULL PRIMARY KEY,
"username" varchar NOT NULL,
"username" varchar NOT NULL UNIQUE,
"hashed_password" varchar NOT NULL,
"email" varchar NOT NULL,
"email" varchar NOT NULL UNIQUE,
"created_at" timestamptz NOT NULL DEFAULT (now())
);

View File

@@ -11,21 +11,27 @@ import (
const createUser = `-- name: CreateUser :one
INSERT INTO users (
username, hashed_password, email
id, username, hashed_password, email
) VALUES (
$1, $2, $3
$1, $2, $3, $4
)
RETURNING id, username, hashed_password, email, created_at
`
type CreateUserParams struct {
ID string `json:"id"`
Username string `json:"username"`
HashedPassword string `json:"hashed_password"`
Email string `json:"email"`
}
func (q *Queries) CreateUser(ctx context.Context, arg CreateUserParams) (User, error) {
row := q.db.QueryRowContext(ctx, createUser, arg.Username, arg.HashedPassword, arg.Email)
row := q.db.QueryRowContext(ctx, createUser,
arg.ID,
arg.Username,
arg.HashedPassword,
arg.Email,
)
var i User
err := row.Scan(
&i.ID,
@@ -65,6 +71,24 @@ func (q *Queries) GetUser(ctx context.Context, id string) (User, error) {
return i, err
}
const getUserByEmail = `-- name: GetUserByEmail :one
SELECT id, username, hashed_password, email, created_at FROM users
WHERE email = $1 LIMIT 1
`
func (q *Queries) GetUserByEmail(ctx context.Context, email string) (User, error) {
row := q.db.QueryRowContext(ctx, getUserByEmail, email)
var i User
err := row.Scan(
&i.ID,
&i.Username,
&i.HashedPassword,
&i.Email,
&i.CreatedAt,
)
return i, err
}
const getUserByName = `-- name: GetUserByName :one
SELECT id, username, hashed_password, email, created_at FROM users
WHERE username = $1 LIMIT 1