diff --git a/components/estimate-test.vue b/components/estimate-test.vue index e0117f9..0cfe639 100644 --- a/components/estimate-test.vue +++ b/components/estimate-test.vue @@ -77,7 +77,8 @@ function getEstimates() { function getPdf() { fetch(`/api/pdf`, - {method: 'GET', + {method: 'POST', + body: JSON.stringify(estimate.value), headers: { "Accept": "application/json", "Authorization": `Bearer ${props.token}`, diff --git a/skouter.go b/skouter.go index 753f521..80d45f0 100644 --- a/skouter.go +++ b/skouter.go @@ -122,6 +122,7 @@ type Loan struct { Mi MI `json:"mi"` Fees []Fee `json:"fees"` Name string `json:"title"` + Result Result `json:"result"` } type MI struct { @@ -159,7 +160,6 @@ type Estimate struct { Zip string `json:"zip"` Pud bool `json:"pud"` Loans []Loan `json:"loans"` - Results []Result `json:"results"` } type Password struct { @@ -861,10 +861,15 @@ func queryUsers(db *sql.DB, id int) ( []User, error ) { return users, nil } -func insertResults(db *sql.DB, results []Result) (error){ +func insertResults(db *sql.DB, estimate Estimate) (error){ var query string var row *sql.Row var err error + var results []Result + + for i := range estimate.Loans { + results = append(results, estimate.Loans[i].Result) + } query = `INSERT INTO estimate_result ( @@ -1456,12 +1461,6 @@ func getEstimates(db *sql.DB, id int, user int) ( []Estimate, error ) { return estimates, err } estimate.Borrower = borrower - - estimate.Results, err = getResults(db, estimate.Id, 0) - if err != nil { - return estimates, err - } - estimates = append(estimates, estimate) } @@ -1676,8 +1675,8 @@ func createEstimate(w http.ResponseWriter, db *sql.DB, r *http.Request) { estimate, err = insertEstimate(db, estimate) if err != nil { http.Error(w, err.Error(), 422); return } - estimate.Results = makeResults(estimate) - err = insertResults(db, estimate.Results) + makeResults(estimate) + err = insertResults(db, estimate) if err != nil { http.Error(w, err.Error(), 500); return } e, err := getEstimates(db, estimate.Id, 0) if err != nil { http.Error(w, err.Error(), 500); return } @@ -1825,6 +1824,8 @@ func showPDF(w http.ResponseWriter, r *http.Request) { var pa = template.Must(template.ParseFiles("views/master.tpl", "views/test.tpl")) + estimates, err := getEstimates(db, 1, 0) + if err != nil { w.WriteHeader(500); return } // claims, err := getClaims(r) if err != nil { w.WriteHeader(500); return } @@ -1836,10 +1837,12 @@ func showPDF(w http.ResponseWriter, r *http.Request) { Avatar string Letterhead string User User + Estimate Estimate }{ Title: "test PDF", Name: "idk-random-name", User: users[0], + Estimate: estimates[0], } avatar, err := fetchAvatar(db, info.User.Id) letterhead, err := fetchLetterhead(db, info.User.Id) @@ -1854,7 +1857,8 @@ func showPDF(w http.ResponseWriter, r *http.Request) { } func getPdf(w http.ResponseWriter, db *sql.DB, r *http.Request) { - var err error + var estimate Estimate + err := json.NewDecoder(r.Body).Decode(&estimate) cmd := exec.Command("wkhtmltopdf", "-", "-") stdout, err := cmd.StdoutPipe() @@ -1878,13 +1882,13 @@ func getPdf(w http.ResponseWriter, db *sql.DB, r *http.Request) { var pa = template.Must(template.ParseFiles("views/pdf.tpl", "views/test.tpl")) - // claims, err := getClaims(r) + claims, err := getClaims(r) if err != nil { w.WriteHeader(500); log.Println(err) return } - users, err := queryUsers(db, 1) + users, err := queryUsers(db, claims.Id) info := struct { Title string @@ -1892,10 +1896,12 @@ func getPdf(w http.ResponseWriter, db *sql.DB, r *http.Request) { Avatar string Letterhead string User User + Estimate Estimate }{ Title: "test PDF", Name: "idk-random-name", User: users[0], + Estimate: estimate, } avatar, err := fetchAvatar(db, info.User.Id) letterhead, err := fetchLetterhead(db, info.User.Id) @@ -2055,7 +2061,7 @@ func api(w http.ResponseWriter, r *http.Request) { guard(r, 1): summarize(w, db, r) case match(p, "/api/pdf", &args) && - r.Method == http.MethodGet && + r.Method == http.MethodPost && guard(r, 1): getPdf(w, db, r) default: diff --git a/views/test.tpl b/views/test.tpl index 55cbe25..a723bca 100644 --- a/views/test.tpl +++ b/views/test.tpl @@ -26,6 +26,55 @@ + + +{{range $l := .Estimate.Loans}}{{end}} + + + + +{{range $l := .Estimate.Loans}}{{end}} + +{{range $l := .Estimate.Loans}}{{end}} + +{{range $l := .Estimate.Loans}}{{end}} + +{{range $l := .Estimate.Loans}}{{end}} + + {{range $l := .Estimate.Loans}}{{end}} + + {{range $l := .Estimate.Loans}}{{end}} + + {{range $l := .Estimate.Loans}}{{end}} + +{{range $l := .Estimate.Loans}}{{end}} + + +{{range $l := .Estimate.Loans}}{{end}} + +{{range $l := .Estimate.Loans}}{{end}} + +{{range $l := .Estimate.Loans}}{{end}} + +{{range $l := .Estimate.Loans}}{{end}} + + {{range $l := .Estimate.Loans}}{{end}} + + {{range $l := .Estimate.Loans}}{{end}} + + {{range $l := .Estimate.Loans}}{{end}} + +{{range $l := .Estimate.Loans}}{{end}} + + +
{{$l.Name}}
Type{{$l.Type.Name}}
Term{{$l.Term}} Years
Amortization{{$l.Amortization}}
Interest rate{{$l.Interest}}%
Monthly MI${{$l.Mi.Premium}}/month
MI Premium${{$l.Mi.Upfront}}
Real estate taxes${{$l.Tax}}
Total monthly payment{{$l.Result}}
Purchase price{{$.Estimate.Price}}
Term{{$l.Term}} Years
Amortization{{$l.Amortization}}
Interest rate{{$l.Interest}}%
Monthly MI${{$l.Mi.Premium}}/month
MI Premium${{$l.Mi.Upfront}}
Real estate taxes${{$l.Tax}}
Total monthly payment{{$l.Result}}
+ +
+
+{{.Estimate}} +
+
+