This commit is contained in:
Ruidy 2024-02-16 12:13:33 +01:00
parent 3f6c5b6c99
commit 72d0212316
No known key found for this signature in database
GPG key ID: E00F51288CB857CC
3 changed files with 12 additions and 7 deletions

1
.gitignore vendored
View file

@ -24,5 +24,6 @@ Network Trash Folder
Temporary Items
.apdisk
tmp
tmp.pdf
.env
*templ.txt

View file

@ -7,7 +7,7 @@ Manage your holiday rental
- [x] Create a booking
- [x] Add line items
- [x] Read from the database
- [ ] Build the pdf invoice
- [x] Build the pdf invoice
- [ ] Refactor the env variable calls to a Config struct with proper defaults
- [ ] Refactor handlers to call their dependencies instead of taking them from the Server struct
@ -17,4 +17,4 @@ Manage your holiday rental
- Htmx
- Templ
- PostgreSQL

View file

@ -3,6 +3,7 @@ package server
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
@ -307,20 +308,24 @@ func (s Server) handleCreateInvoicePdf() echo.HandlerFunc {
defer resp.Body.Close()
log.Warnf("Response Status:", resp.Status)
type response struct {
res := new(struct {
Url string `json:"url"`
Error string `json:"error"`
}
res := new(response)
})
body, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
err = json.Unmarshal(body, res)
if err != nil {
log.Warnf("error decoding response: %s", err)
return err
}
log.Warnf("Response body:", res)
if res.Error != "" {
log.Warnf("error building pdf file %s", err)
return errors.New(res.Error)
}
resp, err = http.Get(res.Url)
if err != nil {
@ -329,7 +334,6 @@ func (s Server) handleCreateInvoicePdf() echo.HandlerFunc {
}
defer resp.Body.Close()
// Copy the file content to the response writer
file, err := os.Create("tmp.pdf")
if err != nil {
log.Fatal(err)