diff --git a/README.md b/README.md index 0cc9185..a66c5a6 100644 --- a/README.md +++ b/README.md @@ -4,10 +4,12 @@ Manage your holiday rental ## To Do -- [ ] Create a booking -- [ ] Add line items -- [ ] Read from the database +- [x] Create a booking +- [x] Add line items +- [x] Read from the database - [ ] 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 ## Built With @@ -15,4 +17,4 @@ Manage your holiday rental - Htmx - Templ - PostgreSQL - \ No newline at end of file + diff --git a/internal/server/handlers.go b/internal/server/handlers.go index d5844db..747c7ef 100644 --- a/internal/server/handlers.go +++ b/internal/server/handlers.go @@ -4,6 +4,7 @@ import ( "bytes" "encoding/json" "fmt" + "io" "net/http" "os" "strconv" @@ -58,13 +59,13 @@ func (s Server) handleNewBookingPage() echo.HandlerFunc { func (s Server) handleCreateBooking() echo.HandlerFunc { return func(c echo.Context) error { type NewBooking struct { - Name string `form:"name"` - PhoneNumber string `form:"phone_number"` - CustomerNumber int `form:"customer_number"` - Email string `form:"email"` From time.Time `json:"from"` To time.Time `from:"to"` + Name string `form:"name"` + PhoneNumber string `form:"phone_number"` + Email string `form:"email"` Platform string `form:"platform"` + CustomerNumber int `form:"customer_number"` PlatformFees float64 `form:"platform_fees"` } nb := new(NewBooking) @@ -220,12 +221,12 @@ func (s Server) handleComputeReport() echo.HandlerFunc { } type Result struct { - Id string - Total float64 - CustomerName string From time.Time To time.Time + Id string + CustomerName string Platform string + Total float64 PlatformFees float64 } res := make([]*Result, 0) @@ -271,26 +272,26 @@ func (s Server) handleComputeReport() echo.HandlerFunc { } func (s Server) handleCreateInvoicePdf() echo.HandlerFunc { - return func(_ echo.Context) error { + return func(c echo.Context) error { data := struct { Context map[string]any `json:"context"` Path string `json:"path"` ProjectId string `json:"projectId"` }{ - Context: map[string]interface{}{}, - Path: "index.html", + Context: map[string]any{}, + Path: "index.html", // TODO: put in a variable ProjectId: os.Getenv("HTMLDOCS_PROJECT_ID"), } payload, err := json.Marshal(data) if err != nil { - fmt.Println("Error marshalling JSON:", err) + log.Warnf("Error marshalling JSON:", err) return err } req, err := http.NewRequest("POST", os.Getenv("HTMLDOCS_URL"), bytes.NewBuffer(payload)) if err != nil { - fmt.Println("Error creating request:", err) + log.Warnf("Error creating request:", err) return err } @@ -300,14 +301,51 @@ func (s Server) handleCreateInvoicePdf() echo.HandlerFunc { client := &http.Client{} resp, err := client.Do(req) if err != nil { - fmt.Println("Error sending request:", err) + log.Warnf("Error sending request:", err) return err } defer resp.Body.Close() - fmt.Println("Response Status:", resp.Status) - fmt.Println("Response Status:", resp.Body) + log.Warnf("Response Status:", resp.Status) + type response 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 { + return err + } + log.Warnf("Response body:", res) - return nil + resp, err = http.Get(res.Url) + if err != nil { + log.Warnf("Error retrieving file") + return err + } + defer resp.Body.Close() + + // Copy the file content to the response writer + file, err := os.Create("tmp.pdf") + if err != nil { + log.Fatal(err) + } + defer file.Close() + + body, err = io.ReadAll(resp.Body) + if err != nil { + return err + } + + _, err = file.Write(body) + if err != nil { + log.Error("Error copying file content") + return err + } + return c.Attachment("tmp.pdf", "tmp.pdf") } } diff --git a/internal/views/booking_by_id.templ b/internal/views/booking_by_id.templ index 3b66686..c76616d 100644 --- a/internal/views/booking_by_id.templ +++ b/internal/views/booking_by_id.templ @@ -35,7 +35,7 @@ templ BookingById(booking *BookingViewModel) {

Manage a booking

- + Create PDF
diff --git a/internal/views/booking_by_id_templ.go b/internal/views/booking_by_id_templ.go index 10e2b2f..8c6834e 100644 --- a/internal/views/booking_by_id_templ.go +++ b/internal/views/booking_by_id_templ.go @@ -56,7 +56,7 @@ func BookingById(booking *BookingViewModel) templ.Component { templ_7745c5c3_Buffer = templ.GetBuffer() defer templ.ReleaseBuffer(templ_7745c5c3_Buffer) } - templ_7745c5c3_Err = templ.WriteWatchModeString(templ_7745c5c3_Buffer, 1) + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("

Booking ID VFNI#") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } @@ -69,7 +69,7 @@ func BookingById(booking *BookingViewModel) templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - templ_7745c5c3_Err = templ.WriteWatchModeString(templ_7745c5c3_Buffer, 2) + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("

Manage a booking

Create PDF

Line Items

") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } for _, item := range booking.Items { - templ_7745c5c3_Err = templ.WriteWatchModeString(templ_7745c5c3_Buffer, 16) + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } } - templ_7745c5c3_Err = templ.WriteWatchModeString(templ_7745c5c3_Buffer, 23) + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("
#ItemQuantityPrice (€)Payment MethodPayment StatusSub-total (€)
") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } @@ -195,7 +195,7 @@ func BookingById(booking *BookingViewModel) templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - templ_7745c5c3_Err = templ.WriteWatchModeString(templ_7745c5c3_Buffer, 17) + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } @@ -208,7 +208,7 @@ func BookingById(booking *BookingViewModel) templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - templ_7745c5c3_Err = templ.WriteWatchModeString(templ_7745c5c3_Buffer, 18) + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } @@ -221,7 +221,7 @@ func BookingById(booking *BookingViewModel) templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - templ_7745c5c3_Err = templ.WriteWatchModeString(templ_7745c5c3_Buffer, 19) + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } @@ -234,7 +234,7 @@ func BookingById(booking *BookingViewModel) templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - templ_7745c5c3_Err = templ.WriteWatchModeString(templ_7745c5c3_Buffer, 20) + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } @@ -247,7 +247,7 @@ func BookingById(booking *BookingViewModel) templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - templ_7745c5c3_Err = templ.WriteWatchModeString(templ_7745c5c3_Buffer, 21) + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } @@ -260,12 +260,12 @@ func BookingById(booking *BookingViewModel) templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - templ_7745c5c3_Err = templ.WriteWatchModeString(templ_7745c5c3_Buffer, 22) + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("
") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } @@ -278,7 +278,7 @@ func BookingById(booking *BookingViewModel) templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - templ_7745c5c3_Err = templ.WriteWatchModeString(templ_7745c5c3_Buffer, 24) + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("
Add line
") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err }