From 3a5477760d5f56e2efb9711564f1c1b79cc62bdd Mon Sep 17 00:00:00 2001 From: Immanuel Onyeka Date: Sun, 16 Jul 2023 21:50:02 -0400 Subject: [PATCH] Fetch user's estimates list from estimates.vue --- components/app.vue | 2 +- components/estimates.vue | 20 ++++++---- migrations/seed.sql | 11 ++++++ skouter.go | 85 ++++++++++++++-------------------------- 4 files changed, 54 insertions(+), 64 deletions(-) diff --git a/components/app.vue b/components/app.vue index 5933b03..de26d85 100644 --- a/components/app.vue +++ b/components/app.vue @@ -12,7 +12,7 @@ - + diff --git a/components/estimates.vue b/components/estimates.vue index 94e443d..9500964 100644 --- a/components/estimates.vue +++ b/components/estimates.vue @@ -32,6 +32,8 @@

Saved Estimates

+
{{e}}
+
@@ -41,8 +43,9 @@ import { ref, computed, onMounted } from 'vue' import FeeDialog from "./fee-dialog.vue" -const props = defineProps(['user', 'fees']) +const props = defineProps(['user', 'fees', 'token']) let edit = ref(null) +let estimates = ref([]) function newFee(fee, isDebit) { this.edit = null @@ -57,24 +60,25 @@ function remove() { } function getEstimates() { - const token = getCookie("skouter") - - return fetch(`/api/estimates`, + fetch(`/api/estimates`, {method: 'GET', headers: { "Accept": "application/json", - "Authorization": `Bearer ${token}`, + "Authorization": `Bearer ${props.token}`, }, }).then(response => { - if (response.ok) { return response.json() } + if (response.ok) { return response.json() } else { + response.text().then(t => console.log(t)) + } }).then (result => { if (!result || !result.length) return // Exit if token is invalid or no fees are saved - this.fees = result + estimates.value = result + // console.log(result) }) } onMounted(() => { - // getEstimates() + getEstimates() }) diff --git a/migrations/seed.sql b/migrations/seed.sql index 6a3eca9..a016887 100644 --- a/migrations/seed.sql +++ b/migrations/seed.sql @@ -250,6 +250,17 @@ INSERT INTO loan ( 0, "For client 2" ), +( + 3, + (SELECT id FROM loan_type WHERE name="FHA"), + 2510000, + 30, + 300, + 90.00, + 6.70, + 0, + "For client 2" +), ( 2, (SELECT id FROM loan_type WHERE name="USDA"), diff --git a/skouter.go b/skouter.go index f8bd366..3102b5c 100644 --- a/skouter.go +++ b/skouter.go @@ -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):