Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 807c42061b | |||
| be28d18723 |
14
db/seed.go
14
db/seed.go
@@ -58,12 +58,12 @@ func GetSeedData() (items []Item, boxes []Box, boxitems []BoxItem) {
|
||||
func CreateTables(client *sql.DB) (int64, error) {
|
||||
script, err := os.ReadFile("/home/mikayla/go/go-htmx-tailwind-example/db/seed.sql")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
return -1, err
|
||||
}
|
||||
|
||||
result, err := client.Exec(string(script))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
return -1, err
|
||||
}
|
||||
|
||||
return result.RowsAffected()
|
||||
@@ -86,6 +86,11 @@ func SeedDB() (int64, error) {
|
||||
for i := range(items) {
|
||||
_, err := PostItem(items[i])
|
||||
if err != nil {
|
||||
// ignore unique constraint violations and continue
|
||||
if err.Error() == "UNIQUE constraint failed: items.Name" {
|
||||
continue
|
||||
}
|
||||
|
||||
return -1, err
|
||||
}
|
||||
insertCount++
|
||||
@@ -94,6 +99,11 @@ func SeedDB() (int64, error) {
|
||||
for i := range(boxes) {
|
||||
_, err := PostBox(boxes[i])
|
||||
if err != nil {
|
||||
// ignore unique constraint violations and continue
|
||||
if err.Error() == "UNIQUE constraint failed: boxes.Name" {
|
||||
continue
|
||||
}
|
||||
|
||||
return -1, err
|
||||
}
|
||||
insertCount++
|
||||
|
||||
8
main.go
8
main.go
@@ -31,10 +31,14 @@ func main() {
|
||||
|
||||
//exit process immediately upon sigterm
|
||||
handleSigTerms()
|
||||
db.SeedDB()
|
||||
i, err := db.SeedDB()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
fmt.Printf("seeded db with %d records\n", i)
|
||||
|
||||
//parse templates
|
||||
var err error
|
||||
html, err = web.TemplateParseFSRecursive(templateFS, ".html", true, nil)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
|
||||
15
util/main.go
Normal file
15
util/main.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func GetIDFromPath(r *http.Request) (id int, err error) {
|
||||
path := r.URL.Path
|
||||
segments := strings.Split(path, "/")
|
||||
last := segments[len(segments)-1]
|
||||
id, err = strconv.Atoi(last)
|
||||
return
|
||||
}
|
||||
Reference in New Issue
Block a user