private BsonDocument SerializeEvent(IEvent @event) { var document = new BsonDocument(); var settings = new BsonDocumentWriterSettings { GuidRepresentation = GuidRepresentation.Standard }; using (var writer = new BsonDocumentWriter(document, settings)) { BsonSerializer.Serialize(writer, typeof(IEvent), @event); } return(document); }
public virtual void Add(T item) { var database = client.GetServer().GetDatabase("tabletop"); var bsonDoc = new BsonDocument(); var bsonDocumentWriterSettings = new BsonDocumentWriterSettings(); bsonDocumentWriterSettings.GuidRepresentation = GuidRepresentation.Standard; var bsonDocumentWriter = new BsonDocumentWriter(bsonDoc, bsonDocumentWriterSettings); BsonSerializer.Serialize(bsonDocumentWriter, item); var collection = database.GetCollection(Util.Mongo.MongoUtilities.GetCollectionFromType(item.GetType())); var saveOptions = new MongoInsertOptions(); saveOptions.WriteConcern = WriteConcern.Acknowledged; var succeeded = collection.Save(bsonDoc, saveOptions); if (!succeeded.Ok) { throw new Exception(succeeded.LastErrorMessage); } }
/// <summary> /// Adds the item to the collection. Modifies the item to add an id that can be used to retrieve the item afterwards. /// </summary> /// <param name="item">The item to add to the collection.</param> public override void Add(Character item) { if (!string.IsNullOrWhiteSpace(item._id) && this.Contains <Character>(item)) { throw new ArgumentException("Item already exists in collection."); } if (string.IsNullOrWhiteSpace(item.PlayerAccountName)) { throw new ArgumentException("A player Id is required for the character."); } var database = client.GetServer().GetDatabase("tabletop"); // Verify that the player exists in the database that we are going to be adding the character to. var playerColl = database.GetCollection(Util.Mongo.MongoUtilities.GetCollectionFromType(typeof(Player))); if (playerColl == null) { throw new Exception("No player collection currently in database."); } var player = playerColl.AsQueryable <Player>().FirstOrDefault(x => x.AccountName == item.PlayerAccountName); //var playerDoc = playerColl.FindOneById(new ObjectId(item.PlayerAccountName)); if (player == null) { throw new Exception("No player found with Id specified."); } try { // Create the id here because then I can add it to the character. var bsonId = ObjectId.GenerateNewId(DateTime.Now); item._id = bsonId.ToString(); base.Add(item); // character has been successfully saved. // now need to update the character ids of this player. // code to update the player that got this character if (player.CharacterIds == null) { player.CharacterIds = new List <string>(); } player.CharacterIds.Add(bsonId.ToString()); var playerDoc = new BsonDocument(); var bsonDocumentWriterSettings = new BsonDocumentWriterSettings(); bsonDocumentWriterSettings.GuidRepresentation = GuidRepresentation.Standard; var bsonDocumentWriter = new BsonDocumentWriter(playerDoc, bsonDocumentWriterSettings); BsonSerializer.Serialize(bsonDocumentWriter, player); var succeeded = playerColl.Save(playerDoc); if (!succeeded.Ok) { throw new Exception("Character saved, player unsuccessfully updated. " + succeeded.LastErrorMessage); } } catch (Exception ex) { // One of the saves failed. Pass it up. throw ex; } }
/// <summary> /// Adds a new acc /// </summary> /// <param name="registerBindingModel"></param> /// <returns></returns> public Account Add(RegisterBindingModel registerBindingModel) { var collection = _client.GetServer().GetDatabase("tabletop").GetCollection(Util.Mongo.MongoUtilities.GetCollectionFromType(typeof(Account))); if (collection == null) { throw new Exception("No account collection in database."); } var hashDict = HashPassword(registerBindingModel.Password); if (!hashDict.ContainsKey("hash") || !hashDict.ContainsKey("salt")) { throw new Exception("Could not hash password properly."); } var id = ObjectId.GenerateNewId(DateTime.Now).ToString(); var account = new Account() { _id = id, name = registerBindingModel.Name, email = registerBindingModel.Email, salt = hashDict["salt"], hash = hashDict["hash"] }; var bsonDoc = new BsonDocument(); var bsonDocumentWriterSettings = new BsonDocumentWriterSettings(); bsonDocumentWriterSettings.GuidRepresentation = GuidRepresentation.Standard; var bsonDocumentWriter = new BsonDocumentWriter(bsonDoc, bsonDocumentWriterSettings); BsonSerializer.Serialize(bsonDocumentWriter, account); var saveOptions = new MongoInsertOptions(); saveOptions.WriteConcern = WriteConcern.Acknowledged; var succeeded = collection.Save(bsonDoc, saveOptions); if (!succeeded.Ok) { throw new Exception(succeeded.LastErrorMessage); } var playerId = ObjectId.GenerateNewId(DateTime.Now).ToString(); var player = new Player() { _id = playerId, AccountName = account.name, Email = account.email, JoinDate = DateTime.Now }; var playerCollection = _client.GetServer().GetDatabase("tabletop").GetCollection(Util.Mongo.MongoUtilities.GetCollectionFromType(player.GetType())); bsonDoc = new BsonDocument(); bsonDocumentWriter = new BsonDocumentWriter(bsonDoc, bsonDocumentWriterSettings); BsonSerializer.Serialize(bsonDocumentWriter, player); succeeded = playerCollection.Save(bsonDoc, saveOptions); if (!succeeded.Ok) { throw new Exception(succeeded.LastErrorMessage); } return(account); }