|
|
@@ -246,44 +246,6 @@ func getLoanType( |
|
|
|
return loans, nil |
|
|
|
} |
|
|
|
|
|
|
|
func getEstimate(db *sql.DB, id int) (Estimate, error) { |
|
|
|
var estimate Estimate |
|
|
|
var err error |
|
|
|
|
|
|
|
query := `SELECT e.id, e.user_id, e.transaction, |
|
|
|
e.price, e.property, e.occupancy, e.zip, e.pud, |
|
|
|
b.id, b.credit_score, b.monthly_income, b.num |
|
|
|
FROM estimate e |
|
|
|
INNER JOIN borrower b ON e.borrower_id = b.id |
|
|
|
WHERE e.id = ? |
|
|
|
` |
|
|
|
// Inner join should always be valid because a borrower is a required |
|
|
|
// foreign key. |
|
|
|
row := db.QueryRow(query, id) |
|
|
|
|
|
|
|
if err = row.Scan( |
|
|
|
&estimate.Id, |
|
|
|
&estimate.User, |
|
|
|
&estimate.Transaction, |
|
|
|
&estimate.Price, |
|
|
|
&estimate.Property, |
|
|
|
&estimate.Occupancy, |
|
|
|
&estimate.Zip, |
|
|
|
&estimate.Pud, |
|
|
|
&estimate.Borrower.Id, |
|
|
|
&estimate.Borrower.Credit, |
|
|
|
&estimate.Borrower.Income, |
|
|
|
&estimate.Borrower.Num, |
|
|
|
) |
|
|
|
err != nil { |
|
|
|
return estimate, fmt.Errorf("Estimate scanning error: %v", err) |
|
|
|
} |
|
|
|
|
|
|
|
estimate.Loans, err = getLoans(db, estimate.Id) |
|
|
|
|
|
|
|
return estimate, err |
|
|
|
} |
|
|
|
|
|
|
|
func getFees(db *sql.DB, loan int) ([]Fee, error) { |
|
|
|
var fees []Fee |
|
|
|
|
|
|
@@ -910,8 +872,6 @@ func queryLoan(db *sql.DB, e int, id int) ( []Loan, error ) { |
|
|
|
var query string |
|
|
|
var rows *sql.Rows |
|
|
|
var err error |
|
|
|
|
|
|
|
fmt.Println(e, id) |
|
|
|
|
|
|
|
query = `SELECT |
|
|
|
l.id, |
|
|
@@ -955,32 +915,33 @@ func queryLoan(db *sql.DB, e int, id int) ( []Loan, error ) { |
|
|
|
} |
|
|
|
loans = append(loans, loan) |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Prevents runtime panics |
|
|
|
if len(loans) == 0 { return loans, errors.New("Loan not found.") } |
|
|
|
|
|
|
|
return loans, nil |
|
|
|
} |
|
|
|
|
|
|
|
func queryEstimate(db *sql.DB, id int) ( []Estimate, error ) { |
|
|
|
func queryEstimate(db *sql.DB, id int, user int) ( []Estimate, error ) { |
|
|
|
var estimates []Estimate |
|
|
|
var query string |
|
|
|
var rows *sql.Rows |
|
|
|
var err error |
|
|
|
|
|
|
|
query = `SELECT |
|
|
|
u.id, |
|
|
|
u.user_id, |
|
|
|
u.borrower_id, |
|
|
|
u.transaction, |
|
|
|
u.price, |
|
|
|
u.property, |
|
|
|
u.occupancy, |
|
|
|
u.zip, |
|
|
|
u.pud |
|
|
|
FROM estimate u WHERE u.id = CASE @e := ? WHEN 0 THEN u.id ELSE @e END |
|
|
|
id, |
|
|
|
user_id, |
|
|
|
borrower_id, |
|
|
|
transaction, |
|
|
|
price, |
|
|
|
property, |
|
|
|
occupancy, |
|
|
|
zip, |
|
|
|
pud |
|
|
|
FROM estimate WHERE id = CASE @e := ? WHEN 0 THEN id ELSE @e END AND |
|
|
|
user_id = CASE @e := ? WHEN 0 THEN user_id ELSE @e END |
|
|
|
` |
|
|
|
rows, err = db.Query(query, id) |
|
|
|
rows, err = db.Query(query, id, user) |
|
|
|
|
|
|
|
|
|
|
|
if err != nil { |
|
|
@@ -1013,7 +974,6 @@ func queryEstimate(db *sql.DB, id int) ( []Estimate, error ) { |
|
|
|
if len(estimates) == 0 { return estimates, errors.New("Estimate not found.") } |
|
|
|
|
|
|
|
for _, e := range estimates { |
|
|
|
fmt.Println("here's the estimate ID:", e.Id) |
|
|
|
e.Loans, err = queryLoan(db, e.Id, 0) |
|
|
|
if err != nil { return estimates, err } |
|
|
|
} |
|
|
@@ -1138,7 +1098,7 @@ func insertEstimate(db *sql.DB, estimate Estimate) (Estimate, error){ |
|
|
|
if err != nil { return estimate, err } |
|
|
|
} |
|
|
|
|
|
|
|
estimates, err := queryEstimate(db, estimate.Id) |
|
|
|
estimates, err := queryEstimate(db, estimate.Id, 0) |
|
|
|
if err != nil { return Estimate{}, err } |
|
|
|
|
|
|
|
return estimates[0], nil |
|
|
@@ -1155,6 +1115,17 @@ func createEstimate(w http.ResponseWriter, db *sql.DB, r *http.Request) { |
|
|
|
json.NewEncoder(w).Encode(estimate) |
|
|
|
} |
|
|
|
|
|
|
|
// Query all estimates that belong to the current user |
|
|
|
func fetchEstimate(w http.ResponseWriter, db *sql.DB, r *http.Request) { |
|
|
|
var estimates []Estimate |
|
|
|
claims, err := getClaims(r) |
|
|
|
|
|
|
|
estimates, err = queryEstimate(db, 0, claims.Id) |
|
|
|
if err != nil { http.Error(w, err.Error(), 500); return } |
|
|
|
|
|
|
|
json.NewEncoder(w).Encode(estimates) |
|
|
|
} |
|
|
|
|
|
|
|
func checkConventional(l Loan, b Borrower) error { |
|
|
|
if b.Credit < 620 { |
|
|
|
return errors.New("Credit score too low for conventional loan") |
|
|
@@ -1315,6 +1286,10 @@ func api(w http.ResponseWriter, r *http.Request) { |
|
|
|
r.Method == http.MethodGet && |
|
|
|
guard(r, 1): |
|
|
|
getFeesTemp(w, db, r) |
|
|
|
case match(p, "/api/estimates", &args) && |
|
|
|
r.Method == http.MethodGet && |
|
|
|
guard(r, 1): |
|
|
|
fetchEstimate(w, db, r) |
|
|
|
case match(p, "/api/estimate", &args) && |
|
|
|
r.Method == http.MethodPost && |
|
|
|
guard(r, 1): |
|
|
|