Added analytics by transactions
This commit is contained in:
@@ -15,6 +15,7 @@ type TransactionRepository interface {
|
||||
Create(ctx context.Context, transaction *domain.Transaction) error
|
||||
GetByID(ctx context.Context, id int64) (*domain.Transaction, error)
|
||||
List(ctx context.Context, filter domain.TransactionListFilter) ([]*domain.Transaction, error)
|
||||
Analytics(ctx context.Context, filter domain.TransactionAnalyticsFilter) (domain.TransactionAnalytics, error)
|
||||
Update(ctx context.Context, transaction *domain.Transaction, syncReceipt bool) error
|
||||
Delete(ctx context.Context, id int64) error
|
||||
}
|
||||
@@ -188,6 +189,47 @@ func (r *TransactionsSQLRepository) List(ctx context.Context, filter domain.Tran
|
||||
return transactions, rows.Err()
|
||||
}
|
||||
|
||||
func (r *TransactionsSQLRepository) Analytics(ctx context.Context, filter domain.TransactionAnalyticsFilter) (domain.TransactionAnalytics, error) {
|
||||
var (
|
||||
whereClauses []string
|
||||
args []any
|
||||
)
|
||||
|
||||
appendFilter := func(condition string, value any) {
|
||||
args = append(args, value)
|
||||
whereClauses = append(whereClauses, fmt.Sprintf(condition, len(args)))
|
||||
}
|
||||
|
||||
appendFilter("datetime >= $%d", filter.DateFrom)
|
||||
appendFilter("datetime <= $%d", filter.DateTo)
|
||||
|
||||
if filter.FamilyID != nil {
|
||||
appendFilter("family_id = $%d", *filter.FamilyID)
|
||||
}
|
||||
if filter.Type != nil {
|
||||
appendFilter("type = $%d", *filter.Type)
|
||||
}
|
||||
|
||||
query := `
|
||||
SELECT
|
||||
COALESCE(SUM(CASE WHEN type = 'expense' THEN amount ELSE 0 END), 0),
|
||||
COALESCE(SUM(CASE WHEN type = 'income' THEN amount ELSE 0 END), 0)
|
||||
FROM transactions
|
||||
`
|
||||
|
||||
if len(whereClauses) > 0 {
|
||||
query += " WHERE " + strings.Join(whereClauses, " AND ")
|
||||
}
|
||||
|
||||
var analytics domain.TransactionAnalytics
|
||||
if err := r.db.QueryRowContext(ctx, query, args...).Scan(&analytics.Expenses, &analytics.Incomes); err != nil {
|
||||
return domain.TransactionAnalytics{}, err
|
||||
}
|
||||
analytics.Total = analytics.Incomes - analytics.Expenses
|
||||
|
||||
return analytics, nil
|
||||
}
|
||||
|
||||
func (r *TransactionsSQLRepository) Update(ctx context.Context, transaction *domain.Transaction, syncReceipt bool) error {
|
||||
tx, err := r.db.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user