all repos — felt @ 11d6463f627f7e54f6bf74e924afb77e9f268976

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

more db infrastructure
Iris Lightshard nilix@nilfm.cc
PGP Signature
-----BEGIN PGP SIGNATURE-----

iQIzBAABCAAdFiEEkFh6dA+k/6CXFXU4O3+8IhROY5gFAmNt+IkACgkQO3+8IhRO
Y5gNzw/8DtEolLS6NGPszfnlNMfOybE8iq6Y7VRpr39loMHe7qWif4HCcUQWvTa/
Non+h7FtS0soX20d/Td13eRDqS31jYFkmWFy8ikMan0PeP9gWQhQuOp3YQOK7+Cd
6d/rnOlKr3dFNFrdfxRmnYRKd3h3TrqH/r6/EAmUmXEBFxr9d/bg8iB7hv2tyIPL
2/5ziZMcWEmrwbErOl8SRkjKnpU6WyxexkuLe2OpON03MD0tBZnJx5M+U3KRUAbQ
q0OS1bLIwSwKTHQXymbnOv1SbWesOMeZuIbZCjLjRxEPichxCUjs1CuJ3RWIjDLz
1uoxt6wrxo2ttrxqAsbroBV1JER0yLhIPz3gGbLDYy/ZOu1r1bVAUJaVi43JBf5L
DKbcRcNOFsZSye3UUtpw5stWdgLLlBAURTyX73xnYyl6iizGA4dNd8QM2SS3zTX9
EpYUq/vBd4liBuCQelhKW6BCplZHHBOYlHruuJ2S3Kw60mCbTktCwbHhh5FVsUY7
ySi5N5dXgPuoXpbg33VS2M8nd1WYRlGkq8CrxZL1L0CWS8OqK1bHSem1VQa6tyRk
RwTfXD/jH2DKAKX9UxdZoaJ70Lyre6UkUf+PsdDp9Qt/oyckFk/SUwEe+tksZ30Y
etUBSMdWWZEnpS8W/wIsKpmUiuh5jne7v83TSA1QrmONI5Cidsg=
=Y0W5
-----END PGP SIGNATURE-----
commit

11d6463f627f7e54f6bf74e924afb77e9f268976

parent

e59f3e1609f9715c90291be2e950b6f8ce393461

1 files changed, 51 insertions(+), 2 deletions(-)

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

@@ -9,6 +9,10 @@ "nilfm.cc/git/felt/models"

"time" ) +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" + type DbAdapter interface { Init(mongoUri string) error

@@ -82,14 +86,59 @@

func (self *DbEngine) CreateTable(table models.TableKey) error { tables := self.db.Collection("tables") if tables != nil { - _, err := tables.insertOne(self.mkCtx(10), bson.D{ + _, err := tables.InsertOne(self.mkCtx(10), bson.D{ {"name", table.Name}, {"passcode", table.Passcode}, {"mapUri", ""}, {"diceRolls", bson.A{}}, {"tokens", bson.A{}}, }) + return err } + return errors.New(fmt.Sprintf(errNoCollection, "tables")) - return err +} + +func (self *DbEngine) SetMapImageUrl(table models.TableKey, url string) error { + tables := self.db.Collection("tables") + if tables != nil { + _, err := tables.UpdateOne( + self.mkCtx(10), + bson.D{ + {"name", table.Name}, + {"passcode", table.Passcode}, + }, + bson.D{ + {"mapImageUrl", url}, + }, + ) + + return err + } + + return errors.New(fmt.Sprintf(errNoCollection, "tables")) +} + +func (self *DbEngine) GetMapImageUrl(table models.TableKey) (string, error) { + tables := self.db.Collection("tables") + if tables != nil { + fromDb := 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")) + } + } else { + return "", errors.New(fmt.Sprintf(errNoDocument, table.Name, "tables")) + } + } + + return "", errors.New(fmt.Sprintf(errNoCollection, "tables")) }