models: add Table model; mongodb/adapter: finish implementing adapter

This commit is contained in:
Iris Lightshard 2022-11-15 20:43:09 -07:00
parent 298022e4e9
commit e6faf9478a
Signed by: Iris Lightshard
GPG key ID: 3B7FBC22144E6398
2 changed files with 123 additions and 35 deletions

View file

@ -19,7 +19,17 @@ type DiceRoll struct {
type Token struct {
Id string
Name string
SpriteUri string
X int
Y int
}
type Table struct {
Name string
Passcode string
MapImageUrl string
DiceRolls []DiceRoll
Tokens []Token
AvailableTokens []Token
}

View file

@ -26,14 +26,15 @@ type DbAdapter interface {
SetMapImageUrl(table models.TableKey, url string) error
GetMapImageUrl(table models.TableKey) (string, error)
AddToken(table models.TableKey, token models.Token) error
RemoveToken(table models.TableKey, tokenId string) error
ModifyToken(table models.TableKey, token models.Token) error
GetTokens(table models.TableKey) ([]models.Token, error)
AddToken(table models.TableKey, token models.Token, active boolean) error
RemoveToken(table models.TableKey, tokenId string, active boolean) error
ModifyToken(table models.TableKey, token models.Token, active boolean) error
GetTokens(table models.TableKey, active boolean) ([]models.Token, error)
}
type DbEngine struct {
client mongo.Client
db mongo.Database
}
func (self *DbEngine) mkCtx(timeoutSec int) context.Context {
@ -52,9 +53,9 @@ func (self *DbEngine) Init(mongoUri string) error {
if err != nil {
return err
}
defer client.Disconnect(ctx)
db := client.Database("felt")
self.db = db
err = self.ensureCollections(db)
return err
@ -90,9 +91,10 @@ func (self *DbEngine) CreateTable(table models.TableKey) error {
_, err := tables.InsertOne(self.mkCtx(10), bson.D{
{"name", table.Name},
{"passcode", table.Passcode},
{"mapUri", ""},
{"mapImageUrl", ""},
{"diceRolls", bson.A{}},
{"tokens", bson.A{}},
{"availableTokens", bson.A{}},
})
return err
}
@ -115,7 +117,7 @@ func (self *DbEngine) DestroyTable(table models.TableKey) error {
func (self *DbEngine) InsertDiceRoll(table models.TableKey, diceRoll models.DiceRoll) error {
tables := self.db.Collection("tables")
if tables != nil {
var result bson.D
var result models.Table
err := tables.FindOneAndUpdate(
self.mkCtx(10),
bson.D{
@ -139,19 +141,15 @@ func (self *DbEngine) InsertDiceRoll(table models.TableKey, diceRoll models.Dice
func (self *DbEngine) GetDiceRolls(table models.TableKey) ([]models.DiceRoll, error) {
tables := self.db.Colletion("tables")
if tables != nil {
fromDb := tables.findOne(
var result models.Table
err := tables.findOne(
self.mkCtx(10),
bson.D{
{"name", table.Name},
{"passcode", table.Passcode},
})
if fromDb != nil {
rolls, ok := fromDb.Lookup("diceRolls").ArrayOK()
if ok {
return rolls, nil
} else {
return "", errors.New(fmt.Sprintf(errNoArray, "tables", table.Name, "diceRolls"))
}
}).Decode(&result)
if err == nil {
return result.DiceRolls, nil
} else {
return "", errors.New(fmt.Sprintf(errNoDocument, table.Name, "tables"))
}
@ -162,7 +160,7 @@ func (self *DbEngine) GetDiceRolls(table models.TableKey) ([]models.DiceRoll, er
func (self *DbEngine) SetMapImageUrl(table models.TableKey, url string) error {
tables := self.db.Collection("tables")
if tables != nil {
var result bson.D
var result models.Table
err := tables.FindOneAndUpdate(
self.mkCtx(10),
bson.D{
@ -183,19 +181,15 @@ func (self *DbEngine) SetMapImageUrl(table models.TableKey, url string) error {
func (self *DbEngine) GetMapImageUrl(table models.TableKey) (string, error) {
tables := self.db.Collection("tables")
if tables != nil {
fromDb := tables.FindOne(
var result models.Table
err := tables.FindOne(
self.mkCtx(10),
bson.D{
{"name", table.Name},
{"passcode", table.Passcode},
})
if fromDb != nil {
url, ok := fromDb.Lookup("mapUri").StringValueOK()
if ok {
return url, nil
} else {
return "", errors.New(fmt.Sprintf(errNotAString, "table", table.Name, "mapUri"))
}
}).Decode(&result)
if err == nil {
return result.MapImageUrl, nil
} else {
return "", errors.New(fmt.Sprintf(errNoDocument, table.Name, "tables"))
}
@ -204,17 +198,101 @@ func (self *DbEngine) GetMapImageUrl(table models.TableKey) (string, error) {
return "", errors.New(fmt.Sprintf(errNoCollection, "tables"))
}
func (self *DbEngine) AddToken(table models.TableKey, token models.Token) error {
return nil
func (self *DbEngine) AddToken(table models.TableKey, token models.Token, active boolean) error {
tables := self.db.Collection("tables")
if tables != nil {
tokenArrKey := "tokens"
if !active {
tokenArrKey = "availableTokens"
}
var result models.Table
err := tables.FindOneAndUpdate(
self.mkCtx(10),
bson.D{
{"name", table.Name},
{"passcode", table.Passcode},
},
bson.D{
{"$push", bson.D{tokenArrKey, token}},
},
).Decode(&result)
return err
}
return errors.New(fmt.Sprintf(errNoCollection, "tables"))
}
func (self *DbEngine) RemoveToken(table models.TableKey, tokenId string) error {
return nil
func (self *DbEngine) RemoveToken(table models.TableKey, tokenId string, active boolean) error {
tables := self.db.Collection("tables")
if tables != nil {
tokenArrKey := "tokens"
if !active {
tokenArrKey = "availableTokens"
}
func (self *DbEngine) ModifyToken(table models.TableKey, token models.Token) error {
return nil
var result models.Table
err := tables.FindOneAndUpdate(
self.mkCtx(10),
bson.D{
{"name", table.Name},
{"passcode", table.Passcode},
},
bson.D{
{"$pull", bson.D{tokenArrKey, bson.D{"_id", tokenId}}},
},
).Decode(&result)
return err
}
return errors.New(fmt.Sprintf(errNoCollection, "tables"))
}
func (self *DbEngine) GetTokens(table models.TableKey) ([]models.Token, error) {
return []models.Token{}, nil
func (self *DbEngine) ModifyToken(table models.TableKey, token models.Token, active boolean) error {
tables := self.db.Collection("tables")
if tables != nil {
tokenArrKey := "tokens"
if !active {
tokenArrKey = "availableTokens"
}
var result models.Table
err := tables.FindOneAndUpdate(
self.mkCtx(10),
bson.D{
{"name", table.Name},
{"passcode", table.Passcode},
{tokenArrKey, bson.D{"_id", tokenId}},
},
bson.D{
{"$set", bson.D{tokenArrKey + ".$", bson.D{
{"name", token.Name},
{"spriteUri", token.SpriteUri},
{"x", token.X},
{"y", token.Y},
}}},
},
).Decode(&result)
return err
}
return errors.New(fmt.Sprintf(errNoCollection, "tables"))
}
func (self *DbEngine) GetTokens(table models.TableKey, active boolean) ([]models.Token, error) {
tables := self.db.Collection("tables")
if tables != nil {
var result models.Table
err := tables.FindOne(
self.mkCtx(10),
bson.D{
{"name", table.Name},
{"passcode", table.Passcode},
}).Decode(&result)
if err == nil {
if active {
return result.Tokens
} else {
return result.AvailableTokens
}
} else {
return nil, errors.New(fmt.Sprintf(errNoDocument, table.Name, "tables"))
}
}
return nil, errors.New(fmt.Sprintf(errNoCollection, "tables"))
}