235 lines
		
	
	
		
			6.1 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			235 lines
		
	
	
		
			6.1 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| // Code generated by sqlc. DO NOT EDIT.
 | |
| // versions:
 | |
| //   sqlc v1.28.0
 | |
| // source: expenses.sql
 | |
| 
 | |
| package db
 | |
| 
 | |
| import (
 | |
| 	"context"
 | |
| 	"time"
 | |
| 
 | |
| 	"github.com/jackc/pgx/v5/pgtype"
 | |
| )
 | |
| 
 | |
| const createExpense = `-- name: CreateExpense :one
 | |
| INSERT INTO expenses (project_id, budget_id, amount, expenses_at, expenses_type, remark, status, created_user_id)
 | |
| VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
 | |
| RETURNING id, project_id, budget_id, amount, expenses_at, expenses_type, remark, status, created_at, created_user_id, updated_at, updated_user_id
 | |
| `
 | |
| 
 | |
| type CreateExpenseParams struct {
 | |
| 	ProjectID     int64          `json:"project_id"`
 | |
| 	BudgetID      int64          `json:"budget_id"`
 | |
| 	Amount        pgtype.Numeric `json:"amount"`
 | |
| 	ExpensesAt    time.Time      `json:"expenses_at"`
 | |
| 	ExpensesType  int32          `json:"expenses_type"`
 | |
| 	Remark        string         `json:"remark"`
 | |
| 	Status        int16          `json:"status"`
 | |
| 	CreatedUserID int32          `json:"created_user_id"`
 | |
| }
 | |
| 
 | |
| func (q *Queries) CreateExpense(ctx context.Context, arg *CreateExpenseParams) (*Expense, error) {
 | |
| 	row := q.db.QueryRow(ctx, createExpense,
 | |
| 		arg.ProjectID,
 | |
| 		arg.BudgetID,
 | |
| 		arg.Amount,
 | |
| 		arg.ExpensesAt,
 | |
| 		arg.ExpensesType,
 | |
| 		arg.Remark,
 | |
| 		arg.Status,
 | |
| 		arg.CreatedUserID,
 | |
| 	)
 | |
| 	var i Expense
 | |
| 	err := row.Scan(
 | |
| 		&i.ID,
 | |
| 		&i.ProjectID,
 | |
| 		&i.BudgetID,
 | |
| 		&i.Amount,
 | |
| 		&i.ExpensesAt,
 | |
| 		&i.ExpensesType,
 | |
| 		&i.Remark,
 | |
| 		&i.Status,
 | |
| 		&i.CreatedAt,
 | |
| 		&i.CreatedUserID,
 | |
| 		&i.UpdatedAt,
 | |
| 		&i.UpdatedUserID,
 | |
| 	)
 | |
| 	return &i, err
 | |
| }
 | |
| 
 | |
| const getExpense = `-- name: GetExpense :one
 | |
| SELECT id, project_id, budget_id, amount, expenses_at, expenses_type, remark, status, created_at, created_user_id, updated_at, updated_user_id 
 | |
| FROM expenses 
 | |
| WHERE id = $1
 | |
| LIMIT 1
 | |
| `
 | |
| 
 | |
| func (q *Queries) GetExpense(ctx context.Context, id int64) (*Expense, error) {
 | |
| 	row := q.db.QueryRow(ctx, getExpense, id)
 | |
| 	var i Expense
 | |
| 	err := row.Scan(
 | |
| 		&i.ID,
 | |
| 		&i.ProjectID,
 | |
| 		&i.BudgetID,
 | |
| 		&i.Amount,
 | |
| 		&i.ExpensesAt,
 | |
| 		&i.ExpensesType,
 | |
| 		&i.Remark,
 | |
| 		&i.Status,
 | |
| 		&i.CreatedAt,
 | |
| 		&i.CreatedUserID,
 | |
| 		&i.UpdatedAt,
 | |
| 		&i.UpdatedUserID,
 | |
| 	)
 | |
| 	return &i, err
 | |
| }
 | |
| 
 | |
| const statisticsExpense = `-- name: StatisticsExpense :many
 | |
| SELECT SUM(amount)::numeric as total_amount,
 | |
|     (select name from categories where id = expenses.expenses_type) expenses_type_name
 | |
| FROM expenses
 | |
| GROUP BY expenses_type
 | |
| `
 | |
| 
 | |
| type StatisticsExpenseRow struct {
 | |
| 	TotalAmount      pgtype.Numeric `json:"total_amount"`
 | |
| 	ExpensesTypeName string         `json:"expenses_type_name"`
 | |
| }
 | |
| 
 | |
| func (q *Queries) StatisticsExpense(ctx context.Context) ([]*StatisticsExpenseRow, error) {
 | |
| 	rows, err := q.db.Query(ctx, statisticsExpense)
 | |
| 	if err != nil {
 | |
| 		return nil, err
 | |
| 	}
 | |
| 	defer rows.Close()
 | |
| 	items := []*StatisticsExpenseRow{}
 | |
| 	for rows.Next() {
 | |
| 		var i StatisticsExpenseRow
 | |
| 		if err := rows.Scan(&i.TotalAmount, &i.ExpensesTypeName); err != nil {
 | |
| 			return nil, err
 | |
| 		}
 | |
| 		items = append(items, &i)
 | |
| 	}
 | |
| 	if err := rows.Err(); err != nil {
 | |
| 		return nil, err
 | |
| 	}
 | |
| 	return items, nil
 | |
| }
 | |
| 
 | |
| const statisticsExpenseByProjectID = `-- name: StatisticsExpenseByProjectID :many
 | |
| SELECT SUM(amount)::numeric as total_amount,
 | |
|     (select name from categories where id = expenses.expenses_type) expenses_type_name
 | |
| FROM expenses
 | |
| WHERE project_id = $1
 | |
| GROUP BY expenses_type
 | |
| `
 | |
| 
 | |
| type StatisticsExpenseByProjectIDRow struct {
 | |
| 	TotalAmount      pgtype.Numeric `json:"total_amount"`
 | |
| 	ExpensesTypeName string         `json:"expenses_type_name"`
 | |
| }
 | |
| 
 | |
| func (q *Queries) StatisticsExpenseByProjectID(ctx context.Context, projectID int64) ([]*StatisticsExpenseByProjectIDRow, error) {
 | |
| 	rows, err := q.db.Query(ctx, statisticsExpenseByProjectID, projectID)
 | |
| 	if err != nil {
 | |
| 		return nil, err
 | |
| 	}
 | |
| 	defer rows.Close()
 | |
| 	items := []*StatisticsExpenseByProjectIDRow{}
 | |
| 	for rows.Next() {
 | |
| 		var i StatisticsExpenseByProjectIDRow
 | |
| 		if err := rows.Scan(&i.TotalAmount, &i.ExpensesTypeName); err != nil {
 | |
| 			return nil, err
 | |
| 		}
 | |
| 		items = append(items, &i)
 | |
| 	}
 | |
| 	if err := rows.Err(); err != nil {
 | |
| 		return nil, err
 | |
| 	}
 | |
| 	return items, nil
 | |
| }
 | |
| 
 | |
| const sumExpense = `-- name: SumExpense :one
 | |
| SELECT SUM(amount)::numeric
 | |
| FROM expenses
 | |
| `
 | |
| 
 | |
| func (q *Queries) SumExpense(ctx context.Context) (pgtype.Numeric, error) {
 | |
| 	row := q.db.QueryRow(ctx, sumExpense)
 | |
| 	var column_1 pgtype.Numeric
 | |
| 	err := row.Scan(&column_1)
 | |
| 	return column_1, err
 | |
| }
 | |
| 
 | |
| const sumExpenseByProjectID = `-- name: SumExpenseByProjectID :one
 | |
| SELECT SUM(amount)::numeric
 | |
| FROM expenses
 | |
| WHERE project_id = $1
 | |
| `
 | |
| 
 | |
| func (q *Queries) SumExpenseByProjectID(ctx context.Context, projectID int64) (pgtype.Numeric, error) {
 | |
| 	row := q.db.QueryRow(ctx, sumExpenseByProjectID, projectID)
 | |
| 	var column_1 pgtype.Numeric
 | |
| 	err := row.Scan(&column_1)
 | |
| 	return column_1, err
 | |
| }
 | |
| 
 | |
| const updateExpense = `-- name: UpdateExpense :one
 | |
| UPDATE expenses
 | |
| SET
 | |
|     project_id = COALESCE($2, project_id),
 | |
|     budget_id = COALESCE($3, budget_id),
 | |
|     amount = COALESCE($4, amount),
 | |
|     expenses_at = COALESCE($5, expenses_at),
 | |
|     expenses_type = COALESCE($6, expenses_type),
 | |
|     remark = COALESCE($7, remark),
 | |
|     status = COALESCE($8, status),
 | |
|     updated_user_id = COALESCE($9, updated_user_id),
 | |
|     updated_at = NOW()
 | |
| WHERE id = $1
 | |
| RETURNING id, project_id, budget_id, amount, expenses_at, expenses_type, remark, status, created_at, created_user_id, updated_at, updated_user_id
 | |
| `
 | |
| 
 | |
| type UpdateExpenseParams struct {
 | |
| 	ID            int64              `json:"id"`
 | |
| 	ProjectID     pgtype.Int8        `json:"project_id"`
 | |
| 	BudgetID      pgtype.Int8        `json:"budget_id"`
 | |
| 	Amount        pgtype.Numeric     `json:"amount"`
 | |
| 	ExpensesAt    pgtype.Timestamptz `json:"expenses_at"`
 | |
| 	ExpensesType  pgtype.Int4        `json:"expenses_type"`
 | |
| 	Remark        pgtype.Text        `json:"remark"`
 | |
| 	Status        pgtype.Int2        `json:"status"`
 | |
| 	UpdatedUserID pgtype.Int4        `json:"updated_user_id"`
 | |
| }
 | |
| 
 | |
| func (q *Queries) UpdateExpense(ctx context.Context, arg *UpdateExpenseParams) (*Expense, error) {
 | |
| 	row := q.db.QueryRow(ctx, updateExpense,
 | |
| 		arg.ID,
 | |
| 		arg.ProjectID,
 | |
| 		arg.BudgetID,
 | |
| 		arg.Amount,
 | |
| 		arg.ExpensesAt,
 | |
| 		arg.ExpensesType,
 | |
| 		arg.Remark,
 | |
| 		arg.Status,
 | |
| 		arg.UpdatedUserID,
 | |
| 	)
 | |
| 	var i Expense
 | |
| 	err := row.Scan(
 | |
| 		&i.ID,
 | |
| 		&i.ProjectID,
 | |
| 		&i.BudgetID,
 | |
| 		&i.Amount,
 | |
| 		&i.ExpensesAt,
 | |
| 		&i.ExpensesType,
 | |
| 		&i.Remark,
 | |
| 		&i.Status,
 | |
| 		&i.CreatedAt,
 | |
| 		&i.CreatedUserID,
 | |
| 		&i.UpdatedAt,
 | |
| 		&i.UpdatedUserID,
 | |
| 	)
 | |
| 	return &i, err
 | |
| }
 |