Use uuid.UUID everywhere and have postgres generate ids
This commit is contained in:
78
postgres/transactions.sql.go
Normal file
78
postgres/transactions.sql.go
Normal file
@ -0,0 +1,78 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// source: transactions.sql
|
||||
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
const createTransaction = `-- name: CreateTransaction :one
|
||||
INSERT INTO transactions
|
||||
(budget_id, date, memo, amount)
|
||||
VALUES ($1, $2, $3, $4)
|
||||
RETURNING id, budget_id, date, memo, amount
|
||||
`
|
||||
|
||||
type CreateTransactionParams struct {
|
||||
BudgetID uuid.UUID
|
||||
Date time.Time
|
||||
Memo sql.NullString
|
||||
Amount string
|
||||
}
|
||||
|
||||
func (q *Queries) CreateTransaction(ctx context.Context, arg CreateTransactionParams) (Transaction, error) {
|
||||
row := q.db.QueryRowContext(ctx, createTransaction,
|
||||
arg.BudgetID,
|
||||
arg.Date,
|
||||
arg.Memo,
|
||||
arg.Amount,
|
||||
)
|
||||
var i Transaction
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.BudgetID,
|
||||
&i.Date,
|
||||
&i.Memo,
|
||||
&i.Amount,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const getTransactionsForBudget = `-- name: GetTransactionsForBudget :many
|
||||
SELECT transactions.id, transactions.budget_id, transactions.date, transactions.memo, transactions.amount FROM transactions
|
||||
WHERE transactions.budget_id = $1
|
||||
`
|
||||
|
||||
func (q *Queries) GetTransactionsForBudget(ctx context.Context, budgetID uuid.UUID) ([]Transaction, error) {
|
||||
rows, err := q.db.QueryContext(ctx, getTransactionsForBudget, budgetID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []Transaction
|
||||
for rows.Next() {
|
||||
var i Transaction
|
||||
if err := rows.Scan(
|
||||
&i.ID,
|
||||
&i.BudgetID,
|
||||
&i.Date,
|
||||
&i.Memo,
|
||||
&i.Amount,
|
||||
); 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
|
||||
}
|
Reference in New Issue
Block a user