Skip to content

Commit

Permalink
fix: fallback to default params on index (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
shanehull authored Oct 20, 2024
1 parent 7a8f8c9 commit a803e05
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 159 deletions.
75 changes: 72 additions & 3 deletions internal/handlers/calc.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package handlers
import (
"fmt"
"net/http"
"net/url"
"strconv"

"debtrecyclingcalc.com/internal/calc"
"debtrecyclingcalc.com/internal/charts"
Expand All @@ -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)
Expand Down Expand Up @@ -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
}
76 changes: 70 additions & 6 deletions internal/handlers/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package handlers

import (
"net/http"
"net/url"
"strconv"

"debtrecyclingcalc.com/internal/buildinfo"
"debtrecyclingcalc.com/internal/calc"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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"
}
}
146 changes: 0 additions & 146 deletions internal/handlers/util.go

This file was deleted.

5 changes: 3 additions & 2 deletions internal/templates/form.templ
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ templ Form(params *calc.Parameters) {
<label for="country_selection" class="block text-sm font-medium text-gray-700">
Country
</label>
<select id="country_selection" name="country" autocomplete="on" class="mt-1 block w-full border border-gray-300 rounded-md shadow-sm focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm">
<select id="country_selection" name="country" class="mt-1 block w-full border border-gray-300 rounded-md shadow-sm focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm">
<option value="AU">🇦🇺 Australia</option>
<option value="NZ">🇳🇿 New Zealand</option>
</select>
Expand All @@ -138,7 +138,7 @@ templ Form(params *calc.Parameters) {
<input
type="checkbox"
name="reinvest_dividends"
autocomplete="on"
value="true"
id="reinvest_dividends"
class="h-4 w-4 text-blue-600 border-gray-300 rounded"
if params.ReinvestDividends {
Expand All @@ -151,6 +151,7 @@ templ Form(params *calc.Parameters) {
<input
type="checkbox"
name="reinvest_tax_refunds"
value="true"
id="reinvest_tax_refunds"
class="h-4 w-4 text-blue-600 border-gray-300 rounded"
if params.ReinvestTaxRefunds {
Expand Down
4 changes: 2 additions & 2 deletions internal/templates/form_templ.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit a803e05

Please sign in to comment.