소스 검색

Fix loan query and its errors

master
Immanuel Onyeka 2 년 전
부모
커밋
ae79dbacdd
3개의 변경된 파일73개의 추가작업 그리고 58개의 파일을 삭제
  1. +2
    -2
      migrations/0_29092022_create_main_tables.sql
  2. +40
    -31
      migrations/seed.sql
  3. +31
    -25
      skouter.go

+ 2
- 2
migrations/0_29092022_create_main_tables.sql 파일 보기

@@ -80,7 +80,7 @@ CREATE TABLE estimate (
); );


CREATE TABLE loan ( CREATE TABLE loan (
id INT,
id INT AUTO_INCREMENT,
estimate_id INT NOT NULL, estimate_id INT NOT NULL,
type_id INT NOT NULL, type_id INT NOT NULL,
amount INT NOT NULL, amount INT NOT NULL,
@@ -94,7 +94,7 @@ CREATE TABLE loan (
name VARCHAR(30) DEFAULT '', name VARCHAR(30) DEFAULT '',
PRIMARY KEY (`id`), PRIMARY KEY (`id`),
FOREIGN KEY (estimate_id) REFERENCES estimate(id), FOREIGN KEY (estimate_id) REFERENCES estimate(id),
FOREIGN KEY (loan_type_id) REFERENCES loan_type(id)
FOREIGN KEY (type_id) REFERENCES loan_type(id)
ON UPDATE RESTRICT ON UPDATE RESTRICT
); );




+ 40
- 31
migrations/seed.sql 파일 보기

@@ -175,15 +175,48 @@ INSERT IGNORE INTO borrower (
INSERT INTO estimate ( INSERT INTO estimate (
user_id, user_id,
borrower_id, borrower_id,
comparison_id,
transaction, transaction,
loan_type_id,
loan_amount,
price, price,
property, property,
occupancy, occupancy,
zip, zip,
pud,
pud
) VALUES
(
(SELECT id FROM user WHERE email="test@example.com" LIMIT 1),
(SELECT id FROM borrower ORDER BY id DESC LIMIT 1),
'Purchase',
100000000,
1,
1,
'95051',
false
),
(
(SELECT id FROM user WHERE email="manager@example.com" LIMIT 1),
(SELECT id FROM borrower ORDER BY id DESC LIMIT 1),
'Purchase',
25000000,
2,
1,
'95051',
false
),
(
(SELECT id FROM user WHERE email="test@example.com" LIMIT 1),
(SELECT id FROM borrower ORDER BY id DESC LIMIT 1),
'Refinance',
50000000,
3,
2,
'95051',
false
);

INSERT INTO loan (
estimate_id,
type_id,
amount,
term, term,
interest, interest,
hoi, hoi,
@@ -193,17 +226,9 @@ INSERT INTO estimate (
name name
) VALUES ) VALUES
( (
(SELECT id FROM user WHERE email="test@example.com" LIMIT 1),
(SELECT id FROM borrower ORDER BY id DESC LIMIT 1),
0,
'Purchase',
1,
(SELECT id FROM loan_type WHERE name="Conventional"), (SELECT id FROM loan_type WHERE name="Conventional"),
3300000, 3300000,
100000000,
1,
1,
'95051',
false,
30, 30,
375, 375,
10000, 10000,
@@ -213,17 +238,9 @@ INSERT INTO estimate (
"For client 1" "For client 1"
), ),
( (
(SELECT id FROM user WHERE email="manager@example.com" LIMIT 1),
(SELECT id FROM borrower ORDER BY id DESC LIMIT 1),
0,
'Purchase',
1,
(SELECT id FROM loan_type WHERE name="FHA"), (SELECT id FROM loan_type WHERE name="FHA"),
2510000, 2510000,
25000000,
2,
1,
'95051',
false,
30, 30,
300, 300,
10000, 10000,
@@ -233,17 +250,9 @@ INSERT INTO estimate (
"For client 2" "For client 2"
), ),
( (
(SELECT id FROM user WHERE email="test@example.com" LIMIT 1),
(SELECT id FROM borrower ORDER BY id DESC LIMIT 1),
0,
'Refinance',
2,
(SELECT id FROM loan_type WHERE name="USDA"), (SELECT id FROM loan_type WHERE name="USDA"),
8000000, 8000000,
50000000,
3,
2,
'95051',
false,
10, 10,
125, 125,
15000, 15000,


+ 31
- 25
skouter.go 파일 보기

@@ -34,7 +34,7 @@ type FeeTemplate struct {
Branch int `json:"branch"` Branch int `json:"branch"`
Amount int `json:"amount"` Amount int `json:"amount"`
Perc int `json:"perc"` Perc int `json:"perc"`
Ftype string `json:"type"`
Type string `json:"type"`
Notes string `json:"notes"` Notes string `json:"notes"`
Name string `json:"name"` Name string `json:"name"`
Category string `json:"category"` Category string `json:"category"`
@@ -63,7 +63,7 @@ type Loan struct {
Id int `json:id` Id int `json:id`
EstimateId int `json:estimate_id` EstimateId int `json:estimate_id`
Type LoanType `json:"loanType"` Type LoanType `json:"loanType"`
LoanAmount int `json:"loanAmount"`
Amount int `json:"loanAmount"`
Term int `json:"term"` Term int `json:"term"`
Ltv int `json:"ltv"` Ltv int `json:"ltv"`
Dti int `json:"dti"` Dti int `json:"dti"`
@@ -224,14 +224,14 @@ func getFees(db *sql.DB, loan int) ([]Fee, error) {
defer rows.Close() defer rows.Close()


for rows.Next() { for rows.Next() {
var fee FeeTemplate
var fee Fee


if err := rows.Scan( if err := rows.Scan(
&fee.Id, &fee.Id,
&fee.LoanId, &fee.LoanId,
&fee.Amount, &fee.Amount,
&fee.Perc, &fee.Perc,
&fee.Ftype,
&fee.Type,
&fee.Notes, &fee.Notes,
&fee.Name, &fee.Name,
&fee.Category, &fee.Category,
@@ -289,15 +289,17 @@ func getFeesTemp(db *sql.DB, user int) ([]FeeTemplate, error) {
return fees, nil return fees, nil
} }


func getLoans(db *sql.DB, estimate int) {
func getLoans(db *sql.DB, estimate int) ([]Loan, error) {
var loans []Loan var loans []Loan


rows, err := db.Query(
"SELECT loan.id, loan.amount, loan.term, loan.interest, loan.ltv,
loan.dti, loan.hoi, loan.mi_name, loan.mi_amount FROM loan " +
"INNER JOIN loan_type ON loan.type_id = loan_type(id)" +
"WHERE loan.estimate_id = ?",
estimate)
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 = ?
`
rows, err := db.Query(query, estimate)
if err != nil { if err != nil {
return nil, fmt.Errorf("Loan query error %v", err) return nil, fmt.Errorf("Loan query error %v", err)
@@ -310,27 +312,31 @@ func getLoans(db *sql.DB, estimate int) {


if err := rows.Scan( if err := rows.Scan(
&loan.Id, &loan.Id,
&loan.EstimateId,
&loan.Branch,
&fee.Amount,
&fee.Perc,
&fee.Ftype,
&fee.Notes,
&fee.Name,
&fee.Category,
&fee.Auto)
&loan.Amount,
&loan.Term,
&loan.Interest,
&loan.Ltv,
&loan.Dti,
&loan.Hoi,
&loan.MiName,
&loan.MiAmount,
&loan.Type.Id,
&loan.Type.User,
&loan.Type.Branch,
&loan.Type.Name,
)
err != nil { err != nil {
return nil, fmt.Errorf("FeesTemplate scanning error: %v", err)
return loans, fmt.Errorf("Loans scanning error: %v", err)
} }


fees = append(fees, fee)
loans = append(loans, loan)
} }
est, err := getEstimate(db, 1) est, err := getEstimate(db, 1)
fmt.Printf("the estimate: %v,\nthe error %v\n", est, err)
fmt.Printf("the loan: %v,\nthe error %v\n", est, err)
// getMi(db, getEstimate(db, 1), getBorrower(db, 1)) // getMi(db, getEstimate(db, 1), getBorrower(db, 1))


return fees, nil
return loans, nil
} }


func getBorrower(db *sql.DB, id int) (Borrower, error) { func getBorrower(db *sql.DB, id int) (Borrower, error) {
@@ -403,7 +409,7 @@ func getMi(db *sql.DB, estimate Estimate, borrower Borrower) {
"propertyTypeCode": propertyCodes[estimate.Property], "propertyTypeCode": propertyCodes[estimate.Property],
"occupancyTypeCode": "PRS", "occupancyTypeCode": "PRS",
"loanPurposeCode": "PUR", "loanPurposeCode": "PUR",
"loanAmount": strconv.Itoa(estimate.LoanAmount / 100),
"loanAmount": strconv.Itoa(500000 / 100),
"loanToValue": "LTV95", "loanToValue": "LTV95",
"amortizationTerm": "A30", "amortizationTerm": "A30",
"loanTypeCode": "FXD", "loanTypeCode": "FXD",


불러오는 중...
취소
저장