From 72d0212316adf4576df61720d98c2d2317f2b9d4 Mon Sep 17 00:00:00 2001 From: Ruidy Date: Fri, 16 Feb 2024 12:13:33 +0100 Subject: [PATCH] refactor --- .gitignore | 1 + README.md | 4 ++-- internal/server/handlers.go | 14 +++++++++----- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/.gitignore b/.gitignore index 07bc6cf..4747098 100644 --- a/.gitignore +++ b/.gitignore @@ -24,5 +24,6 @@ Network Trash Folder Temporary Items .apdisk tmp +tmp.pdf .env *templ.txt diff --git a/README.md b/README.md index a66c5a6..3066290 100644 --- a/README.md +++ b/README.md @@ -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 - + diff --git a/internal/server/handlers.go b/internal/server/handlers.go index 747c7ef..dfb3131 100644 --- a/internal/server/handlers.go +++ b/internal/server/handlers.go @@ -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)