Explorar el Código

Fetch user's estimates list from estimates.vue

master
Immanuel Onyeka hace 1 año
padre
commit
3a5477760d
Se han modificado 4 ficheros con 54 adiciones y 64 borrados
  1. +1
    -1
      components/app.vue
  2. +12
    -8
      components/estimates.vue
  3. +11
    -0
      migrations/seed.sql
  4. +30
    -55
      skouter.go

+ 1
- 1
components/app.vue Ver fichero

@@ -12,7 +12,7 @@

<home :user="user" v-else-if="active == 1" />
<new-estimate :user="user" :fees="fees" :token="token" v-else-if="active == 2" />
<estimates :user="user" :fees="fees" v-else-if="active == 3" />
<estimates :user="user" :fees="fees" v-else-if="active == 3" :token="token" />
<settings :user="user" v-else-if="active == 4" />
<sign-out :user="user" v-else-if="active == 5" />
</template>


+ 12
- 8
components/estimates.vue Ver fichero

@@ -32,6 +32,8 @@
<section class="inputs">
<h3>Saved Estimates</h3>

<div v-for="e in estimates">{{e}}</div>

</section>

</div>
@@ -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()
})
</script>

+ 11
- 0
migrations/seed.sql Ver fichero

@@ -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"),


+ 30
- 55
skouter.go Ver fichero

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


Cargando…
Cancelar
Guardar