Successfully accessing rooms and areas
parent
f2a703ed9f
commit
a234bd6f9b
@ -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=
|
||||
|
@ -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…
Reference in New Issue