|
|
@@ -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) |
|
|
|
} |
|
|
|
|
|
|
|