all repos — felt @ 298022e4e9ab2a339b71691db945382103447d84

virtual tabletop for dungeons and dragons (and similar) using Go, MongoDB, and websockets

mongodb/adapter: implement Insert/Get for DiceRolls
Iris Lightshard nilix@nilfm.cc
PGP Signature
-----BEGIN PGP SIGNATURE-----

iQIzBAABCAAdFiEEkFh6dA+k/6CXFXU4O3+8IhROY5gFAmNx0fAACgkQO3+8IhRO
Y5hcpQ/8CGwKGeRUmMKqlBp629+bRlbru4HK/mwnAWEn4dQCGm/HudB6XE0Xj0TA
WOGeyNtHXY+9p3ve+FdVILNjsh3XRh0K87XmaFdN0/4aQ00yofHADi/z6bEJ+Sx7
hLMgN39si4QBpPzXNc88/R5cM6Iz9BFq506cgxh/I3w1X0qkJ+z+1PR6KPu3IHHs
oIevKGdEKVEg8zmIyP1OjCSoClf/LU3YqdTLkRF2ZsGPqa9xo1/DPHUmFvp0nX+h
P2cciUVbitpb7/AJ03ZPR3QR8AJK70FFvqKUIAHs+cMC/Dw6QZ0L0FkCq4g3xD2q
Ef1BMOZ7GwIeDC2OfcZyRCPib5j4Jl0SZnNB8hKpYTV8lxinY7R6NBHzB9C0gckO
WH3nmFV0b6jdUL8NQI94eNu60s7TWC6x5nGrE9w6C7FHa1NYFkBE8tHXzveUWkUR
ThkQbW+mwgjF3aXKd6zjGlZfTJguMfAGRmQiO0rz39Wh+uBWKwOlZC4tVKrQ72DX
tBEXkTS2+BA/QZvRPQ5qDh0yybVhyLsmHojNwzla1c/3hjMTtd6xK5pYpkcciy9W
n9C/w7uveEgXs1cBvmBt6YriO0oGaz3q9PjGz3oyD0zsypOTr0QWRH3YkeOyv+rX
cL/dTv5U3rdWA+v2hdWJ1mtc4on5zgWy/OTVkCjwlM2oKIkX1/I=
=5BXd
-----END PGP SIGNATURE-----
commit

298022e4e9ab2a339b71691db945382103447d84

parent

07d797e1dfd485038bf98962cef55335b8cf4ce4

1 files changed, 60 insertions(+), 19 deletions(-)

jump to
M mongodb/adapter.gomongodb/adapter.go

@@ -12,6 +12,7 @@

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) 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 @@ },

bson.D{ {"mapImageUrl", url}, }, - ) + ).Decode(&result) return err }

@@ -164,16 +205,16 @@ return "", errors.New(fmt.Sprintf(errNoCollection, "tables"))

} 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 +}