You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
56 lines
1017 B
Go
56 lines
1017 B
Go
2 years ago
|
package main
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
|
||
|
"gorm.io/driver/sqlite"
|
||
|
"gorm.io/gorm"
|
||
|
)
|
||
|
|
||
|
func NewMapper(fn string) (AardMapper, error) {
|
||
|
var am AardMapper
|
||
|
|
||
|
db, err := gorm.Open(sqlite.Open(fn), &gorm.Config{})
|
||
|
db.AutoMigrate(&Area{})
|
||
|
db.AutoMigrate(&Room{})
|
||
|
db.AutoMigrate(&Exit{})
|
||
|
if err != nil {
|
||
|
|
||
|
return am, fmt.Errorf("error opening mapper sqlite3 database: %s\n", err)
|
||
|
}
|
||
|
am.DB = db
|
||
|
return am, nil
|
||
|
}
|
||
|
|
||
|
type AardMapper struct {
|
||
|
DB *gorm.DB
|
||
|
}
|
||
|
|
||
|
func (am AardMapper) Areas() (map[string]Area, error) {
|
||
|
areas := make(map[string]Area)
|
||
|
x := make([]Area, 0)
|
||
|
tx := am.DB.Find(&x)
|
||
|
|
||
|
if tx.Error != nil {
|
||
|
return nil, fmt.Errorf("error fetching areas: %w", tx.Error)
|
||
|
}
|
||
|
for _, a := range x {
|
||
|
areas[a.Uid] = a
|
||
|
}
|
||
|
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
|
||
|
}
|