From 26850a0e7349bbdbfd4ce045576bd6f87ea5ac1a Mon Sep 17 00:00:00 2001 From: Immanuel Onyeka Date: Tue, 25 Jul 2023 16:29:48 -0400 Subject: [PATCH] Also retrieve loan type and borrower in getEstimate --- components/estimates.vue | 9 ++++++- skouter.go | 53 ++++++++++++++++++++++++++++++---------- 2 files changed, 48 insertions(+), 14 deletions(-) diff --git a/components/estimates.vue b/components/estimates.vue index ceb7ed7..f5f0eec 100644 --- a/components/estimates.vue +++ b/components/estimates.vue @@ -33,7 +33,7 @@

Saved Estimates

+@click="() => estimate = e" key="e.id"> {{e.id}} - {{e.property}} - ${{(e.price/100).toLocaleString()}} @@ -49,6 +49,13 @@ ${{(estimate.price / 100).toLocaleString()}} + +
+

{{l.title}}

+ + +
+
diff --git a/skouter.go b/skouter.go index 13c052a..ad14984 100644 --- a/skouter.go +++ b/skouter.go @@ -86,8 +86,8 @@ type LoanType struct { } type Loan struct { - Id int `json:id` - EstimateId int `json:estimate_id` + Id int `json:"id"` + EstimateId int `json:"estimateId"` Type LoanType `json:"type"` Amount int `json:"amount"` Amortization string `json:"amortization"` @@ -118,7 +118,7 @@ type MI struct { } type Result struct { - Id int `json:"loanId"` + Id int `json:"id"` LoanId int `json:"loanId"` LoanPayment int `json:"loanPayment"` TotalMonthly int `json:"totalMonthly"` @@ -257,17 +257,32 @@ func summarize(w http.ResponseWriter, db *sql.DB, r *http.Request) { json.NewEncoder(w).Encode(results) } -func getLoanType( - db *sql.DB, - user int, - branch int, - isUser bool) ([]LoanType, error) { +func getLoanType( db *sql.DB, id int) (LoanType, error) { + types, err := getLoanTypes(db, id, 0, 0) + if err != nil { return LoanType{Id: id}, err } + if len(types) == 0 { + return LoanType{Id: id}, errors.New("No type with that id") + } + + return types[0], nil +} + +func getLoanTypes( db *sql.DB, id int, user int, branch int ) ( +[]LoanType, error) { var loans []LoanType + var query = `SELECT + id, + user_id, + branch_id, + name + FROM loan_type WHERE loan_type.id = CASE @e := ? WHEN 0 THEN id ELSE @e END + OR + loan_type.user_id = CASE @e := ? WHEN 0 THEN id ELSE @e END + OR + loan_type.branch_id = CASE @e := ? WHEN 0 THEN id ELSE @e END` // Should be changed to specify user - rows, err := - db.Query(`SELECT * FROM loan_type WHERE user_id = ? AND branch_id = ? ` + - "OR (user_id = 0 AND branch_id = 0)", user, branch) + rows, err := db.Query(query, id, user, branch) if err != nil { return nil, fmt.Errorf("loan_type error: %v", err) @@ -970,6 +985,7 @@ func getLoans(db *sql.DB, e int, id int) ( []Loan, error ) { query = `SELECT l.id, + l.type_id, l.estimate_id, l.amount, l.term, @@ -995,6 +1011,7 @@ func getLoans(db *sql.DB, e int, id int) ( []Loan, error ) { if err := rows.Scan( &loan.Id, + &loan.Type.Id, &loan.EstimateId, &loan.Amount, &loan.Term, @@ -1020,6 +1037,11 @@ func getLoans(db *sql.DB, e int, id int) ( []Loan, error ) { } loan.Fees = fees + loan.Type, err = getLoanType(db, loan.Type.Id) + if err != nil { + return loans, err + } + loans = append(loans, loan) } @@ -1074,12 +1096,17 @@ func getEstimates(db *sql.DB, id int, user int) ( []Estimate, error ) { return estimates, err } - results, err := getResults(db, estimate.Id, 0) + borrower, err := getBorrower(db, estimate.Borrower.Id) + if err != nil { + return estimates, err + } + estimate.Borrower = borrower + + estimate.Results, err = getResults(db, estimate.Id, 0) if err != nil { return estimates, err } - estimate.Results = results estimates = append(estimates, estimate) }