Skip to content

Commit

Permalink
Add service column and genericise others
Browse files Browse the repository at this point in the history
  • Loading branch information
scottmckendry committed Nov 4, 2023
1 parent ee699fc commit 713aab9
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 39 deletions.
48 changes: 27 additions & 21 deletions data.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ var dbUrl = "file:./db.sqlite3"
var Db *sql.DB

type Dog struct {
ID int
Name string
OwnerName string
Address string
City string
Email string
WalksPerWeek int
PricePerWalk float64
ID int
Name string
OwnerName string
Address string
City string
Email string
Service string
Quantity int
Price float64
}

func Init() error {
Expand Down Expand Up @@ -55,8 +56,9 @@ func createTables() error {
address TEXT,
city TEXT,
email TEXT,
walksPerWeek INTEGER,
pricePerWalk INTEGER
service TEXT,
quantity INTEGER,
price INTEGER
);
`)
if err != nil {
Expand All @@ -82,8 +84,9 @@ func GetDogs() ([]Dog, error) {
&dog.Address,
&dog.City,
&dog.Email,
&dog.WalksPerWeek,
&dog.PricePerWalk,
&dog.Service,
&dog.Quantity,
&dog.Price,
)
if err != nil {
return nil, fmt.Errorf("error scanning dog: %v", err)
Expand All @@ -103,8 +106,9 @@ func GetDog(id int) (Dog, error) {
&dog.Address,
&dog.City,
&dog.Email,
&dog.WalksPerWeek,
&dog.PricePerWalk,
&dog.Service,
&dog.Quantity,
&dog.Price,
)
if err != nil {
return Dog{}, fmt.Errorf("error getting dog: %v", err)
Expand All @@ -121,10 +125,11 @@ func AddDog(dog Dog) error {
address,
city,
email,
walksPerWeek,
pricePerWalk
) VALUES (?, ?, ?, ?, ?, ?, ?)
`, dog.Name, dog.OwnerName, dog.Address, dog.City, dog.Email, dog.WalksPerWeek, dog.PricePerWalk)
service,
quantity,
price
) VALUES (?, ?, ?, ?, ?, ?, ?, ?)
`, dog.Name, dog.OwnerName, dog.Address, dog.City, dog.Email, dog.Service, dog.Quantity, dog.Price)
if err != nil {
return fmt.Errorf("error adding dog: %v", err)
}
Expand All @@ -140,10 +145,11 @@ func UpdateDog(dog Dog) error {
address = ?,
city = ?,
email = ?,
walksPerWeek = ?,
pricePerWalk = ?
service = ?,
quantity = ?,
price = ?
WHERE id = ?
`, dog.Name, dog.OwnerName, dog.Address, dog.City, dog.Email, dog.WalksPerWeek, dog.PricePerWalk, dog.ID)
`, dog.Name, dog.OwnerName, dog.Address, dog.City, dog.Email, dog.Service, dog.Quantity, dog.Price, dog.ID)
if err != nil {
return fmt.Errorf("error updating dog: %v", err)
}
Expand Down
4 changes: 3 additions & 1 deletion invoice.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,15 @@ func sendEmail(dog Dog) error {
)
d.TLSConfig = &tls.Config{InsecureSkipVerify: true}

ownerFirstName := strings.Split(dog.OwnerName, " ")[0]

m := gomail.NewMessage()
m.SetHeader("From", fmt.Sprintf("Canine Club<%s>", os.Getenv("SMTP_USER")))
m.SetHeader("To", fmt.Sprintf("%s <%s>", dog.OwnerName, dog.Email))
m.SetHeader("Subject", "Canine Club - Invoice for "+dog.Name)
m.SetBody(
"text/html",
"Hi "+dog.OwnerName+",<br><br>Here is your invoice for "+dog.Name+".<br><br>Kind regards,<br>Canine Club",
"Hi "+ownerFirstName+",<br><br>Here is your invoice for "+dog.Name+".<br><br>Kind regards,<br>Canine Club",
)
m.Attach(invoiceFile)

Expand Down
7 changes: 4 additions & 3 deletions routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,11 @@ func SetRoutes(app *fiber.App) {
"OwnerName": dog.OwnerName,
"Address": dog.Address,
"City": dog.City,
"Walks": dog.WalksPerWeek,
"Price": strconv.FormatFloat(dog.PricePerWalk, 'f', 2, 64),
"Service": dog.Service,
"Quantity": dog.Quantity,
"Price": strconv.FormatFloat(dog.Price, 'f', 2, 64),
"Total": strconv.FormatFloat(
(float64(dog.WalksPerWeek) * dog.PricePerWalk),
(float64(dog.Quantity) * dog.Price),
'f',
2,
64,
Expand Down
5 changes: 3 additions & 2 deletions views/dogs.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,16 @@
class="border-b font-medium dark:border-neutral-500"
>
<tr>
<th scope="col" class="px-6 py-4">#</th>
<th scope="col" class="px-6 py-4 hidden">#</th>
<th scope="col" class="px-6 py-4">Name</th>
<th scope="col" class="px-6 py-4">
Owner Name
</th>
<th scope="col" class="px-6 py-4">Address</th>
<th scope="col" class="px-6 py-4">City</th>
<th scope="col" class="px-6 py-4">Email</th>
<th scope="col" class="px-6 py-4">Walks</th>
<th scope="col" class="px-6 py-4">Service</th>
<th scope="col" class="px-6 py-4">Quantity</th>
<th scope="col" class="px-6 py-4">Price</th>
<th scope="col" class="px-6 py-4">Actions</th>
</tr>
Expand Down
4 changes: 2 additions & 2 deletions views/invoice.html
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,8 @@
</tr>

<tr class="item">
<td>Walks</td>
<td>{{.Walks}}</td>
<td>{{.Service}}</td>
<td>{{.Quantity}}</td>
<td>${{.Price}}</td>
</tr>

Expand Down
15 changes: 12 additions & 3 deletions views/row-add.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<tr id="datarow-add" class="border-b dark:border-neutral-500">
<td class="px-6 py-4"></td>
<td class="px-6 py-4 hidden"></td>
<td class="px-6 py-4">
<input
type="text"
Expand Down Expand Up @@ -50,7 +50,7 @@
type="text"
class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline"
data-include-add=""
name="walksPerWeek"
name="service"
value=""
/>
</td>
Expand All @@ -59,7 +59,16 @@
type="text"
class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline"
data-include-add=""
name="pricePerWalk"
name="quantity"
value=""
/>
</td>
<td class="px-6 py-4">
<input
type="text"
class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline"
data-include-add=""
name="price"
value=""
/>
</td>
Expand Down
17 changes: 13 additions & 4 deletions views/row-edit.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<tr id="datarow-{{.ID}}" class="border-b dark:border-neutral-500">
<td class="px-6 py-4">{{.ID}}</td>
<td class="px-6 py-4 hidden">{{.ID}}</td>
<td class="px-6 py-4">
<input
type="text"
Expand Down Expand Up @@ -50,8 +50,17 @@
type="text"
class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline"
data-include-edit="{{.ID}}"
name="walksPerWeek"
value="{{.WalksPerWeek}}"
name="service"
value="{{.Service}}"
/>
</td>
<td class="px-6 py-4">
<input
type="text"
class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline"
data-include-edit="{{.ID}}"
name="quantity"
value="{{.Quantity}}"
/>
</td>
<td class="px-6 py-4">
Expand All @@ -60,7 +69,7 @@
class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline"
data-include-edit="{{.ID}}"
name="pricePerWalk"
value="{{.PricePerWalk}}"
value="{{.Price}}"
/>
</td>
<td class="px-1 py-1">
Expand Down
7 changes: 4 additions & 3 deletions views/row.html
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
<tr id="datarow-{{.ID}}" class="border-b dark:border-neutral-500">
<td class="px-6 py-4">{{.ID}}</td>
<td class="px-6 py-4 hidden">{{.ID}}</td>
<td class="px-6 py-4">{{.Name}}</td>
<td class="px-6 py-4">{{.OwnerName}}</td>
<td class="px-6 py-4">{{.Address}}</td>
<td class="px-6 py-4">{{.City}}</td>
<td class="px-6 py-4">{{.Email}}</td>
<td class="px-6 py-4">{{.WalksPerWeek}}</td>
<td class="px-6 py-4">{{.PricePerWalk}}</td>
<td class="px-6 py-4">{{.Service}}</td>
<td class="px-6 py-4">{{.Quantity}}</td>
<td class="px-6 py-4">{{.Price}}</td>
<td class="px-1 py-1">
<a
hx-get="/dogs/edit/{{.ID}}"
Expand Down

0 comments on commit 713aab9

Please sign in to comment.