mongodb/adapter: implement Insert/Get for DiceRolls
This commit is contained in:
parent
07d797e1df
commit
298022e4e9
1 changed files with 60 additions and 19 deletions
|
@ -12,6 +12,7 @@ import (
|
||||||
const errNoCollection string = "collection not found: felt.%s"
|
const errNoCollection string = "collection not found: felt.%s"
|
||||||
const errNoDocument string = "document with name/id '%s' doesn't exist in collection: %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 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 {
|
type DbAdapter interface {
|
||||||
Init(mongoUri string) error
|
Init(mongoUri string) error
|
||||||
|
@ -112,17 +113,57 @@ 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 {
|
||||||
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) {
|
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 {
|
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 {
|
||||||
_, err := tables.UpdateOne(
|
var result bson.D
|
||||||
|
err := tables.FindOneAndUpdate(
|
||||||
self.mkCtx(10),
|
self.mkCtx(10),
|
||||||
bson.D{
|
bson.D{
|
||||||
{"name", table.Name},
|
{"name", table.Name},
|
||||||
|
@ -131,7 +172,7 @@ func (self *DbEngine) SetMapImageUrl(table models.TableKey, url string) error {
|
||||||
bson.D{
|
bson.D{
|
||||||
{"mapImageUrl", url},
|
{"mapImageUrl", url},
|
||||||
},
|
},
|
||||||
)
|
).Decode(&result)
|
||||||
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue