414 lines
9.9 KiB
Go
414 lines
9.9 KiB
Go
// Code generated by sqlc. DO NOT EDIT.
|
|
// versions:
|
|
// sqlc v1.30.0
|
|
// source: users.sql
|
|
|
|
package sqlc
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/jackc/pgx/v5/pgtype"
|
|
)
|
|
|
|
const createUser = `-- name: CreateUser :one
|
|
INSERT INTO users (
|
|
name,
|
|
email,
|
|
password_hash,
|
|
avatar_url
|
|
) VALUES (
|
|
$1, $2, $3, $4
|
|
)
|
|
RETURNING id, name, email, password_hash, avatar_url, is_active, is_verified, token_version, refresh_token, is_deleted, created_at, updated_at
|
|
`
|
|
|
|
type CreateUserParams struct {
|
|
Name string `json:"name"`
|
|
Email string `json:"email"`
|
|
PasswordHash string `json:"password_hash"`
|
|
AvatarUrl pgtype.Text `json:"avatar_url"`
|
|
}
|
|
|
|
func (q *Queries) CreateUser(ctx context.Context, arg CreateUserParams) (User, error) {
|
|
row := q.db.QueryRow(ctx, createUser,
|
|
arg.Name,
|
|
arg.Email,
|
|
arg.PasswordHash,
|
|
arg.AvatarUrl,
|
|
)
|
|
var i User
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.Name,
|
|
&i.Email,
|
|
&i.PasswordHash,
|
|
&i.AvatarUrl,
|
|
&i.IsActive,
|
|
&i.IsVerified,
|
|
&i.TokenVersion,
|
|
&i.RefreshToken,
|
|
&i.IsDeleted,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const deleteUser = `-- name: DeleteUser :exec
|
|
UPDATE users
|
|
SET
|
|
is_deleted = true,
|
|
updated_at = now()
|
|
WHERE id = $1
|
|
`
|
|
|
|
func (q *Queries) DeleteUser(ctx context.Context, id pgtype.UUID) error {
|
|
_, err := q.db.Exec(ctx, deleteUser, id)
|
|
return err
|
|
}
|
|
|
|
const existsUserByEmail = `-- name: ExistsUserByEmail :one
|
|
SELECT EXISTS (
|
|
SELECT 1 FROM users
|
|
WHERE email = $1
|
|
AND is_deleted = false
|
|
)
|
|
`
|
|
|
|
func (q *Queries) ExistsUserByEmail(ctx context.Context, email string) (bool, error) {
|
|
row := q.db.QueryRow(ctx, existsUserByEmail, email)
|
|
var exists bool
|
|
err := row.Scan(&exists)
|
|
return exists, err
|
|
}
|
|
|
|
const getUserByEmail = `-- name: GetUserByEmail :one
|
|
SELECT
|
|
u.id,
|
|
u.name,
|
|
u.email,
|
|
u.password_hash,
|
|
u.avatar_url,
|
|
u.is_active,
|
|
u.is_verified,
|
|
u.token_version,
|
|
u.is_deleted,
|
|
u.created_at,
|
|
u.updated_at,
|
|
COALESCE(
|
|
json_agg(
|
|
json_build_object('id', r.id, 'name', r.name)
|
|
) FILTER (WHERE r.id IS NOT NULL),
|
|
'[]'
|
|
)::json AS roles
|
|
FROM users u
|
|
LEFT JOIN user_roles ur ON u.id = ur.user_id
|
|
LEFT JOIN roles r ON ur.role_id = r.id
|
|
WHERE u.email = $1 AND u.is_deleted = false
|
|
GROUP BY u.id
|
|
`
|
|
|
|
type GetUserByEmailRow struct {
|
|
ID pgtype.UUID `json:"id"`
|
|
Name string `json:"name"`
|
|
Email string `json:"email"`
|
|
PasswordHash string `json:"password_hash"`
|
|
AvatarUrl pgtype.Text `json:"avatar_url"`
|
|
IsActive pgtype.Bool `json:"is_active"`
|
|
IsVerified pgtype.Bool `json:"is_verified"`
|
|
TokenVersion int32 `json:"token_version"`
|
|
IsDeleted pgtype.Bool `json:"is_deleted"`
|
|
CreatedAt pgtype.Timestamptz `json:"created_at"`
|
|
UpdatedAt pgtype.Timestamptz `json:"updated_at"`
|
|
Roles []byte `json:"roles"`
|
|
}
|
|
|
|
func (q *Queries) GetUserByEmail(ctx context.Context, email string) (GetUserByEmailRow, error) {
|
|
row := q.db.QueryRow(ctx, getUserByEmail, email)
|
|
var i GetUserByEmailRow
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.Name,
|
|
&i.Email,
|
|
&i.PasswordHash,
|
|
&i.AvatarUrl,
|
|
&i.IsActive,
|
|
&i.IsVerified,
|
|
&i.TokenVersion,
|
|
&i.IsDeleted,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
&i.Roles,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const getUserByID = `-- name: GetUserByID :one
|
|
SELECT
|
|
u.id,
|
|
u.name,
|
|
u.email,
|
|
u.password_hash,
|
|
u.avatar_url,
|
|
u.is_active,
|
|
u.is_verified,
|
|
u.token_version,
|
|
u.refresh_token,
|
|
u.is_deleted,
|
|
u.created_at,
|
|
u.updated_at,
|
|
COALESCE(
|
|
json_agg(
|
|
json_build_object('id', r.id, 'name', r.name)
|
|
) FILTER (WHERE r.id IS NOT NULL),
|
|
'[]'
|
|
)::json AS roles
|
|
FROM users u
|
|
LEFT JOIN user_roles ur ON u.id = ur.user_id
|
|
LEFT JOIN roles r ON ur.role_id = r.id
|
|
WHERE u.id = $1 AND u.is_deleted = false
|
|
GROUP BY u.id
|
|
`
|
|
|
|
type GetUserByIDRow struct {
|
|
ID pgtype.UUID `json:"id"`
|
|
Name string `json:"name"`
|
|
Email string `json:"email"`
|
|
PasswordHash string `json:"password_hash"`
|
|
AvatarUrl pgtype.Text `json:"avatar_url"`
|
|
IsActive pgtype.Bool `json:"is_active"`
|
|
IsVerified pgtype.Bool `json:"is_verified"`
|
|
TokenVersion int32 `json:"token_version"`
|
|
RefreshToken pgtype.Text `json:"refresh_token"`
|
|
IsDeleted pgtype.Bool `json:"is_deleted"`
|
|
CreatedAt pgtype.Timestamptz `json:"created_at"`
|
|
UpdatedAt pgtype.Timestamptz `json:"updated_at"`
|
|
Roles []byte `json:"roles"`
|
|
}
|
|
|
|
func (q *Queries) GetUserByID(ctx context.Context, id pgtype.UUID) (GetUserByIDRow, error) {
|
|
row := q.db.QueryRow(ctx, getUserByID, id)
|
|
var i GetUserByIDRow
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.Name,
|
|
&i.Email,
|
|
&i.PasswordHash,
|
|
&i.AvatarUrl,
|
|
&i.IsActive,
|
|
&i.IsVerified,
|
|
&i.TokenVersion,
|
|
&i.RefreshToken,
|
|
&i.IsDeleted,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
&i.Roles,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const getUsers = `-- name: GetUsers :many
|
|
SELECT
|
|
u.id,
|
|
u.name,
|
|
u.email,
|
|
u.password_hash,
|
|
u.avatar_url,
|
|
u.is_active,
|
|
u.is_verified,
|
|
u.token_version,
|
|
u.refresh_token,
|
|
u.is_deleted,
|
|
u.created_at,
|
|
u.updated_at,
|
|
COALESCE(
|
|
json_agg(
|
|
json_build_object('id', r.id, 'name', r.name)
|
|
) FILTER (WHERE r.id IS NOT NULL),
|
|
'[]'
|
|
)::json AS roles
|
|
FROM users u
|
|
LEFT JOIN user_roles ur ON u.id = ur.user_id
|
|
LEFT JOIN roles r ON ur.role_id = r.id
|
|
WHERE u.is_deleted = false
|
|
GROUP BY u.id
|
|
`
|
|
|
|
type GetUsersRow struct {
|
|
ID pgtype.UUID `json:"id"`
|
|
Name string `json:"name"`
|
|
Email string `json:"email"`
|
|
PasswordHash string `json:"password_hash"`
|
|
AvatarUrl pgtype.Text `json:"avatar_url"`
|
|
IsActive pgtype.Bool `json:"is_active"`
|
|
IsVerified pgtype.Bool `json:"is_verified"`
|
|
TokenVersion int32 `json:"token_version"`
|
|
RefreshToken pgtype.Text `json:"refresh_token"`
|
|
IsDeleted pgtype.Bool `json:"is_deleted"`
|
|
CreatedAt pgtype.Timestamptz `json:"created_at"`
|
|
UpdatedAt pgtype.Timestamptz `json:"updated_at"`
|
|
Roles []byte `json:"roles"`
|
|
}
|
|
|
|
func (q *Queries) GetUsers(ctx context.Context) ([]GetUsersRow, error) {
|
|
rows, err := q.db.Query(ctx, getUsers)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := []GetUsersRow{}
|
|
for rows.Next() {
|
|
var i GetUsersRow
|
|
if err := rows.Scan(
|
|
&i.ID,
|
|
&i.Name,
|
|
&i.Email,
|
|
&i.PasswordHash,
|
|
&i.AvatarUrl,
|
|
&i.IsActive,
|
|
&i.IsVerified,
|
|
&i.TokenVersion,
|
|
&i.RefreshToken,
|
|
&i.IsDeleted,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
&i.Roles,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const restoreUser = `-- name: RestoreUser :exec
|
|
UPDATE users
|
|
SET
|
|
is_deleted = false,
|
|
updated_at = now()
|
|
WHERE id = $1
|
|
`
|
|
|
|
func (q *Queries) RestoreUser(ctx context.Context, id pgtype.UUID) error {
|
|
_, err := q.db.Exec(ctx, restoreUser, id)
|
|
return err
|
|
}
|
|
|
|
const updateUser = `-- name: UpdateUser :one
|
|
UPDATE users
|
|
SET
|
|
name = $1,
|
|
avatar_url = $2,
|
|
is_active = $3,
|
|
is_verified = $4,
|
|
updated_at = now()
|
|
WHERE users.id = $5 AND users.is_deleted = false
|
|
RETURNING
|
|
users.id,
|
|
users.name,
|
|
users.email,
|
|
users.password_hash,
|
|
users.avatar_url,
|
|
users.is_active,
|
|
users.is_verified,
|
|
users.token_version,
|
|
users.refresh_token,
|
|
users.is_deleted,
|
|
users.created_at,
|
|
users.updated_at,
|
|
(
|
|
SELECT COALESCE(json_agg(json_build_object('id', roles.id, 'name', roles.name)), '[]')::json
|
|
FROM user_roles
|
|
JOIN roles ON user_roles.role_id = roles.id
|
|
WHERE user_roles.user_id = users.id
|
|
) AS roles
|
|
`
|
|
|
|
type UpdateUserParams struct {
|
|
Name string `json:"name"`
|
|
AvatarUrl pgtype.Text `json:"avatar_url"`
|
|
IsActive pgtype.Bool `json:"is_active"`
|
|
IsVerified pgtype.Bool `json:"is_verified"`
|
|
ID pgtype.UUID `json:"id"`
|
|
}
|
|
|
|
type UpdateUserRow struct {
|
|
ID pgtype.UUID `json:"id"`
|
|
Name string `json:"name"`
|
|
Email string `json:"email"`
|
|
PasswordHash string `json:"password_hash"`
|
|
AvatarUrl pgtype.Text `json:"avatar_url"`
|
|
IsActive pgtype.Bool `json:"is_active"`
|
|
IsVerified pgtype.Bool `json:"is_verified"`
|
|
TokenVersion int32 `json:"token_version"`
|
|
RefreshToken pgtype.Text `json:"refresh_token"`
|
|
IsDeleted pgtype.Bool `json:"is_deleted"`
|
|
CreatedAt pgtype.Timestamptz `json:"created_at"`
|
|
UpdatedAt pgtype.Timestamptz `json:"updated_at"`
|
|
Roles []byte `json:"roles"`
|
|
}
|
|
|
|
func (q *Queries) UpdateUser(ctx context.Context, arg UpdateUserParams) (UpdateUserRow, error) {
|
|
row := q.db.QueryRow(ctx, updateUser,
|
|
arg.Name,
|
|
arg.AvatarUrl,
|
|
arg.IsActive,
|
|
arg.IsVerified,
|
|
arg.ID,
|
|
)
|
|
var i UpdateUserRow
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.Name,
|
|
&i.Email,
|
|
&i.PasswordHash,
|
|
&i.AvatarUrl,
|
|
&i.IsActive,
|
|
&i.IsVerified,
|
|
&i.TokenVersion,
|
|
&i.RefreshToken,
|
|
&i.IsDeleted,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
&i.Roles,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const updateUserPassword = `-- name: UpdateUserPassword :exec
|
|
UPDATE users
|
|
SET
|
|
password_hash = $2,
|
|
updated_at = now()
|
|
WHERE id = $1
|
|
AND is_deleted = false
|
|
`
|
|
|
|
type UpdateUserPasswordParams struct {
|
|
ID pgtype.UUID `json:"id"`
|
|
PasswordHash string `json:"password_hash"`
|
|
}
|
|
|
|
func (q *Queries) UpdateUserPassword(ctx context.Context, arg UpdateUserPasswordParams) error {
|
|
_, err := q.db.Exec(ctx, updateUserPassword, arg.ID, arg.PasswordHash)
|
|
return err
|
|
}
|
|
|
|
const verifyUser = `-- name: VerifyUser :exec
|
|
UPDATE users
|
|
SET
|
|
is_verified = true,
|
|
updated_at = now()
|
|
WHERE id = $1
|
|
AND is_deleted = false
|
|
`
|
|
|
|
func (q *Queries) VerifyUser(ctx context.Context, id pgtype.UUID) error {
|
|
_, err := q.db.Exec(ctx, verifyUser, id)
|
|
return err
|
|
}
|