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 { type Token struct {
Id string Id string
Name string
SpriteUri string SpriteUri string
X int X int
Y 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 SetMapImageUrl(table models.TableKey, url string) error
GetMapImageUrl(table models.TableKey) (string, error) GetMapImageUrl(table models.TableKey) (string, error)
AddToken(table models.TableKey, token models.Token) error AddToken(table models.TableKey, token models.Token, active boolean) error
RemoveToken(table models.TableKey, tokenId string) error RemoveToken(table models.TableKey, tokenId string, active boolean) error
ModifyToken(table models.TableKey, token models.Token) error ModifyToken(table models.TableKey, token models.Token, active boolean) error
GetTokens(table models.TableKey) ([]models.Token, error) GetTokens(table models.TableKey, active boolean) ([]models.Token, error)
} }
type DbEngine struct { type DbEngine struct {
client mongo.Client client mongo.Client
db mongo.Database
} }
func (self *DbEngine) mkCtx(timeoutSec int) context.Context { func (self *DbEngine) mkCtx(timeoutSec int) context.Context {
@ -52,9 +53,9 @@ func (self *DbEngine) Init(mongoUri string) error {
if err != nil { if err != nil {
return err return err
} }
defer client.Disconnect(ctx)
db := client.Database("felt") db := client.Database("felt")
self.db = db
err = self.ensureCollections(db) err = self.ensureCollections(db)
return err return err
@ -90,9 +91,10 @@ func (self *DbEngine) CreateTable(table models.TableKey) error {
_, err := tables.InsertOne(self.mkCtx(10), bson.D{ _, err := tables.InsertOne(self.mkCtx(10), bson.D{
{"name", table.Name}, {"name", table.Name},
{"passcode", table.Passcode}, {"passcode", table.Passcode},
{"mapUri", ""}, {"mapImageUrl", ""},
{"diceRolls", bson.A{}}, {"diceRolls", bson.A{}},
{"tokens", bson.A{}}, {"tokens", bson.A{}},
{"availableTokens", bson.A{}},
}) })
return err 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 { func (self *DbEngine) InsertDiceRoll(table models.TableKey, diceRoll models.DiceRoll) error {
tables := self.db.Collection("tables") tables := self.db.Collection("tables")
if tables != nil { if tables != nil {
var result bson.D var result models.Table
err := tables.FindOneAndUpdate( err := tables.FindOneAndUpdate(
self.mkCtx(10), self.mkCtx(10),
bson.D{ 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) { func (self *DbEngine) GetDiceRolls(table models.TableKey) ([]models.DiceRoll, error) {
tables := self.db.Colletion("tables") tables := self.db.Colletion("tables")
if tables != nil { if tables != nil {
fromDb := tables.findOne( var result models.Table
err := tables.findOne(
self.mkCtx(10), self.mkCtx(10),
bson.D{ bson.D{
{"name", table.Name}, {"name", table.Name},
{"passcode", table.Passcode}, {"passcode", table.Passcode},
}) }).Decode(&result)
if fromDb != nil { if err == nil {
rolls, ok := fromDb.Lookup("diceRolls").ArrayOK() return result.DiceRolls, nil
if ok {
return rolls, nil
} else {
return "", errors.New(fmt.Sprintf(errNoArray, "tables", table.Name, "diceRolls"))
}
} else { } else {
return "", errors.New(fmt.Sprintf(errNoDocument, table.Name, "tables")) 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 { func (self *DbEngine) SetMapImageUrl(table models.TableKey, url string) error {
tables := self.db.Collection("tables") tables := self.db.Collection("tables")
if tables != nil { if tables != nil {
var result bson.D var result models.Table
err := tables.FindOneAndUpdate( err := tables.FindOneAndUpdate(
self.mkCtx(10), self.mkCtx(10),
bson.D{ 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) { func (self *DbEngine) GetMapImageUrl(table models.TableKey) (string, error) {
tables := self.db.Collection("tables") tables := self.db.Collection("tables")
if tables != nil { if tables != nil {
fromDb := tables.FindOne( var result models.Table
err := tables.FindOne(
self.mkCtx(10), self.mkCtx(10),
bson.D{ bson.D{
{"name", table.Name}, {"name", table.Name},
{"passcode", table.Passcode}, {"passcode", table.Passcode},
}) }).Decode(&result)
if fromDb != nil { if err == nil {
url, ok := fromDb.Lookup("mapUri").StringValueOK() return result.MapImageUrl, nil
if ok {
return url, nil
} else {
return "", errors.New(fmt.Sprintf(errNotAString, "table", table.Name, "mapUri"))
}
} else { } else {
return "", errors.New(fmt.Sprintf(errNoDocument, table.Name, "tables")) 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")) return "", errors.New(fmt.Sprintf(errNoCollection, "tables"))
} }
func (self *DbEngine) AddToken(table models.TableKey, token models.Token) error { func (self *DbEngine) AddToken(table models.TableKey, token models.Token, active boolean) error {
return nil 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 { func (self *DbEngine) RemoveToken(table models.TableKey, tokenId string, active boolean) error {
return nil 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 { var result models.Table
return nil 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) { func (self *DbEngine) ModifyToken(table models.TableKey, token models.Token, active boolean) error {
return []models.Token{}, nil 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"))
} }