265 lines
6.0 KiB
Go
265 lines
6.0 KiB
Go
// Code generated by sqlc. DO NOT EDIT.
|
|
// versions:
|
|
// sqlc v1.27.0
|
|
// source: expense.sql
|
|
|
|
package sqlc
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"encoding/json"
|
|
"time"
|
|
)
|
|
|
|
const deleteExpense = `-- name: DeleteExpense :exec
|
|
DELETE FROM "expense" WHERE id = $1
|
|
`
|
|
|
|
func (q *Queries) DeleteExpense(ctx context.Context, id int32) error {
|
|
_, err := q.db.ExecContext(ctx, deleteExpense, id)
|
|
return err
|
|
}
|
|
|
|
const deleteTransactionsOfExpenseID = `-- name: DeleteTransactionsOfExpenseID :exec
|
|
DELETE FROM "transaction" WHERE transaction.expense_id = $1
|
|
`
|
|
|
|
func (q *Queries) DeleteTransactionsOfExpenseID(ctx context.Context, expenseID int32) error {
|
|
_, err := q.db.ExecContext(ctx, deleteTransactionsOfExpenseID, expenseID)
|
|
return err
|
|
}
|
|
|
|
const getExpenseByID = `-- name: GetExpenseByID :one
|
|
WITH payer_transaction as (
|
|
SELECT pt.expense_id,
|
|
json_agg(json_build_object(
|
|
'payer_id', p.id,
|
|
'payer_first_name', p.first_name,
|
|
'payer_last_name', p.last_name,
|
|
'amount', pt.amount,
|
|
'currency', pt.currency
|
|
)) AS payments
|
|
FROM "transaction" pt
|
|
JOIN "user" p ON p.id = pt.user_id
|
|
WHERE pt.is_income = FALSE
|
|
GROUP BY pt.expense_id
|
|
), -- For each expense, aggregate payment info
|
|
recipient_transaction as (
|
|
SELECT rt.expense_id,
|
|
json_agg(json_build_object(
|
|
'recipient_id', p.id,
|
|
'recipient_first_name', p.first_name,
|
|
'recipient_last_name', p.last_name,
|
|
'amount', rt.amount,
|
|
'currency', rt.currency
|
|
)) AS benefits
|
|
FROM "transaction" rt
|
|
JOIN "user" p ON p.id = rt.user_id
|
|
WHERE rt.is_income = TRUE
|
|
GROUP BY rt.expense_id
|
|
) -- For each expense, aggregate benefits info
|
|
SELECT
|
|
ex.id, ex.created_at, ex.updated_at, ex.amount, ex.currency, ex.event_id,
|
|
ex.name, ex.place,
|
|
COALESCE(pt.payments, '[]') AS payments,
|
|
COALESCE(rt.benefits, '[]') AS benefits
|
|
FROM "expense" ex
|
|
LEFT JOIN "payer_transaction" pt ON pt.expense_id = ex.id
|
|
LEFT JOIN "recipient_transaction" rt ON rt.expense_id = ex.id
|
|
WHERE ex.id = $1
|
|
`
|
|
|
|
type GetExpenseByIDRow struct {
|
|
ID int32
|
|
CreatedAt time.Time
|
|
UpdatedAt time.Time
|
|
Amount int32
|
|
Currency string
|
|
EventID int32
|
|
Name sql.NullString
|
|
Place sql.NullString
|
|
Payments json.RawMessage
|
|
Benefits json.RawMessage
|
|
}
|
|
|
|
func (q *Queries) GetExpenseByID(ctx context.Context, id int32) (GetExpenseByIDRow, error) {
|
|
row := q.db.QueryRowContext(ctx, getExpenseByID, id)
|
|
var i GetExpenseByIDRow
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
&i.Amount,
|
|
&i.Currency,
|
|
&i.EventID,
|
|
&i.Name,
|
|
&i.Place,
|
|
&i.Payments,
|
|
&i.Benefits,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const insertExpense = `-- name: InsertExpense :one
|
|
INSERT INTO "expense" (
|
|
created_at, updated_at, amount, currency, event_id, name, place
|
|
) VALUES ( $1, $2, $3, $4, $5, $6, $7 )
|
|
RETURNING id, created_at, updated_at, amount, currency, event_id, name, place
|
|
`
|
|
|
|
type InsertExpenseParams struct {
|
|
CreatedAt time.Time
|
|
UpdatedAt time.Time
|
|
Amount int32
|
|
Currency string
|
|
EventID int32
|
|
Name sql.NullString
|
|
Place sql.NullString
|
|
}
|
|
|
|
func (q *Queries) InsertExpense(ctx context.Context, arg InsertExpenseParams) (Expense, error) {
|
|
row := q.db.QueryRowContext(ctx, insertExpense,
|
|
arg.CreatedAt,
|
|
arg.UpdatedAt,
|
|
arg.Amount,
|
|
arg.Currency,
|
|
arg.EventID,
|
|
arg.Name,
|
|
arg.Place,
|
|
)
|
|
var i Expense
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
&i.Amount,
|
|
&i.Currency,
|
|
&i.EventID,
|
|
&i.Name,
|
|
&i.Place,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const listExpensesByEventID = `-- name: ListExpensesByEventID :many
|
|
SELECT
|
|
ex.id, ex.created_at, ex.updated_at, ex.amount, ex.currency, ex.event_id,
|
|
ex.name, ex.place
|
|
FROM "expense" ex
|
|
JOIN "event" ev ON ev.id = ex.event_id
|
|
WHERE ev.id = $1
|
|
`
|
|
|
|
func (q *Queries) ListExpensesByEventID(ctx context.Context, id int32) ([]Expense, error) {
|
|
rows, err := q.db.QueryContext(ctx, listExpensesByEventID, id)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []Expense
|
|
for rows.Next() {
|
|
var i Expense
|
|
if err := rows.Scan(
|
|
&i.ID,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
&i.Amount,
|
|
&i.Currency,
|
|
&i.EventID,
|
|
&i.Name,
|
|
&i.Place,
|
|
); 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 listExpensesByEventIDByUserID = `-- name: ListExpensesByEventIDByUserID :many
|
|
SELECT
|
|
ex.id, ex.created_at, ex.updated_at, ex.amount, ex.currency, ex.event_id,
|
|
ex.name, ex.place
|
|
FROM "expense" ex
|
|
JOIN "event" ev ON ev.id = ex.event_id
|
|
WHERE ev.id = $1
|
|
`
|
|
|
|
func (q *Queries) ListExpensesByEventIDByUserID(ctx context.Context, id int32) ([]Expense, error) {
|
|
rows, err := q.db.QueryContext(ctx, listExpensesByEventIDByUserID, id)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []Expense
|
|
for rows.Next() {
|
|
var i Expense
|
|
if err := rows.Scan(
|
|
&i.ID,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
&i.Amount,
|
|
&i.Currency,
|
|
&i.EventID,
|
|
&i.Name,
|
|
&i.Place,
|
|
); 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 updateExpenseByID = `-- name: UpdateExpenseByID :one
|
|
UPDATE "expense"
|
|
SET updated_at = $2, amount = $3, currency = $4, name = $5, place = $6
|
|
WHERE id = $1
|
|
RETURNING id, created_at, updated_at, amount, currency, event_id, name, place
|
|
`
|
|
|
|
type UpdateExpenseByIDParams struct {
|
|
ID int32
|
|
UpdatedAt time.Time
|
|
Amount int32
|
|
Currency string
|
|
Name sql.NullString
|
|
Place sql.NullString
|
|
}
|
|
|
|
func (q *Queries) UpdateExpenseByID(ctx context.Context, arg UpdateExpenseByIDParams) (Expense, error) {
|
|
row := q.db.QueryRowContext(ctx, updateExpenseByID,
|
|
arg.ID,
|
|
arg.UpdatedAt,
|
|
arg.Amount,
|
|
arg.Currency,
|
|
arg.Name,
|
|
arg.Place,
|
|
)
|
|
var i Expense
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
&i.Amount,
|
|
&i.Currency,
|
|
&i.EventID,
|
|
&i.Name,
|
|
&i.Place,
|
|
)
|
|
return i, err
|
|
}
|