public void Save(FeedbackSummary entity)
        {
            try
            {
                IMongoQuery queryBase = Query.And
                    (
                        Query<FeedbackSummary>.EQ<DateTime>(mem => mem.Date, entity.Date),
                        Query<FeedbackSummary>.EQ<Guid>(mem => mem.ApplicationId, entity.ApplicationId),
                        Query<FeedbackSummary>.EQ<string>(mem => mem.Version, entity.Version),
                        Query<FeedbackSummary>.EQ<PlatformType>(mem => mem.PlatformId, entity.PlatformId)
                    );

                IMongoUpdate update = Update<FeedbackSummary>
                    .SetOnInsert(x => x.Version, entity.Version)
                    .SetOnInsert(x => x.Date, entity.Date)
                    .SetOnInsert(x => x.ApplicationId, entity.ApplicationId)
                    .SetOnInsert(x => x.PlatformId, entity.PlatformId)
                    .SetOnInsert(x => x.Ratings, new List<RatingAggregate>())
                    .Inc(mem => mem.Count, entity.Count)
                    .Inc(mem => mem.SumOfRatings, entity.SumOfRatings);

                this.GetCollection<FeedbackSummary>()
                    .FindAndModify(queryBase, SortBy.Descending("Date"), update, false, true);
                this.GetCollection<FeedbackSummary>().EnsureIndex(IndexKeys.Descending("Date"));

                IMongoQuery queryFeedbackInsert = Query.And
                    (
                        queryBase,
                        Query.NE("Ratings.Key", BsonValue.Create(entity.Ratings.First().Key))
                    );

                IMongoUpdate updateFeedback = Update
                    .Push("Ratings", BsonValue.Create(entity.Ratings.First().CopyOnlyKey().ToBsonDocument()));

                this.GetCollection<FeedbackSummary>().Update(queryFeedbackInsert, updateFeedback);

                IMongoQuery queryFeedbackUpdate = Query.And
                    (
                        queryBase,
                        Query.EQ("Ratings.Key", BsonValue.Create(entity.Ratings.First().Key))
                    );

                IMongoUpdate updateInc = Update
                    .Inc("Ratings.$.Rating", entity.Ratings.First().Rating)
                    .Inc("Ratings.$.Count", 1);

                this.GetCollection<FeedbackSummary>().Update(queryFeedbackUpdate, updateInc);
            }
            catch (Exception ex)
            {
                throw new DataAccessLayerException(ex);
            }
        }
 public void Save(FeedbackSummary entity)
 {
     this.feedbackMapper.Save(entity);
 }
        public void Log(Feedback feedbackItem)
        {
            Application application = this.applicationRepository.Find(feedbackItem.ApplicationId);

            if (application != null)
            {
                DeviceInfo device = this.deviceRepository.Find(feedbackItem.DeviceId);

                if (device != null)
                {
                    feedbackItem.PlatformId = device.PlatformType;

                    FeedbackSummary feedbackSum = new FeedbackSummary(feedbackItem);

                    this.feedbackRepository.Save(feedbackSum);
                    this.feedbackRepository.Save(feedbackItem);
                }
                else
                {
                    throw new NoDeviceException(feedbackItem.DeviceId);
                }
            }
            else
            {
                throw new InactiveApplicationException(feedbackItem.ApplicationId);
            }
        }
        public void Save_SummaryRecord_MatchesSummary()
        {
            FeedbackMapper feedbackMapper = new FeedbackMapper(this.client, this.database);
            Guid applicationId = Guid.NewGuid();
            string screenName = "screenName";

            FeedbackSummary expected = new FeedbackSummary()
            {
                ApplicationId = applicationId,
                Count = 2,
                Date = date,
                PlatformId = platform,
                Ratings = new List<RatingAggregate>()
                {
                     new RatingAggregate(screenName, 8)
                     {
                         Count = 2
                     }
                },
                Version = version,
                SumOfRatings = 8
            };

            Feedback feedback = new Feedback()
                {
                    ApplicationId = applicationId,
                    DeviceId = Guid.NewGuid(),
                    Message = "feedback",
                    Rating =  FeedbackRatingType.Five,
                    ScreenName =  screenName,
                    SessionId = Guid.NewGuid(),
                    DateCreatedOnDevice =  dateCreatedOnDevice,
                    Date = date,
                    DateCreated = DateTime.Now,
                    Version = version,
                    PlatformId = platform
                };

            FeedbackSummary summary = new FeedbackSummary(feedback);
            feedbackMapper.Save(summary);

            Feedback feedback2 = new Feedback()
            {
                ApplicationId = applicationId,
                DeviceId = Guid.NewGuid(),
                Message = "feedback",
                Rating =  FeedbackRatingType.Three,
                ScreenName =  screenName,
                SessionId = Guid.NewGuid(),
                DateCreatedOnDevice =  dateCreatedOnDevice,
                Date = date,
                DateCreated = DateTime.Now,
                Version = version,
                PlatformId = platform
            };

            FeedbackSummary summary2 = new FeedbackSummary(feedback2);
            feedbackMapper.Save(summary2);

            IMongoQuery query = Query.And
                (
                    Query<FeedbackSummary>.EQ<DateTime>(mem => mem.Date, date),
                    Query<FeedbackSummary>.EQ<Guid>(mem => mem.ApplicationId, applicationId),
                    Query<FeedbackSummary>.EQ<string>(mem => mem.Version, version),
                    Query<FeedbackSummary>.EQ<PlatformType>(mem => mem.PlatformId, platform)
                );

            FeedbackSummary actual = this.GetCollection<FeedbackSummary>().FindOne(query);

            actual.ShouldHave().AllPropertiesBut(x => x.Id)
                .IncludingNestedObjects().EqualTo(expected);
        }