|
|
@@ -321,14 +321,15 @@ func match(path, pattern string, args *[]string) bool { |
|
|
|
return true |
|
|
|
} |
|
|
|
|
|
|
|
func makeResults(estimate Estimate) ([]Result) { |
|
|
|
func (estimate *Estimate) makeResults() []Result { |
|
|
|
var results []Result |
|
|
|
amortize := func(principle float64, rate float64, periods float64) int { |
|
|
|
exp := math.Pow(1+rate, periods) |
|
|
|
return int(principle * rate * exp / (exp - 1)) |
|
|
|
} |
|
|
|
|
|
|
|
for _, loan := range estimate.Loans { |
|
|
|
for i := range estimate.Loans { |
|
|
|
var loan = &estimate.Loans[i] |
|
|
|
var result Result |
|
|
|
// Monthly payments use amortized loan payment formula plus monthly MI, |
|
|
|
// plus all other recurring fees |
|
|
@@ -353,6 +354,7 @@ func makeResults(estimate Estimate) ([]Result) { |
|
|
|
result.TotalFees + result.TotalCredits + (estimate.Price - loan.Amount) |
|
|
|
result.LoanId = loan.Id |
|
|
|
|
|
|
|
loan.Result = result |
|
|
|
results = append(results, result) |
|
|
|
} |
|
|
|
|
|
|
@@ -363,7 +365,7 @@ func summarize(w http.ResponseWriter, db *sql.DB, r *http.Request) { |
|
|
|
var estimate Estimate |
|
|
|
err := json.NewDecoder(r.Body).Decode(&estimate) |
|
|
|
if err != nil { http.Error(w, "Invalid estimate.", 422); return } |
|
|
|
results := makeResults(estimate) |
|
|
|
results := estimate.makeResults() |
|
|
|
|
|
|
|
json.NewEncoder(w).Encode(results) |
|
|
|
} |
|
|
@@ -1129,15 +1131,11 @@ func queryUsers(db *sql.DB, id int) ( []User, error ) { |
|
|
|
return users, nil |
|
|
|
} |
|
|
|
|
|
|
|
func insertResults(db *sql.DB, estimate Estimate) (error){ |
|
|
|
func (estimate *Estimate) insertResults(db *sql.DB) (error){ |
|
|
|
var query string |
|
|
|
var row *sql.Row |
|
|
|
var err error |
|
|
|
var results []Result |
|
|
|
|
|
|
|
for i := range estimate.Loans { |
|
|
|
results = append(results, estimate.Loans[i].Result) |
|
|
|
} |
|
|
|
var id int |
|
|
|
|
|
|
|
query = `INSERT INTO estimate_result |
|
|
|
( |
|
|
@@ -1148,20 +1146,24 @@ func insertResults(db *sql.DB, estimate Estimate) (error){ |
|
|
|
total_credits, |
|
|
|
cash_to_close |
|
|
|
) |
|
|
|
VALUES (?, ?, ?, ?, ?, ?, ?) |
|
|
|
VALUES (?, ?, ?, ?, ?, ?) |
|
|
|
RETURNING id |
|
|
|
` |
|
|
|
for i := range results { |
|
|
|
db.QueryRow(query, |
|
|
|
results[i].LoanId, |
|
|
|
results[i].LoanPayment, |
|
|
|
results[i].TotalMonthly, |
|
|
|
results[i].TotalFees, |
|
|
|
results[i].TotalCredits, |
|
|
|
results[i].CashToClose, |
|
|
|
for i := range estimate.Loans { |
|
|
|
r := estimate.Loans[i].Result |
|
|
|
r.LoanId = estimate.Loans[i].Id |
|
|
|
|
|
|
|
row = db.QueryRow(query, |
|
|
|
r.LoanId, |
|
|
|
r.LoanPayment, |
|
|
|
r.TotalMonthly, |
|
|
|
r.TotalFees, |
|
|
|
r.TotalCredits, |
|
|
|
r.CashToClose, |
|
|
|
) |
|
|
|
err = row.Scan(&results[i].Id) |
|
|
|
err = row.Scan(&id) |
|
|
|
if err != nil { return err } |
|
|
|
r.Id = id |
|
|
|
} |
|
|
|
|
|
|
|
return nil |
|
|
@@ -1866,7 +1868,7 @@ func insertLoanType(db *sql.DB, lt LoanType) (int, error) { |
|
|
|
return id, nil |
|
|
|
} |
|
|
|
|
|
|
|
func insertLoan(db *sql.DB, loan Loan) (Loan, error){ |
|
|
|
func (loan *Loan) insertLoan(db *sql.DB) error { |
|
|
|
var query string |
|
|
|
var row *sql.Row |
|
|
|
var err error |
|
|
@@ -1901,27 +1903,27 @@ func insertLoan(db *sql.DB, loan Loan) (Loan, error){ |
|
|
|
) |
|
|
|
|
|
|
|
err = row.Scan(&loan.Id) |
|
|
|
if err != nil { return loan, err } |
|
|
|
if err != nil { return err } |
|
|
|
_, err = insertMi(db, loan.Mi) |
|
|
|
if err != nil { return loan, err } |
|
|
|
if err != nil { return err } |
|
|
|
for i := range loan.Fees { |
|
|
|
loan.Fees[i].LoanId = loan.Id |
|
|
|
_, err := insertFee(db, loan.Fees[i]) |
|
|
|
if err != nil { return loan, err } |
|
|
|
if err != nil { return err } |
|
|
|
} |
|
|
|
|
|
|
|
return loan, nil |
|
|
|
return nil |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func insertEstimate(db *sql.DB, estimate Estimate) (Estimate, error){ |
|
|
|
func (estimate *Estimate) insertEstimate(db *sql.DB) (error){ |
|
|
|
var query string |
|
|
|
var row *sql.Row |
|
|
|
var err error |
|
|
|
// var id int // Inserted estimate's id |
|
|
|
|
|
|
|
estimate.Borrower.Id, err = insertBorrower(db, estimate.Borrower) |
|
|
|
if err != nil { return Estimate{}, err } |
|
|
|
if err != nil { return err } |
|
|
|
|
|
|
|
query = `INSERT INTO estimate |
|
|
|
( |
|
|
@@ -1949,15 +1951,15 @@ func insertEstimate(db *sql.DB, estimate Estimate) (Estimate, error){ |
|
|
|
) |
|
|
|
|
|
|
|
err = row.Scan(&estimate.Id) |
|
|
|
if err != nil { return Estimate{}, err } |
|
|
|
if err != nil { return err } |
|
|
|
|
|
|
|
for _, l := range estimate.Loans { |
|
|
|
l.EstimateId = estimate.Id |
|
|
|
_, err = insertLoan(db, l) |
|
|
|
if err != nil { return estimate, err } |
|
|
|
for i := range estimate.Loans { |
|
|
|
estimate.Loans[i].EstimateId = estimate.Id |
|
|
|
err = estimate.Loans[i].insertLoan(db) |
|
|
|
if err != nil { return err } |
|
|
|
} |
|
|
|
|
|
|
|
return estimate, nil |
|
|
|
return nil |
|
|
|
} |
|
|
|
|
|
|
|
func createEstimate(w http.ResponseWriter, db *sql.DB, r *http.Request) { |
|
|
@@ -1967,10 +1969,10 @@ func createEstimate(w http.ResponseWriter, db *sql.DB, r *http.Request) { |
|
|
|
claims, err := getClaims(r) |
|
|
|
estimate.User = claims.Id |
|
|
|
|
|
|
|
estimate, err = insertEstimate(db, estimate) |
|
|
|
err = estimate.insertEstimate(db) |
|
|
|
if err != nil { http.Error(w, err.Error(), 422); return } |
|
|
|
makeResults(estimate) |
|
|
|
err = insertResults(db, estimate) |
|
|
|
estimate.makeResults() |
|
|
|
err = estimate.insertResults(db) |
|
|
|
if err != nil { http.Error(w, err.Error(), 500); return } |
|
|
|
e, err := getEstimates(db, estimate.Id, 0) |
|
|
|
if err != nil { http.Error(w, err.Error(), 500); return } |
|
|
@@ -2568,7 +2570,7 @@ func seedEstimates(db *sql.DB, users []User, ltypes []LoanType) []Estimate { |
|
|
|
var l Loan |
|
|
|
var err error |
|
|
|
|
|
|
|
for i := 0; i < 20; i++ { |
|
|
|
for i := 0; i < 15; i++ { |
|
|
|
estimate.User = users[gofakeit.Number(0, len(users) - 1)].Id |
|
|
|
estimate.Borrower = Borrower{ |
|
|
|
Credit: gofakeit.Number(600, 800), |
|
|
@@ -2599,23 +2601,36 @@ func seedEstimates(db *sql.DB, users []User, ltypes []LoanType) []Estimate { |
|
|
|
l.Name = gofakeit.AdjectiveDescriptive() |
|
|
|
estimate.Loans = append(estimate.Loans, l) |
|
|
|
} |
|
|
|
estimate, err = insertEstimate(db, estimate) |
|
|
|
|
|
|
|
err = estimate.insertEstimate(db) |
|
|
|
if err != nil {log.Println(err); return estimates} |
|
|
|
|
|
|
|
estimates = append(estimates, estimate) |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return estimates |
|
|
|
} |
|
|
|
|
|
|
|
func seedResults(db *sql.DB, estimates []Estimate) error { |
|
|
|
var err error |
|
|
|
|
|
|
|
for i := range estimates { |
|
|
|
estimates[i].makeResults() |
|
|
|
err = estimates[i].insertResults(db) |
|
|
|
if err != nil {log.Println(err); return err} |
|
|
|
} |
|
|
|
|
|
|
|
return nil |
|
|
|
} |
|
|
|
|
|
|
|
func dbSeed(db *sql.DB) { |
|
|
|
addresses := seedAddresses(db) |
|
|
|
branches := seedBranches(db, addresses) |
|
|
|
users := seedUsers(db, addresses, branches) |
|
|
|
_ = seedLicenses(db, users) |
|
|
|
loantypes := seedLoanTypes(db) |
|
|
|
log.Println(loantypes) |
|
|
|
estimates := seedEstimates(db, users, loantypes) |
|
|
|
log.Println(estimates) |
|
|
|
_ = seedResults(db, estimates) |
|
|
|
} |
|
|
|
|
|
|
|
func dev(args []string) { |
|
|
@@ -2650,6 +2665,8 @@ func dev(args []string) { |
|
|
|
default: |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
db.Close() |
|
|
|
} |
|
|
|
|
|
|
|
func check(args []string) { |
|
|
|