diff --git a/internal/handlers/calc.go b/internal/handlers/calc.go index 0df331c..b45a483 100644 --- a/internal/handlers/calc.go +++ b/internal/handlers/calc.go @@ -3,6 +3,8 @@ package handlers import ( "fmt" "net/http" + "net/url" + "strconv" "debtrecyclingcalc.com/internal/calc" "debtrecyclingcalc.com/internal/charts" @@ -21,13 +23,12 @@ func CalcHandler(w http.ResponseWriter, r *http.Request) { return } - params, err := getFormParams(r) + params, err := getParamsFromForm(r.Form) if err != nil { - http.Error(w, err.Error(), http.StatusBadRequest) + http.Error(w, "error parsing form params", http.StatusBadRequest) return } - // if params is empty respond with error data, err := calc.DebtRecycling(*params) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) @@ -84,3 +85,71 @@ func CalcHandler(w http.ResponseWriter, r *http.Request) { http.Error(w, err.Error(), http.StatusInternalServerError) } } + +func getParamsFromForm(form url.Values) (*calc.Parameters, error) { + parseFloat := func(key string) (float64, error) { + return strconv.ParseFloat(form.Get(key), 64) + } + + parseInt := func(key string) (int, error) { + return strconv.Atoi(form.Get(key)) + } + + salary, err := parseFloat("salary") + if err != nil { + return nil, fmt.Errorf("error parsing salary: %w", err) + } + + initialInvestmentAmount, err := parseFloat("initial_investment") + if err != nil { + return nil, fmt.Errorf("error parsing initial investment amount: %w", err) + } + + annualInvestmentAmount, err := parseFloat("annual_investment") + if err != nil { + return nil, fmt.Errorf("error parsing annual investment amount: %w", err) + } + + mortgageSize, err := parseFloat("mortgage_size") + if err != nil { + return nil, fmt.Errorf("error parsing mortgage size: %w", err) + } + + mortgageInterestRate, err := parseFloat("mortgage_interest_rate") + if err != nil { + return nil, fmt.Errorf("error parsing mortgage interest rate: %w", err) + } + + dividendReturnRate, err := parseFloat("dividend_return_rate") + if err != nil { + return nil, fmt.Errorf("error parsing dividend return rate: %w", err) + } + + capitalGrowthRate, err := parseFloat("capital_growth_rate") + if err != nil { + return nil, fmt.Errorf("error parsing capital growth rate: %w", err) + } + + years, err := parseInt("years") + if err != nil { + return nil, fmt.Errorf("error parsing years: %w", err) + } + + country := form.Get("country") + reinvestDividends := form.Get("reinvest_dividends") == "true" + reinvestTaxRefunds := form.Get("reinvest_tax_refunds") == "true" + + return &calc.Parameters{ + Salary: salary, + InitialInvestment: initialInvestmentAmount, + AnnualInvestment: annualInvestmentAmount, + MortgageSize: mortgageSize, + MortgageInterestRate: mortgageInterestRate / 100, + DividendReturnRate: dividendReturnRate / 100, + CapitalGrowthRate: capitalGrowthRate / 100, + NumYears: years, + Country: country, + ReinvestDividends: reinvestDividends, + ReinvestTaxRefunds: reinvestTaxRefunds, + }, nil +} diff --git a/internal/handlers/index.go b/internal/handlers/index.go index 11fc4c8..2cd79d7 100644 --- a/internal/handlers/index.go +++ b/internal/handlers/index.go @@ -2,6 +2,8 @@ package handlers import ( "net/http" + "net/url" + "strconv" "debtrecyclingcalc.com/internal/buildinfo" "debtrecyclingcalc.com/internal/calc" @@ -43,13 +45,8 @@ func IndexHandler(w http.ResponseWriter, r *http.Request) { } query := r.URL.Query() - var err error if len(query) != 0 { - params, err = getQueryParams(query) - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } + setParamsFromQuery(params, query) } data, err := calc.DebtRecycling(*params) @@ -88,3 +85,70 @@ func IndexHandler(w http.ResponseWriter, r *http.Request) { http.Error(w, err.Error(), http.StatusInternalServerError) } } + +func setParamsFromQuery(params *calc.Parameters, query url.Values) { + parseFloat := func(key string) (float64, error) { + if val := query.Get(key); val != "" { + return strconv.ParseFloat(val, 64) + } + return 0, nil // Return zero but no error if key is absent + } + + parseInt := func(key string) (int, error) { + if val := query.Get(key); val != "" { + return strconv.Atoi(val) + } + return 0, nil // Return zero but no error if key is absent + } + + // Update fields if the corresponding query parameter is present + if salary, err := parseFloat("salary"); err == nil && salary != 0 { + params.Salary = salary + } + + if initialInvestmentAmount, err := parseFloat("initial_investment"); err == nil && + initialInvestmentAmount != 0 { + params.InitialInvestment = initialInvestmentAmount + } + + if annualInvestmentAmount, err := parseFloat("annual_investment"); err == nil && + annualInvestmentAmount != 0 { + params.AnnualInvestment = annualInvestmentAmount + } + + if mortgageSize, err := parseFloat("mortgage_size"); err == nil && mortgageSize != 0 { + params.MortgageSize = mortgageSize + } + + if mortgageInterestRate, err := parseFloat("mortgage_interest_rate"); err == nil && + mortgageInterestRate != 0 { + params.MortgageInterestRate = mortgageInterestRate / 100 + } + + if dividendReturnRate, err := parseFloat("dividend_return_rate"); err == nil && + dividendReturnRate != 0 { + params.DividendReturnRate = dividendReturnRate / 100 + } + + if capitalGrowthRate, err := parseFloat("capital_growth_rate"); err == nil && + capitalGrowthRate != 0 { + params.CapitalGrowthRate = capitalGrowthRate / 100 + } + + if years, err := parseInt("years"); err == nil && years != 0 { + params.NumYears = years + } + + // Simple string params + if country := query.Get("country"); country != "" { + params.Country = country + } + + // Checkboxes for reinvestment toggles: maintain defaults if not present + if query["reinvest_dividends"] != nil { + params.ReinvestDividends = query.Get("reinvest_dividends") == "true" + } + if query["reinvest_tax_refunds"] != nil { + params.ReinvestTaxRefunds = query.Get("reinvest_tax_refunds") == "true" + } +} diff --git a/internal/handlers/util.go b/internal/handlers/util.go deleted file mode 100644 index a1e44fd..0000000 --- a/internal/handlers/util.go +++ /dev/null @@ -1,146 +0,0 @@ -package handlers - -import ( - "fmt" - "net/http" - "net/url" - "strconv" - - "debtrecyclingcalc.com/internal/calc" -) - -func getFormParams(r *http.Request) (*calc.Parameters, error) { - parseFloat := func(key string) (float64, error) { - return strconv.ParseFloat(r.Form.Get(key), 64) - } - - parseInt := func(key string) (int, error) { - return strconv.Atoi(r.Form.Get(key)) - } - - salary, err := parseFloat("salary") - if err != nil { - return nil, fmt.Errorf("error parsing salary: %w", err) - } - - initialInvestmentAmount, err := parseFloat("initial_investment") - if err != nil { - return nil, fmt.Errorf("error parsing initial investment amount: %w", err) - } - - annualInvestmentAmount, err := parseFloat("annual_investment") - if err != nil { - return nil, fmt.Errorf("error parsing annual investment amount: %w", err) - } - - mortgageSize, err := parseFloat("mortgage_size") - if err != nil { - return nil, fmt.Errorf("error parsing mortgage size: %w", err) - } - - mortgageInterestRate, err := parseFloat("mortgage_interest_rate") - if err != nil { - return nil, fmt.Errorf("error parsing mortgage interest rate: %w", err) - } - - dividendReturnRate, err := parseFloat("dividend_return_rate") - if err != nil { - return nil, fmt.Errorf("error parsing dividend return rate: %w", err) - } - - capitalGrowthRate, err := parseFloat("capital_growth_rate") - if err != nil { - return nil, fmt.Errorf("error parsing capital growth rate: %w", err) - } - - years, err := parseInt("years") - if err != nil { - return nil, fmt.Errorf("error parsing years: %w", err) - } - - country := r.Form.Get("country") - reinvestDividends := r.Form.Get("reinvest_dividends") == "on" - reinvestTaxRefunds := r.Form.Get("reinvest_tax_refunds") == "on" - - return &calc.Parameters{ - Salary: salary, - InitialInvestment: initialInvestmentAmount, - AnnualInvestment: annualInvestmentAmount, - MortgageSize: mortgageSize, - MortgageInterestRate: mortgageInterestRate / 100, - DividendReturnRate: dividendReturnRate / 100, - CapitalGrowthRate: capitalGrowthRate / 100, - NumYears: years, - Country: country, - ReinvestDividends: reinvestDividends, - ReinvestTaxRefunds: reinvestTaxRefunds, - }, nil -} - -func getQueryParams(query url.Values) (*calc.Parameters, error) { - parseFloat := func(key string) (float64, error) { - return strconv.ParseFloat(query.Get(key), 64) - } - - parseInt := func(key string) (int, error) { - return strconv.Atoi(query.Get(key)) - } - - salary, err := parseFloat("salary") - if err != nil { - return nil, fmt.Errorf("error parsing salary: %w", err) - } - - initialInvestmentAmount, err := parseFloat("initial_investment") - if err != nil { - return nil, fmt.Errorf("error parsing initial investment amount: %w", err) - } - - annualInvestmentAmount, err := parseFloat("annual_investment") - if err != nil { - return nil, fmt.Errorf("error parsing annual investment amount: %w", err) - } - - mortgageSize, err := parseFloat("mortgage_size") - if err != nil { - return nil, fmt.Errorf("error parsing mortgage size: %w", err) - } - - mortgageInterestRate, err := parseFloat("mortgage_interest_rate") - if err != nil { - return nil, fmt.Errorf("error parsing mortgage interest rate: %w", err) - } - - dividendReturnRate, err := parseFloat("dividend_return_rate") - if err != nil { - return nil, fmt.Errorf("error parsing dividend return rate: %w", err) - } - - capitalGrowthRate, err := parseFloat("capital_growth_rate") - if err != nil { - return nil, fmt.Errorf("error parsing capital growth rate: %w", err) - } - - years, err := parseInt("years") - if err != nil { - return nil, fmt.Errorf("error parsing years: %w", err) - } - - country := query.Get("country") - reinvestDividends := query.Get("reinvest_dividends") == "true" - reinvestTaxRefunds := query.Get("reinvest_tax_refunds") == "true" - - return &calc.Parameters{ - Salary: salary, - InitialInvestment: initialInvestmentAmount, - AnnualInvestment: annualInvestmentAmount, - MortgageSize: mortgageSize, - MortgageInterestRate: mortgageInterestRate / 100, - DividendReturnRate: dividendReturnRate / 100, - CapitalGrowthRate: capitalGrowthRate / 100, - NumYears: years, - Country: country, - ReinvestDividends: reinvestDividends, - ReinvestTaxRefunds: reinvestTaxRefunds, - }, nil -} diff --git a/internal/templates/form.templ b/internal/templates/form.templ index 623da38..3de75a6 100644 --- a/internal/templates/form.templ +++ b/internal/templates/form.templ @@ -129,7 +129,7 @@ templ Form(params *calc.Parameters) { Country - + 🇦🇺 Australia 🇳🇿 New Zealand @@ -138,7 +138,7 @@ templ Form(params *calc.Parameters) { Country 🇦🇺 Australia 🇳🇿 New ZealandCountry 🇦🇺 Australia 🇳🇿 New Zealand Reinvest Dividends Reinvest Dividends