105 lines
		
	
	
		
			2.6 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			105 lines
		
	
	
		
			2.6 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| // Code generated by sqlc. DO NOT EDIT.
 | |
| // source: budgets.sql
 | |
| 
 | |
| package postgres
 | |
| 
 | |
| import (
 | |
| 	"context"
 | |
| 	"time"
 | |
| 
 | |
| 	"github.com/google/uuid"
 | |
| )
 | |
| 
 | |
| const createBudget = `-- name: CreateBudget :one
 | |
| INSERT INTO budgets
 | |
| (name, last_modification)
 | |
| VALUES ($1, NOW())
 | |
| RETURNING id, name, last_modification, income_category_id
 | |
| `
 | |
| 
 | |
| 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,
 | |
| 		&i.IncomeCategoryID,
 | |
| 	)
 | |
| 	return i, err
 | |
| }
 | |
| 
 | |
| const getBudget = `-- name: GetBudget :one
 | |
| SELECT id, name, last_modification, income_category_id FROM budgets 
 | |
| WHERE id = $1
 | |
| `
 | |
| 
 | |
| 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,
 | |
| 		&i.IncomeCategoryID,
 | |
| 	)
 | |
| 	return i, err
 | |
| }
 | |
| 
 | |
| const getBudgetsForUser = `-- name: GetBudgetsForUser :many
 | |
| SELECT budgets.id, budgets.name, budgets.last_modification, budgets.income_category_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 uuid.UUID) ([]Budget, error) {
 | |
| 	rows, err := q.db.QueryContext(ctx, getBudgetsForUser, userID)
 | |
| 	if err != nil {
 | |
| 		return nil, err
 | |
| 	}
 | |
| 	defer rows.Close()
 | |
| 	var items []Budget
 | |
| 	for rows.Next() {
 | |
| 		var i Budget
 | |
| 		if err := rows.Scan(
 | |
| 			&i.ID,
 | |
| 			&i.Name,
 | |
| 			&i.LastModification,
 | |
| 			&i.IncomeCategoryID,
 | |
| 		); 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 getFirstActivity = `-- name: GetFirstActivity :one
 | |
| SELECT MIN(dates.min_date)::date as min_date
 | |
| FROM (
 | |
|         SELECT MIN(assignments.date) as min_date
 | |
|         FROM assignments
 | |
|         INNER JOIN categories ON categories.id = assignments.category_id
 | |
|         INNER JOIN category_groups ON category_groups.id = categories.category_group_id
 | |
|         WHERE category_groups.budget_id = $1
 | |
|         UNION
 | |
|         SELECT MIN(transactions.date) as min_date
 | |
|         FROM transactions
 | |
|         INNER JOIN accounts ON accounts.id = transactions.account_id
 | |
|         WHERE accounts.budget_id = $1
 | |
| ) dates
 | |
| `
 | |
| 
 | |
| func (q *Queries) GetFirstActivity(ctx context.Context, budgetID uuid.UUID) (time.Time, error) {
 | |
| 	row := q.db.QueryRowContext(ctx, getFirstActivity, budgetID)
 | |
| 	var min_date time.Time
 | |
| 	err := row.Scan(&min_date)
 | |
| 	return min_date, err
 | |
| }
 |