Successfully accessing rooms and areas

main
Nick Dumas 2 years ago
parent f2a703ed9f
commit a234bd6f9b

@ -2,4 +2,13 @@ module code.ndumas.com/ndumas/aardmaps
go 1.19
require github.com/mattn/go-sqlite3 v1.14.16
require (
github.com/mattn/go-sqlite3 v1.14.16
gorm.io/driver/sqlite v1.4.4
gorm.io/gorm v1.24.6
)
require (
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
)

@ -1,2 +1,13 @@
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
github.com/jinzhu/now v1.1.4/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
github.com/mattn/go-sqlite3 v1.14.15/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=
github.com/mattn/go-sqlite3 v1.14.16 h1:yOQRA0RpS5PFz/oikGwBEqvAWhWg5ufRz4ETLjwpU1Y=
github.com/mattn/go-sqlite3 v1.14.16/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=
gorm.io/driver/sqlite v1.4.4 h1:gIufGoR0dQzjkyqDyYSCvsYR6fba1Gw5YKDqKeChxFc=
gorm.io/driver/sqlite v1.4.4/go.mod h1:0Aq3iPO+v9ZKbcdiz8gLWRw5VOPcBOPUQJFLq5e2ecI=
gorm.io/gorm v1.24.0/go.mod h1:DVrVomtaYTbqs7gB/x2uVvqnXzv0nqjB396B8cG4dBA=
gorm.io/gorm v1.24.6 h1:wy98aq9oFEetsc4CAbKD2SoBCdMzsbSIvSUUFJuHi5s=
gorm.io/gorm v1.24.6/go.mod h1:L4uxeKpfBml98NYqVqwAdmV1a2nBtAec/cf3fpucW/k=

@ -24,7 +24,26 @@ func main() {
log.Fatalf("error creating AardMapper: %s\n", err)
}
log.Printf("databse opened: %#+v\n", am)
areas, _ := am.Areas()
log.Printf("databse opened: %#+v\n", areas)
areas, err := am.Areas()
if err != nil {
log.Fatalf("error fetching Areas: %s\n", err)
}
log.Printf("found %d areas\n", len(areas))
for k, v := range areas {
log.Printf("found Area: %s\n", k)
log.Printf("%#+v\n", v)
break
}
rooms, err := am.Rooms()
if err != nil {
log.Fatalf("error fetching Rooms: %s\n", err)
}
log.Printf("found %d rooms\n", len(rooms))
for k, v := range rooms {
log.Printf("found Room: (%s,%s)[%s]\n", v.Name, v.Area, k)
log.Printf("%#+v\n", v)
break
}
}

@ -1,46 +1,70 @@
package main
import (
"database/sql"
"fmt"
"log"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)
func NewMapper(fn string) (AardMapper, error) {
var am AardMapper
db, err := sql.Open("sqlite3", fn)
db, err := gorm.Open(sqlite.Open(fn), &gorm.Config{})
db.AutoMigrate(&Area{})
db.AutoMigrate(&Room{})
if err != nil {
return am, fmt.Errorf("error opening mapper sqlite3 database: %s\n", err)
}
am.db = db
am.DB = db
return am, nil
}
type AardMapper struct {
db *sql.DB
DB *gorm.DB
}
func (am AardMapper) Areas() (map[string]Area, error) {
areas := make(map[string]Area)
rows, err := am.db.Query("select uid, name, flags from areas;")
x := make([]Area, 0)
tx := am.DB.Find(&x)
if err != nil {
log.Fatalf("error querying areas: %s\n", err)
if tx.Error != nil {
return nil, fmt.Errorf("error fetching areas: %w", tx.Error)
}
for rows.Next() {
var a Area
err := rows.Scan(&a.Uid, &a.Name, &a.Flags)
for _, a := range x {
areas[a.Uid] = a
if err != nil {
return areas, fmt.Errorf("error scanning area into struct: %s\n", err)
}
}
return areas, nil
}
func (am AardMapper) Rooms() (map[string]Room, error) {
rooms := make(map[string]Room)
x := make([]Room, 0)
tx := am.DB.Find(&x)
if tx.Error != nil {
return nil, fmt.Errorf("error fetching areas: %w", tx.Error)
}
for _, a := range x {
rooms[a.Uid] = a
}
return rooms, nil
}
type Area struct {
Uid, Name, Flags string
gorm.Model
Uid string
Name, Flags string
}
type Room struct {
gorm.Model
Uid string
Name, Area string
Building string
Info, Notes, Flags string
X, Y, Z int
Norecall, Noportal int
}

Loading…
Cancel
Save