budgeteer/postgres/accounts.sql.go
2021-12-02 21:29:11 +00:00

63 lines
1.3 KiB
Go

// Code generated by sqlc. DO NOT EDIT.
// source: accounts.sql
package postgres
import (
"context"
"github.com/google/uuid"
)
const createAccount = `-- name: CreateAccount :one
INSERT INTO accounts
(name, budget_id)
VALUES ($1, $2)
RETURNING id, budget_id, name
`
type CreateAccountParams struct {
Name string
BudgetID uuid.UUID
}
func (q *Queries) CreateAccount(ctx context.Context, arg CreateAccountParams) (Account, error) {
row := q.db.QueryRow(ctx, createAccount, arg.Name, arg.BudgetID)
var i Account
err := row.Scan(&i.ID, &i.BudgetID, &i.Name)
return i, err
}
const getAccounts = `-- name: GetAccounts :many
SELECT accounts.id, accounts.name, SUM(transactions.amount) as balance FROM accounts
WHERE accounts.budget_id = $1
AND transactions.date < NOW()
GROUP BY accounts.id, accounts.name
`
type GetAccountsRow struct {
ID uuid.UUID
Name string
Balance int64
}
func (q *Queries) GetAccounts(ctx context.Context, budgetID uuid.UUID) ([]GetAccountsRow, error) {
rows, err := q.db.Query(ctx, getAccounts, budgetID)
if err != nil {
return nil, err
}
defer rows.Close()
var items []GetAccountsRow
for rows.Next() {
var i GetAccountsRow
if err := rows.Scan(&i.ID, &i.Name, &i.Balance); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}