Use uuid.UUID everywhere and have postgres generate ids

This commit is contained in:
2021-11-29 21:49:37 +00:00
parent 5e8a98872f
commit 85ef7557c1
19 changed files with 199 additions and 93 deletions

View File

@ -5,46 +5,43 @@ package postgres
import (
"context"
"github.com/google/uuid"
)
const createBudget = `-- name: CreateBudget :one
INSERT INTO budgets
(id, name, last_modification)
VALUES ($1, $2, NOW())
RETURNING id, name, last_modification
(name, last_modification)
VALUES ($1, NOW())
RETURNING name, last_modification, id
`
type CreateBudgetParams struct {
ID string
Name string
}
func (q *Queries) CreateBudget(ctx context.Context, arg CreateBudgetParams) (Budget, error) {
row := q.db.QueryRowContext(ctx, createBudget, arg.ID, arg.Name)
func (q *Queries) CreateBudget(ctx context.Context, name string) (Budget, error) {
row := q.db.QueryRowContext(ctx, createBudget, name)
var i Budget
err := row.Scan(&i.ID, &i.Name, &i.LastModification)
err := row.Scan(&i.Name, &i.LastModification, &i.ID)
return i, err
}
const getBudget = `-- name: GetBudget :one
SELECT id, name, last_modification FROM budgets
SELECT name, last_modification, id FROM budgets
WHERE id = $1
`
func (q *Queries) GetBudget(ctx context.Context, id string) (Budget, error) {
func (q *Queries) GetBudget(ctx context.Context, id uuid.UUID) (Budget, error) {
row := q.db.QueryRowContext(ctx, getBudget, id)
var i Budget
err := row.Scan(&i.ID, &i.Name, &i.LastModification)
err := row.Scan(&i.Name, &i.LastModification, &i.ID)
return i, err
}
const getBudgetsForUser = `-- name: GetBudgetsForUser :many
SELECT budgets.id, budgets.name, budgets.last_modification FROM budgets
SELECT budgets.name, budgets.last_modification, budgets.id FROM budgets
LEFT JOIN user_budgets ON budgets.id = user_budgets.budget_id
WHERE user_budgets.user_id = $1
`
func (q *Queries) GetBudgetsForUser(ctx context.Context, userID string) ([]Budget, error) {
func (q *Queries) GetBudgetsForUser(ctx context.Context, userID uuid.UUID) ([]Budget, error) {
rows, err := q.db.QueryContext(ctx, getBudgetsForUser, userID)
if err != nil {
return nil, err
@ -53,7 +50,7 @@ func (q *Queries) GetBudgetsForUser(ctx context.Context, userID string) ([]Budge
var items []Budget
for rows.Next() {
var i Budget
if err := rows.Scan(&i.ID, &i.Name, &i.LastModification); err != nil {
if err := rows.Scan(&i.Name, &i.LastModification, &i.ID); err != nil {
return nil, err
}
items = append(items, i)