package service import ( "context" "spend-sparrow/internal/types" "time" "github.com/jmoiron/sqlx" ) type Dashboard struct { db *sqlx.DB } func NewDashboard(db *sqlx.DB) TreasureChest { return TreasureChestImpl{ db: db, } } func (s Dashboard) Summary(ctx context.Context, user *types.User, month time.Time) (*types.DashboardSummary, error) { if user == nil { return nil, ErrUnauthorized } var summary types.DashboardSummary s.db.SelectContext(ctx, &summary.Expenses, ` SELECT FROM "transaction" WHERE user_id = $1 AND value < 0 AND account_id IS NOT NULL AND error IS NULL AND date_trunc('month', date) = date_trund('month', $2)`, user.Id, month) s.db.SelectContext(ctx, &summary.Income, ` SELECT FROM "transaction" WHERE user_id = $1 AND value > 0 AND account_id IS NOT NULL AND treasure_chest_id IS NULL AND error IS NULL AND date_trunc('month', date) = date_trund('month', $2)`, user.Id, month) s.db.SelectContext(ctx, &summary.Savings, ` SELECT FROM "transaction" WHERE user_id = $1 AND value > 0 AND treasure_chest_id IS NOT NULL AND account_id IS NULL AND error IS NULL AND date_trunc('month', date) = date_trund('month', $2)`, user.Id, month) return &summary, nil }