-
Notifications
You must be signed in to change notification settings - Fork 5
/
handlers.go
181 lines (169 loc) · 4.72 KB
/
handlers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package main
import (
"log"
"os"
"strconv"
"time"
"github.com/gofiber/fiber/v2"
)
// Renders the index page.
func renderIndex(c *fiber.Ctx) error {
dogs, err := getDogs()
if err != nil {
return c.Status(500).SendString(err.Error())
}
return c.Render("index", fiber.Map{
"dogs": dogs,
})
}
// Renders the cards for all dogs.
func renderDogs(c *fiber.Ctx) error {
dogs, err := getDogs()
if err != nil {
return c.Status(500).SendString(err.Error())
}
return c.Render("dogs", fiber.Map{
"dogs": dogs,
})
}
// Renders the modal for adding a new dog.
func renderAdd(c *fiber.Ctx) error {
return c.Render("modal-add", nil)
}
// Gets details for the given dog ID and renders the modal for editing.
func renderEdit(c *fiber.Ctx) error {
id, err := strconv.Atoi(c.Params("id"))
if err != nil {
return c.Status(400).SendString(err.Error())
}
dog, err := getDog(id)
if err != nil {
return c.Status(500).SendString(err.Error())
}
return c.Render("modal-edit", dog)
}
// Generates a preview of the invoice for a given dog as HTML. This is used for generating the PDF.
func renderInvoice(c *fiber.Ctx) error {
id, err := strconv.Atoi(c.Params("id"))
if err != nil {
return c.Status(400).SendString(err.Error())
}
dog, err := getDog(id)
if err != nil {
return c.Status(500).SendString(err.Error())
}
return c.Render("invoice", fiber.Map{
"InvoiceNumber": getInvoiceNumber(dog),
"Date": time.Now().Format("Monday, 2 January 2006"),
"DueDate": nextMonday(time.Now()).Format("Monday, 2 January 2006"),
"FromName": os.Getenv("FROM_NAME"),
"FromAddress": os.Getenv("FROM_ADDRESS"),
"FromCity": os.Getenv("FROM_CITY"),
"AccountNumber": os.Getenv("ACCOUNT_NUMBER"),
"Name": dog.Name,
"OwnerName": dog.OwnerName,
"Address": dog.Address,
"City": dog.City,
"Service": dog.Service,
"Quantity": dog.Quantity,
"Price": strconv.FormatFloat(dog.Price, 'f', 2, 64),
"Total": strconv.FormatFloat(
(float64(dog.Quantity) * dog.Price),
'f',
2,
64,
),
})
}
// Generates a PDF version of the HTML returned by renderInvoice() using the wkhtmltopdf command line tool.
func renderInvoicePdf(c *fiber.Ctx) error {
id, err := strconv.Atoi(c.Params("id"))
if err != nil {
return c.Status(400).SendString(err.Error())
}
dog, err := getDog(id)
if err != nil {
return c.Status(500).SendString(err.Error())
}
pdfPath, err := generatePdf(dog)
if err != nil {
return c.Status(500).SendString(err.Error())
}
return c.SendFile(pdfPath)
}
// Adds a new dog to the database and returns the updated list of dogs.
func handleDogAdd(c *fiber.Ctx) error {
dog := new(Dog)
if err := c.BodyParser(dog); err != nil {
return c.Status(400).SendString(err.Error())
}
err := addDog(*dog)
if err != nil {
return c.Status(500).SendString(err.Error())
}
dogs, err := getDogs()
if err != nil {
return c.Status(500).SendString(err.Error())
}
return c.Render("dogs", fiber.Map{
"dogs": dogs,
})
}
// Updates a dog matching the provided ID in the database and returns the updated list of dogs.
func handleDogUpdate(c *fiber.Ctx) error {
id, err := strconv.Atoi(c.Params("id"))
if err != nil {
return c.Status(400).SendString(err.Error())
}
dog := new(Dog)
dog.ID = id
if err := c.BodyParser(dog); err != nil {
return c.Status(400).SendString(err.Error())
}
err = updateDog(*dog)
if err != nil {
return c.Status(500).SendString(err.Error())
}
dogs, err := getDogs()
if err != nil {
return c.Status(500).SendString(err.Error())
}
return c.Render("dogs", fiber.Map{
"dogs": dogs,
})
}
// Deletes a dog matching the provided ID from the database and returns the updated list of dogs.
func handleDogDelete(c *fiber.Ctx) error {
id, err := strconv.Atoi(c.Params("id"))
if err != nil {
return c.Status(400).SendString(err.Error())
}
err = deleteDog(id)
if err != nil {
return c.Status(500).SendString(err.Error())
}
dogs, err := getDogs()
if err != nil {
return c.Status(500).SendString(err.Error())
}
return c.Render("dogs", fiber.Map{
"dogs": dogs,
})
}
// Sends an invoice for the given dog to the owner's email address.
func handleSendInvoice(c *fiber.Ctx) error {
id, err := strconv.Atoi(c.Params("id"))
if err != nil {
return c.Status(400).SendString(err.Error())
}
// err = sendInvoice(id)
// err = nil // replace with db insert into email queue table
err = queueEmail(id)
if err != nil {
log.Println(err)
// We're intentionally not returning the error to the client here. HTMX won't update the element if the response is an error.
// Not sure why this is the case, but probably worth revisiting in the future.
return c.SendString("Failed :(")
}
return c.SendString("<button class=\"btn-disabled\" disabled>Queued!</button>")
}