chore: initial commit of HouseholdBudgetBook (backend, frontend, desktop)
This commit is contained in:
commit
4a5db3f1f9
167 files changed
+21732
No files matched your search
@@ -0,0 +1,107 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"sort"
|
||||
|
||||
"household-book/model"
|
||||
)
|
||||
|
||||
type DashboardService struct {
|
||||
db *sql.DB
|
||||
}
|
||||
|
||||
func NewDashboardService(db *sql.DB) *DashboardService {
|
||||
return &DashboardService{db: db}
|
||||
}
|
||||
|
||||
func (s *DashboardService) Summary(ctx context.Context, year, month string) (*model.DashboardSummary, error) {
|
||||
var result model.DashboardSummary
|
||||
prefix := ""
|
||||
if year != "" {
|
||||
prefix = year
|
||||
if month != "" {
|
||||
if len(month) == 1 {
|
||||
month = "0" + month
|
||||
}
|
||||
prefix += "-" + month
|
||||
}
|
||||
}
|
||||
|
||||
query := `SELECT
|
||||
COALESCE(SUM(CASE WHEN type = 'income' THEN amount ELSE 0 END), 0),
|
||||
COALESCE(SUM(CASE WHEN type = 'expense' THEN amount ELSE 0 END), 0),
|
||||
COUNT(*)
|
||||
FROM transactions
|
||||
WHERE type IN ('income', 'expense')`
|
||||
|
||||
var args []any
|
||||
if prefix != "" {
|
||||
query += " AND date LIKE ?"
|
||||
args = append(args, prefix+"%")
|
||||
}
|
||||
|
||||
if err := s.db.QueryRowContext(ctx, query, args...).Scan(
|
||||
&result.Income, &result.Expense, &result.TransactionCount,
|
||||
); err != nil {
|
||||
return nil, fmt.Errorf("query dashboard summary: %w", err)
|
||||
}
|
||||
result.Balance = result.Income - result.Expense
|
||||
return &result, nil
|
||||
}
|
||||
|
||||
func (s *DashboardService) Recent(ctx context.Context, limit int) ([]model.TransactionListItem, error) {
|
||||
if limit < 1 {
|
||||
limit = 5
|
||||
}
|
||||
|
||||
rows, err := s.db.QueryContext(ctx,
|
||||
`SELECT t.id, t.type, t.amount, t.category_id, COALESCE(c.name,''), COALESCE(c.is_active, 1),
|
||||
t.account_id, COALESCE(a.name,''), t.to_account_id, COALESCE(ta.name,''),
|
||||
t.member_id, COALESCE(m.name,''),
|
||||
t.date, t.description, COALESCE(t.counterparty,''), t.import_batch_id, COALESCE(ib.filename,''),
|
||||
t.created_at, t.updated_at
|
||||
FROM transactions t
|
||||
LEFT JOIN categories c ON t.category_id = c.id
|
||||
LEFT JOIN accounts a ON t.account_id = a.id
|
||||
LEFT JOIN accounts ta ON t.to_account_id = ta.id
|
||||
LEFT JOIN members m ON t.member_id = m.id
|
||||
LEFT JOIN import_batches ib ON t.import_batch_id = ib.id
|
||||
ORDER BY t.date DESC, t.id DESC LIMIT ?`, limit)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("query recent transactions: %w", err)
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var items []model.TransactionListItem
|
||||
for rows.Next() {
|
||||
var item model.TransactionListItem
|
||||
var categoryIsActive int
|
||||
if err := rows.Scan(
|
||||
&item.ID, &item.Type, &item.Amount,
|
||||
&item.CategoryID, &item.CategoryName, &categoryIsActive,
|
||||
&item.AccountID, &item.AccountName,
|
||||
&item.ToAccountID, &item.ToAccountName,
|
||||
&item.MemberID, &item.MemberName,
|
||||
&item.Date, &item.Description, &item.Counterparty,
|
||||
&item.ImportBatchID, &item.ImportFilename,
|
||||
&item.CreatedAt, &item.UpdatedAt,
|
||||
); err != nil {
|
||||
return nil, fmt.Errorf("scan transaction: %w", err)
|
||||
}
|
||||
item.CategoryIsActive = categoryIsActive == 1
|
||||
items = append(items, item)
|
||||
}
|
||||
if items == nil {
|
||||
items = []model.TransactionListItem{}
|
||||
}
|
||||
sort.SliceStable(items, func(i, j int) bool {
|
||||
if items[i].Date == items[j].Date {
|
||||
return items[i].ID < items[j].ID
|
||||
}
|
||||
return items[i].Date < items[j].Date
|
||||
})
|
||||
return items, rows.Err()
|
||||
}
|
||||
Reference in new issue
Block a user