diff --git a/migrations/0_29092022_create_main_tables.sql b/migrations/0_29092022_create_main_tables.sql index 076600b..aae9972 100644 --- a/migrations/0_29092022_create_main_tables.sql +++ b/migrations/0_29092022_create_main_tables.sql @@ -86,6 +86,8 @@ CREATE TABLE loan ( amount INT NOT NULL, term INT, /* In years */ interest INT, /* Per year, precise to 2 decimals */ + ltv INT DEFAULT 0, + dti INT DEFAULT 0, hoi INT DEFAULT 0, /* Hazard insurance annual payments */ mi_name VARCHAR(50) NOT NULL, /* Mortgage insurance title shown in menu */ diff --git a/migrations/seed.sql b/migrations/seed.sql index c75bb59..6d4853a 100644 --- a/migrations/seed.sql +++ b/migrations/seed.sql @@ -219,6 +219,8 @@ INSERT INTO loan ( amount, term, interest, + ltv, + dti, hoi, mi_name, mi_amount, @@ -232,6 +234,8 @@ INSERT INTO loan ( 30, 375, 10000, + 0, + 0, "custom mi", 234000, "National MI", @@ -244,6 +248,8 @@ INSERT INTO loan ( 30, 300, 10000, + 0, + 0, "maybe MGIC", 234000, "MGIC", @@ -256,6 +262,8 @@ INSERT INTO loan ( 10, 125, 15000, + 0, + 0, "custom mi", 234000, "another lender", diff --git a/skouter.go b/skouter.go index 76a5e29..11e2403 100644 --- a/skouter.go +++ b/skouter.go @@ -182,31 +182,40 @@ func getLoanType( 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( - "SELECT * FROM estimate "+ - "WHERE id = ? " + - "INNER JOIN borrower ON estimate.borrower = borrower.id", - id) + row := db.QueryRow(query, id) - if err := row.Scan( + if err = row.Scan( &estimate.Id, &estimate.User, - &estimate.Borrower.Id, &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) } - return estimate, nil + estimate.Loans, err = getLoans(db, estimate.Id) + + return estimate, err } func getFees(db *sql.DB, loan int) ([]Fee, error) { @@ -282,10 +291,6 @@ func getFeesTemp(db *sql.DB, user int) ([]FeeTemplate, error) { fees = append(fees, fee) } - est, err := getEstimate(db, 1) - fmt.Printf("the estimate: %v,\nthe error %v\n", est, err) - // getMi(db, getEstimate(db, 1), getBorrower(db, 1)) - return fees, nil } @@ -293,11 +298,10 @@ func getLoans(db *sql.DB, estimate int) ([]Loan, error) { var loans []Loan query := `SELECT - loan.id, loan.amount, loan.term, loan.interest, loan.ltv, loan.dti, - loan.hoi, loan.mi_name, loan.mi_amount - loan_type.id, loan_type.user_id, loan_type.branch_id, loan_type.name - FROM loan INNER JOIN loan_type ON loan.type_id = loan_type(id) - WHERE loan.estimate_id = ? + l.id, l.amount, l.term, l.interest, l.ltv, l.dti, l.hoi, l.mi_name, + l.mi_amount, lt.id, lt.user_id, lt.branch_id, lt.name + FROM loan l INNER JOIN loan_type lt ON l.type_id = lt.id + WHERE l.estimate_id = ? ` rows, err := db.Query(query, estimate) @@ -332,10 +336,6 @@ func getLoans(db *sql.DB, estimate int) ([]Loan, error) { loans = append(loans, loan) } - est, err := getEstimate(db, 1) - fmt.Printf("the loan: %v,\nthe error %v\n", est, err) - // getMi(db, getEstimate(db, 1), getBorrower(db, 1)) - return loans, nil } @@ -360,7 +360,7 @@ func getBorrower(db *sql.DB, id int) (Borrower, error) { return borrower, nil } -func getMi(db *sql.DB, estimate Estimate, borrower Borrower) { +func getMi(db *sql.DB, estimate *Estimate) { // body := map[string]string{ // "zipCode": estimate.Zip, // // "stateCode": "CA", @@ -420,13 +420,13 @@ func getMi(db *sql.DB, estimate Estimate, borrower Borrower) { "productCode": "BPM", "renewalTypeCode": "CON", "numberOfBorrowers": 1, - "borrowerCreditScore": strconv.Itoa(borrower.Credit), + "borrowerCreditScore": strconv.Itoa(estimate.Borrower.Credit), "masterPolicy": nil, "selfEmployedIndicator": false, "armType": "", "userId": 44504, }) - log.Println(bytes.NewBuffer(body)) + log.Println("the bytes: %v", bytes.NewBuffer(body)) } @@ -459,6 +459,7 @@ func route(w http.ResponseWriter, r *http.Request) { func api(w http.ResponseWriter, r *http.Request) { var args []string // var response string + p := r.URL.Path db, err := sql.Open("mysql", fmt.Sprintf("%s:%s@tcp(127.0.0.1:3306)/skouter", @@ -492,6 +493,24 @@ func api(w http.ResponseWriter, r *http.Request) { } else { json.NewEncoder(w).Encode(err) } + + case match(p, "/api/mi", &args): + var err error + est, err := getEstimate(db, 1) + if err != nil { + json.NewEncoder(w).Encode(err) + log.Println("error occured:", err) + break + } + + getMi(db, &est) + + // if err != nil { + // json.NewEncoder(w).Encode(err) + // break + // } else { + // json.NewEncoder(w).Encode(resp) + // } } }