// Code generated by sqlc. DO NOT EDIT.
// versions:
//   sqlc v1.30.0
// source: users.sql

package db

import (
	"context"
)

const createUser = `-- name: CreateUser :one
INSERT INTO users (id, username, biography)
VALUES ($1, $2, $3)
RETURNING id, username, biography
`

type CreateUserParams struct {
	ID        string  `json:"id"`
	Username  string  `json:"username"`
	Biography *string `json:"biography"`
}

func (q *Queries) CreateUser(ctx context.Context, arg CreateUserParams) (*User, error) {
	row := q.db.QueryRow(ctx, createUser, arg.ID, arg.Username, arg.Biography)
	var i User
	err := row.Scan(&i.ID, &i.Username, &i.Biography)
	return &i, err
}

const deleteUser = `-- name: DeleteUser :exec
DELETE FROM users
WHERE id = $1
`

func (q *Queries) DeleteUser(ctx context.Context, id string) error {
	_, err := q.db.Exec(ctx, deleteUser, id)
	return err
}

const getUser = `-- name: GetUser :one
SELECT id, username, biography FROM users
WHERE id = $1
`

func (q *Queries) GetUser(ctx context.Context, id string) (*User, error) {
	row := q.db.QueryRow(ctx, getUser, id)
	var i User
	err := row.Scan(&i.ID, &i.Username, &i.Biography)
	return &i, err
}

const getUserByUsername = `-- name: GetUserByUsername :one
SELECT id, username, biography FROM users
WHERE username = $1
`

func (q *Queries) GetUserByUsername(ctx context.Context, username string) (*User, error) {
	row := q.db.QueryRow(ctx, getUserByUsername, username)
	var i User
	err := row.Scan(&i.ID, &i.Username, &i.Biography)
	return &i, err
}

const listUsers = `-- name: ListUsers :many
SELECT id, username, biography FROM users
ORDER BY id
`

func (q *Queries) ListUsers(ctx context.Context) ([]*User, error) {
	rows, err := q.db.Query(ctx, listUsers)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []*User{}
	for rows.Next() {
		var i User
		if err := rows.Scan(&i.ID, &i.Username, &i.Biography); err != nil {
			return nil, err
		}
		items = append(items, &i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const updateUser = `-- name: UpdateUser :one
UPDATE users
SET
  username  = coalesce($1, username),
  biography = coalesce($2, biography)
WHERE id = $3
RETURNING id, username, biography
`

type UpdateUserParams struct {
	Username  *string `json:"username"`
	Biography *string `json:"biography"`
	ID        string  `json:"id"`
}

func (q *Queries) UpdateUser(ctx context.Context, arg UpdateUserParams) (*User, error) {
	row := q.db.QueryRow(ctx, updateUser, arg.Username, arg.Biography, arg.ID)
	var i User
	err := row.Scan(&i.ID, &i.Username, &i.Biography)
	return &i, err
}
