Skip to content

Commit

Permalink
Add support for card ordering (#10)
Browse files Browse the repository at this point in the history
* add new 'weekday' column to database

* update frontend components to include new column
  • Loading branch information
scottmckendry committed Feb 22, 2024
1 parent d60b209 commit 4cd138b
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 9 deletions.
35 changes: 29 additions & 6 deletions data.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"database/sql"
"fmt"
"strings"

_ "github.com/tursodatabase/libsql-client-go/libsql"
_ "modernc.org/sqlite"
Expand All @@ -21,6 +22,7 @@ type Dog struct {
Service string
Quantity int
Price float64
Grouping int
}

func Init() error {
Expand All @@ -32,6 +34,11 @@ func Init() error {
// No need to check for error here, if the connection can be made, the tables will be created
_ = createTables()

err = updateTables()
if err != nil {
return fmt.Errorf("error updating tables: %v", err)
}

return nil
}

Expand All @@ -58,6 +65,7 @@ func createTables() error {
service TEXT,
quantity INTEGER,
price INTEGER
grouping INTEGER
);
`)
if err != nil {
Expand All @@ -67,8 +75,19 @@ func createTables() error {
return nil
}

func updateTables() error {
_, err := db.Exec(`
ALTER TABLE dogs ADD COLUMN grouping INTEGER;
`)
if err == nil || strings.Contains(err.Error(), "duplicate column name") {
return nil
}

return fmt.Errorf("error updating dogs table: %v", err)
}

func getDogs() ([]Dog, error) {
rows, err := db.Query("SELECT * FROM dogs")
rows, err := db.Query("SELECT * FROM dogs group by grouping, id")
if err != nil {
return nil, fmt.Errorf("error getting dogs: %v", err)
}
Expand All @@ -86,6 +105,7 @@ func getDogs() ([]Dog, error) {
&dog.Service,
&dog.Quantity,
&dog.Price,
&dog.Grouping,
)
if err != nil {
rows.Close()
Expand All @@ -109,6 +129,7 @@ func getDog(id int) (Dog, error) {
&dog.Service,
&dog.Quantity,
&dog.Price,
&dog.Grouping,
)
if err != nil {
return Dog{}, fmt.Errorf("error getting dog: %v", err)
Expand All @@ -127,9 +148,10 @@ func addDog(dog Dog) error {
email,
service,
quantity,
price
) VALUES (?, ?, ?, ?, ?, ?, ?, ?)
`, dog.Name, dog.OwnerName, dog.Address, dog.City, dog.Email, dog.Service, dog.Quantity, dog.Price)
price,
grouping
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
`, dog.Name, dog.OwnerName, dog.Address, dog.City, dog.Email, dog.Service, dog.Quantity, dog.Price, dog.Grouping)
if err != nil {
return fmt.Errorf("error adding dog: %v", err)
}
Expand All @@ -147,9 +169,10 @@ func updateDog(dog Dog) error {
email = ?,
service = ?,
quantity = ?,
price = ?
price = ?,
grouping = ?
WHERE id = ?
`, dog.Name, dog.OwnerName, dog.Address, dog.City, dog.Email, dog.Service, dog.Quantity, dog.Price, dog.ID)
`, dog.Name, dog.OwnerName, dog.Address, dog.City, dog.Email, dog.Service, dog.Quantity, dog.Price, dog.Grouping, dog.ID)
if err != nil {
return fmt.Errorf("error updating dog: %v", err)
}
Expand Down
29 changes: 26 additions & 3 deletions data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ var testDog Dog = Dog{
Service: "walk",
Quantity: 1,
Price: 25,
Grouping: 2,
}

func TestInit(t *testing.T) {
Expand Down Expand Up @@ -80,9 +81,10 @@ func TestCreateTables(t *testing.T) {
email,
service,
quantity,
price
) VALUES (?, ?, ?, ?, ?, ?, ?, ?)
`, testDog.Name, testDog.OwnerName, testDog.Address, testDog.City, testDog.Email, testDog.Service, testDog.Quantity, testDog.Price)
price,
grouping
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
`, testDog.Name, testDog.OwnerName, testDog.Address, testDog.City, testDog.Email, testDog.Service, testDog.Quantity, testDog.Price, testDog.Grouping)

if err != nil {
t.Errorf("createTables() error = %q", err)
Expand All @@ -105,6 +107,27 @@ func TestCreateTables(t *testing.T) {
})
}

func TestUpdateTables(t *testing.T) {
err := updateTables()
if err != nil {
t.Errorf("updateTables() error = %q", err)
}

// Force error
oldDbUrl := dbUrl
dbUrl = "someBadUrl"
_ = connect()
err = updateTables()
if err == nil {
t.Errorf("no error returned when expected")
}

t.Cleanup(func() {
dbUrl = oldDbUrl
_ = connect()
})
}

func TestAddDog(t *testing.T) {
err := addDog(testDog)
if err != nil {
Expand Down
6 changes: 6 additions & 0 deletions views/modal-add.html
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ <h1>Add</h1>
placeholder="Price"
data-include-add=""
/>
<input
type="text"
name="grouping"
placeholder="Grouping"
data-include-add=""
/>
<br />
<br />
<div class="action-buttons">
Expand Down
7 changes: 7 additions & 0 deletions views/modal-edit.html
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@ <h1>Edit {{.Name}}</h1>
data-include-edit=""
value="{{.Price}}"
/>
<input
type="text"
name="grouping"
placeholder="Grouping"
data-include-edit=""
value="{{.Grouping}}"
/>
<br />
<br />
<div class="action-buttons">
Expand Down

0 comments on commit 4cd138b

Please sign in to comment.