public HttpResponseMessage PostBabyEvent([FromBody] BabyEventModel babyEventModel, [ValueProvider(typeof(HeaderValueProviderFactory <string>))] string sessionKey, string babyProfileId) { HttpResponseMessage responseMsg = this.PerformOperationAndHandleExceptions( () => { var usersWithSpecificId = from u in this.db.GetCollection <User>("usersInfo").AsQueryable() where u.SessionKey == sessionKey select u; User selectedUser = usersWithSpecificId.FirstOrDefault(); if (selectedUser == null) { throw new NullReferenceException("User is logged out or does not exist!"); } BabyEvent babyEvent = new BabyEvent() { Title = babyEventModel.Title, Date = babyEventModel.Date, Description = babyEventModel.Description, PictureNames = babyEventModel.PictureNames }; var collection = this.db.GetCollection("baby" + babyProfileId); collection.Insert <BabyEvent>(babyEvent); var response = this.Request.CreateResponse(HttpStatusCode.Created, babyEvent.Id.ToString()); return(response); } ); return(responseMsg); }
private void ChangePropertiesOfBabyProfile( BabyEventModel babyEventModel, BabyEvent selectedBabyEvent, MongoCollection babyEvents) { if (babyEventModel.Title != null) { var query = new QueryDocument { { "_id", selectedBabyEvent.Id } }; var update = new UpdateDocument { { "$set", new BsonDocument("Title", babyEventModel.Title) } }; babyEvents.Update(query, update); } if (babyEventModel.Description != null) { var query = new QueryDocument { { "_id", selectedBabyEvent.Id } }; var update = new UpdateDocument { { "$set", new BsonDocument("Description", babyEventModel.Description) } }; babyEvents.Update(query, update); } if (babyEventModel.Date != null) { var query = new QueryDocument { { "_id", selectedBabyEvent.Id } }; var update = new UpdateDocument { { "$set", new BsonDocument("Date", babyEventModel.Date) } }; babyEvents.Update(query, update); } }