// Code generated by sqlc. DO NOT EDIT.
// versions:
//   sqlc v1.27.0
// source: heartbeat.sql

package sqlite

import (
	"context"
	"time"
)

const createHeartbeat = `-- name: CreateHeartbeat :one
INSERT INTO heartbeat
    ("user", created_at, ip_addr, auth_token)
VALUES (?, ?, ?, ?)
RETURNING id, user, created_at, ip_addr, auth_token
`

type CreateHeartbeatParams struct {
	User      int64      `db:"user" json:"user"`
	CreatedAt *time.Time `db:"created_at" json:"created_at"`
	IpAddr    string     `db:"ip_addr" json:"ip_addr"`
	AuthToken string     `db:"auth_token" json:"auth_token"`
}

func (q *Queries) CreateHeartbeat(ctx context.Context, arg CreateHeartbeatParams) (Heartbeat, error) {
	row := q.db.QueryRowContext(ctx, createHeartbeat,
		arg.User,
		arg.CreatedAt,
		arg.IpAddr,
		arg.AuthToken,
	)
	var i Heartbeat
	err := row.Scan(
		&i.Identifier,
		&i.User,
		&i.CreatedAt,
		&i.IpAddr,
		&i.AuthToken,
	)
	return i, err
}

const findHeartbeatByID = `-- name: FindHeartbeatByID :one
SELECT id, user, created_at, ip_addr, auth_token
FROM heartbeat
WHERE id = ?
`

func (q *Queries) FindHeartbeatByID(ctx context.Context, id int64) (Heartbeat, error) {
	row := q.db.QueryRowContext(ctx, findHeartbeatByID, id)
	var i Heartbeat
	err := row.Scan(
		&i.Identifier,
		&i.User,
		&i.CreatedAt,
		&i.IpAddr,
		&i.AuthToken,
	)
	return i, err
}

const findHeartbeatByUser = `-- name: FindHeartbeatByUser :many
SELECT id, user, created_at, ip_addr, auth_token
FROM heartbeat
WHERE "user" = ?
ORDER BY created_at DESC
`

func (q *Queries) FindHeartbeatByUser(ctx context.Context, user int64) ([]Heartbeat, error) {
	rows, err := q.db.QueryContext(ctx, findHeartbeatByUser, user)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []Heartbeat{}
	for rows.Next() {
		var i Heartbeat
		if err := rows.Scan(
			&i.Identifier,
			&i.User,
			&i.CreatedAt,
			&i.IpAddr,
			&i.AuthToken,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Close(); err != nil {
		return nil, err
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}
