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 Temporary Items
.apdisk .apdisk
tmp tmp
tmp.pdf
.env .env
*templ.txt *templ.txt

View file

@ -7,7 +7,7 @@ Manage your holiday rental
- [x] Create a booking - [x] Create a booking
- [x] Add line items - [x] Add line items
- [x] Read from the database - [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 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 - [ ] Refactor handlers to call their dependencies instead of taking them from the Server struct
@ -17,4 +17,4 @@ Manage your holiday rental
- Htmx - Htmx
- Templ - Templ
- PostgreSQL - PostgreSQL

View file

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