Import category
This commit is contained in:
117
postgres/categories.sql.go
Normal file
117
postgres/categories.sql.go
Normal file
@ -0,0 +1,117 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// source: categories.sql
|
||||
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
const createCategory = `-- name: CreateCategory :one
|
||||
INSERT INTO categories
|
||||
(name, category_group_id)
|
||||
VALUES ($1, $2)
|
||||
RETURNING id, category_group_id, name
|
||||
`
|
||||
|
||||
type CreateCategoryParams struct {
|
||||
Name string
|
||||
CategoryGroupID uuid.UUID
|
||||
}
|
||||
|
||||
func (q *Queries) CreateCategory(ctx context.Context, arg CreateCategoryParams) (Category, error) {
|
||||
row := q.db.QueryRowContext(ctx, createCategory, arg.Name, arg.CategoryGroupID)
|
||||
var i Category
|
||||
err := row.Scan(&i.ID, &i.CategoryGroupID, &i.Name)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const createCategoryGroup = `-- name: CreateCategoryGroup :one
|
||||
INSERT INTO category_groups
|
||||
(name, budget_id)
|
||||
VALUES ($1, $2)
|
||||
RETURNING id, budget_id, name
|
||||
`
|
||||
|
||||
type CreateCategoryGroupParams struct {
|
||||
Name string
|
||||
BudgetID uuid.UUID
|
||||
}
|
||||
|
||||
func (q *Queries) CreateCategoryGroup(ctx context.Context, arg CreateCategoryGroupParams) (CategoryGroup, error) {
|
||||
row := q.db.QueryRowContext(ctx, createCategoryGroup, arg.Name, arg.BudgetID)
|
||||
var i CategoryGroup
|
||||
err := row.Scan(&i.ID, &i.BudgetID, &i.Name)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const getCategories = `-- name: GetCategories :many
|
||||
SELECT categories.id, categories.category_group_id, categories.name, category_groups.name as group FROM categories
|
||||
INNER JOIN category_groups ON categories.category_group_id = category_groups.id
|
||||
WHERE category_groups.budget_id = $1
|
||||
`
|
||||
|
||||
type GetCategoriesRow struct {
|
||||
ID uuid.UUID
|
||||
CategoryGroupID uuid.UUID
|
||||
Name string
|
||||
Group string
|
||||
}
|
||||
|
||||
func (q *Queries) GetCategories(ctx context.Context, budgetID uuid.UUID) ([]GetCategoriesRow, error) {
|
||||
rows, err := q.db.QueryContext(ctx, getCategories, budgetID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []GetCategoriesRow
|
||||
for rows.Next() {
|
||||
var i GetCategoriesRow
|
||||
if err := rows.Scan(
|
||||
&i.ID,
|
||||
&i.CategoryGroupID,
|
||||
&i.Name,
|
||||
&i.Group,
|
||||
); 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
|
||||
}
|
||||
|
||||
const getCategoryGroups = `-- name: GetCategoryGroups :many
|
||||
SELECT category_groups.id, category_groups.budget_id, category_groups.name FROM category_groups
|
||||
WHERE category_groups.budget_id = $1
|
||||
`
|
||||
|
||||
func (q *Queries) GetCategoryGroups(ctx context.Context, budgetID uuid.UUID) ([]CategoryGroup, error) {
|
||||
rows, err := q.db.QueryContext(ctx, getCategoryGroups, budgetID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []CategoryGroup
|
||||
for rows.Next() {
|
||||
var i CategoryGroup
|
||||
if err := rows.Scan(&i.ID, &i.BudgetID, &i.Name); 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
|
||||
}
|
@ -21,6 +21,18 @@ type Budget struct {
|
||||
LastModification sql.NullTime
|
||||
}
|
||||
|
||||
type Category struct {
|
||||
ID uuid.UUID
|
||||
CategoryGroupID uuid.UUID
|
||||
Name string
|
||||
}
|
||||
|
||||
type CategoryGroup struct {
|
||||
ID uuid.UUID
|
||||
BudgetID uuid.UUID
|
||||
Name string
|
||||
}
|
||||
|
||||
type Payee struct {
|
||||
ID uuid.UUID
|
||||
BudgetID uuid.UUID
|
||||
@ -28,12 +40,13 @@ type Payee struct {
|
||||
}
|
||||
|
||||
type Transaction struct {
|
||||
ID uuid.UUID
|
||||
Date time.Time
|
||||
Memo string
|
||||
Amount Numeric
|
||||
AccountID uuid.UUID
|
||||
PayeeID uuid.NullUUID
|
||||
ID uuid.UUID
|
||||
Date time.Time
|
||||
Memo string
|
||||
Amount Numeric
|
||||
AccountID uuid.UUID
|
||||
CategoryID uuid.NullUUID
|
||||
PayeeID uuid.NullUUID
|
||||
}
|
||||
|
||||
type User struct {
|
||||
|
20
postgres/queries/categories.sql
Normal file
20
postgres/queries/categories.sql
Normal file
@ -0,0 +1,20 @@
|
||||
-- name: CreateCategoryGroup :one
|
||||
INSERT INTO category_groups
|
||||
(name, budget_id)
|
||||
VALUES ($1, $2)
|
||||
RETURNING *;
|
||||
|
||||
-- name: GetCategoryGroups :many
|
||||
SELECT category_groups.* FROM category_groups
|
||||
WHERE category_groups.budget_id = $1;
|
||||
|
||||
-- name: CreateCategory :one
|
||||
INSERT INTO categories
|
||||
(name, category_group_id)
|
||||
VALUES ($1, $2)
|
||||
RETURNING *;
|
||||
|
||||
-- name: GetCategories :many
|
||||
SELECT categories.*, category_groups.name as group FROM categories
|
||||
INNER JOIN category_groups ON categories.category_group_id = category_groups.id
|
||||
WHERE category_groups.budget_id = $1;
|
@ -1,7 +1,7 @@
|
||||
-- name: CreateTransaction :one
|
||||
INSERT INTO transactions
|
||||
(date, memo, amount, account_id, payee_id)
|
||||
VALUES ($1, $2, $3, $4, $5)
|
||||
(date, memo, amount, account_id, payee_id, category_id)
|
||||
VALUES ($1, $2, $3, $4, $5, $6)
|
||||
RETURNING *;
|
||||
|
||||
-- name: GetTransactionsForBudget :many
|
||||
|
@ -33,6 +33,7 @@ CREATE TABLE payees (
|
||||
budget_id uuid NOT NULL,
|
||||
name varchar(50) NOT NULL
|
||||
);
|
||||
ALTER TABLE "payees" ADD FOREIGN KEY ("budget_id") REFERENCES "budgets" ("id");
|
||||
|
||||
CREATE TABLE transactions (
|
||||
id uuid DEFAULT uuid_generate_v4() PRIMARY KEY,
|
||||
@ -40,10 +41,27 @@ CREATE TABLE transactions (
|
||||
memo text NOT NULL,
|
||||
amount decimal(12,2) NOT NULL,
|
||||
account_id uuid NOT NULL,
|
||||
category_id uuid,
|
||||
payee_id uuid
|
||||
);
|
||||
ALTER TABLE "transactions" ADD FOREIGN KEY ("account_id") REFERENCES "accounts" ("id");
|
||||
ALTER TABLE "transactions" ADD FOREIGN KEY ("payee_id") REFERENCES "payees" ("id");
|
||||
ALTER TABLE "transactions" ADD FOREIGN KEY ("category_id") REFERENCES "categories" ("id");
|
||||
|
||||
CREATE TABLE category_groups (
|
||||
id uuid DEFAULT uuid_generate_v4() PRIMARY KEY,
|
||||
budget_id uuid NOT NULL,
|
||||
name varchar(50) NOT NULL
|
||||
);
|
||||
ALTER TABLE "category_groups" ADD FOREIGN KEY ("budget_id") REFERENCES "budgets" ("id");
|
||||
|
||||
CREATE TABLE categories (
|
||||
id uuid DEFAULT uuid_generate_v4() PRIMARY KEY,
|
||||
category_group_id uuid NOT NULL,
|
||||
name varchar(50) NOT NULL
|
||||
);
|
||||
ALTER TABLE "categories" ADD FOREIGN KEY ("category_group_id") REFERENCES "category_group" ("id");
|
||||
|
||||
|
||||
-- +goose Down
|
||||
DROP TABLE transactions;
|
||||
|
@ -12,17 +12,18 @@ import (
|
||||
|
||||
const createTransaction = `-- name: CreateTransaction :one
|
||||
INSERT INTO transactions
|
||||
(date, memo, amount, account_id, payee_id)
|
||||
VALUES ($1, $2, $3, $4, $5)
|
||||
RETURNING id, date, memo, amount, account_id, payee_id
|
||||
(date, memo, amount, account_id, payee_id, category_id)
|
||||
VALUES ($1, $2, $3, $4, $5, $6)
|
||||
RETURNING id, date, memo, amount, account_id, category_id, payee_id
|
||||
`
|
||||
|
||||
type CreateTransactionParams struct {
|
||||
Date time.Time
|
||||
Memo string
|
||||
Amount Numeric
|
||||
AccountID uuid.UUID
|
||||
PayeeID uuid.NullUUID
|
||||
Date time.Time
|
||||
Memo string
|
||||
Amount Numeric
|
||||
AccountID uuid.UUID
|
||||
PayeeID uuid.NullUUID
|
||||
CategoryID uuid.NullUUID
|
||||
}
|
||||
|
||||
func (q *Queries) CreateTransaction(ctx context.Context, arg CreateTransactionParams) (Transaction, error) {
|
||||
@ -32,6 +33,7 @@ func (q *Queries) CreateTransaction(ctx context.Context, arg CreateTransactionPa
|
||||
arg.Amount,
|
||||
arg.AccountID,
|
||||
arg.PayeeID,
|
||||
arg.CategoryID,
|
||||
)
|
||||
var i Transaction
|
||||
err := row.Scan(
|
||||
@ -40,6 +42,7 @@ func (q *Queries) CreateTransaction(ctx context.Context, arg CreateTransactionPa
|
||||
&i.Memo,
|
||||
&i.Amount,
|
||||
&i.AccountID,
|
||||
&i.CategoryID,
|
||||
&i.PayeeID,
|
||||
)
|
||||
return i, err
|
||||
|
Reference in New Issue
Block a user