mongodb/adapter: implement Insert/Get for DiceRolls

This commit is contained in:
Iris Lightshard 2022-11-13 22:28:16 -07:00
parent 07d797e1df
commit 298022e4e9
Signed by: nilix
GPG key ID: 3B7FBC22144E6398

View file

@ -12,6 +12,7 @@ import (
const errNoCollection string = "collection not found: felt.%s"
const errNoDocument string = "document with name/id '%s' doesn't exist in collection: %s"
const errNotAString string = "document property is not a string: %s<key=%s>.%s"
const errNotAnArray string = "doccument property is not an array: %s<key=%s>.%s"
type DbAdapter interface {
Init(mongoUri string) error
@ -100,29 +101,69 @@ func (self *DbEngine) CreateTable(table models.TableKey) error {
}
func (self *DbEngine) DestroyTable(table models.TableKey) error {
tables := self.db.Collection("tables")
if tables != nil {
_, err := tables.DeleteOne(self.mkCtx(10), bson.D{
{"name", table.Name},
{"passcode", table.Passcode},
})
return err
}
return errors.New(fmt.Sprintf(errNoCollection, "tables"))
tables := self.db.Collection("tables")
if tables != nil {
_, err := tables.DeleteOne(self.mkCtx(10), bson.D{
{"name", table.Name},
{"passcode", table.Passcode},
})
return err
}
return errors.New(fmt.Sprintf(errNoCollection, "tables"))
}
func (self *DbEngine) InsertDiceRoll(table models.TableKey, diceRoll models.DiceRoll) error {
return errors.New(fmt.Sprintf(errNoCollections, "tables"))
tables := self.db.Collection("tables")
if tables != nil {
var result bson.D
err := tables.FindOneAndUpdate(
self.mkCtx(10),
bson.D{
{"name", table.Name},
{"passcode", table.Passcode},
},
bson.D{
{"$push", bson.D{
"diceRolls", bson.D{
{"$each", []models.DiceRoll{diceRoll}},
{"$slice", 1000},
},
}},
},
).Decode(&result)
return err
}
return errors.New(fmt.Sprintf(errNoCollection, "tables"))
}
func (self *DbEngine) GetDiceRolls(table models.TableKey) ([]models.DiceRoll, error) {
return []models.DiceRoll{}, nil
tables := self.db.Colletion("tables")
if tables != nil {
fromDb := 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"))
}
} else {
return "", errors.New(fmt.Sprintf(errNoDocument, table.Name, "tables"))
}
}
return "", errors.New(fmt.Sprintf(errNoCollection, "tables"))
}
func (self *DbEngine) SetMapImageUrl(table models.TableKey, url string) error {
tables := self.db.Collection("tables")
if tables != nil {
_, err := tables.UpdateOne(
var result bson.D
err := tables.FindOneAndUpdate(
self.mkCtx(10),
bson.D{
{"name", table.Name},
@ -131,7 +172,7 @@ func (self *DbEngine) SetMapImageUrl(table models.TableKey, url string) error {
bson.D{
{"mapImageUrl", url},
},
)
).Decode(&result)
return err
}
@ -164,16 +205,16 @@ func (self *DbEngine) GetMapImageUrl(table models.TableKey) (string, error) {
}
func (self *DbEngine) AddToken(table models.TableKey, token models.Token) error {
return nil
return nil
}
func (self *DbEngine) RemoveToken(table models.TableKey, tokenId string) error {
return nil
func (self *DbEngine) RemoveToken(table models.TableKey, tokenId string) error {
return nil
}
func (self *DbEngine) ModifyToken(table models.TableKey, token models.Token) error {
return nil
return nil
}
func (self *DbEngine) GetTokens(table models.TableKey) ([]models.Token, error) {
return []models.Token{}, nil
}
return []models.Token{}, nil
}