fix errors and test pdf

This commit is contained in:
vinchent 2024-08-26 23:06:29 +02:00
parent 08fb20d7be
commit 491f482fc7
3 changed files with 23 additions and 10 deletions

View File

@ -53,7 +53,7 @@ func (app *application) writeJSON(
} }
func (app *application) badRequest(w http.ResponseWriter, r *http.Request, err error) error { func (app *application) badRequest(w http.ResponseWriter, r *http.Request, err error) error {
var payload jsonResponse var payload JSONResponse
payload.OK = false payload.OK = false
payload.Message = err.Error() payload.Message = err.Error()
@ -70,7 +70,7 @@ func (app *application) badRequest(w http.ResponseWriter, r *http.Request, err e
} }
func (app *application) invalidCredentials(w http.ResponseWriter) error { func (app *application) invalidCredentials(w http.ResponseWriter) error {
var payload jsonResponse var payload JSONResponse
payload.OK = false payload.OK = false
payload.Message = "invalid authentication credentials" payload.Message = "invalid authentication credentials"

View File

@ -24,14 +24,27 @@ func (app *application) CreateAndSendInvoice(w http.ResponseWriter, r *http.Requ
// receive json // receive json
var order Order var order Order
err := app.readJSON(w, r, &order) order.ID = 100
order.Email = "me@here.com"
order.FirstName = "John"
order.LastName = "Smith"
order.Quantity = 1
order.Amount = 1000
order.Product = "Widget"
order.CreatedAt = time.Now()
// err = app.readJSON(w, r, &order)
// if err != nil {
// app.badRequest(w, r, err)
// return
// }
// generate a pdf invoice
err := app.createInvoicePDF(order)
if err != nil { if err != nil {
app.badRequest(w, r, err) app.badRequest(w, r, err)
return return
} }
// generate a pdf invoice
// create mail // create mail
// send mail with attachment // send mail with attachment
@ -50,7 +63,7 @@ func (app *application) createInvoicePDF(order Order) error {
importer := gofpdi.NewImporter() importer := gofpdi.NewImporter()
t := importer.ImportPage(pdf, "./pdf-templates/invoice.pdf", q, "/MediaBox") t := importer.ImportPage(pdf, "./pdf-templates/invoice.pdf", 1, "/MediaBox")
pdf.AddPage() pdf.AddPage()
importer.UseImportedTemplate(pdf, t, 0, 0, 215.9, 0) importer.UseImportedTemplate(pdf, t, 0, 0, 215.9, 0)
@ -88,7 +101,7 @@ func (app *application) createInvoicePDF(order Order) error {
pdf.CellFormat( pdf.CellFormat(
97, 97,
8, 8,
order.CreatedAt.Format("2005-01-02"), order.CreatedAt.Format("2006-01-02"),
"", "",
0, 0,
"L", "L",
@ -125,9 +138,9 @@ func (app *application) createInvoicePDF(order Order) error {
pdf.SetX(185) pdf.SetX(185)
pdf.CellFormat( pdf.CellFormat(
155, 20,
8, 8,
fmt.Sprintf("%.2f", float32(order.Amount/100.0)), fmt.Sprintf("$%.2f", float32(order.Amount)/100.0),
"", "",
0, 0,
"R", "R",

View File

@ -18,6 +18,6 @@ func (app *application) routes() http.Handler {
MaxAge: 300, MaxAge: 300,
})) }))
mux.Post("/invoice/create-and-send", app.CreateAndSendInvoice) mux.Get("/invoice/create-and-send", app.CreateAndSendInvoice)
return mux return mux
} }