public void Log(AppUser user) { Application application = this.applicationRepository.Find(user.ApplicationId); if (application != null) { DeviceInfo device = this.deviceRepository.Find(user.DeviceId); if (device != null) { device.PlatformType = user.PlatformId; AppUserSummary appUserSum = new AppUserSummary(user); this.appUserRepository.Save(appUserSum); if (this.settings.DataLoggingRecordRaw) { this.appUserRepository.Save(user); } } else { throw new NoDeviceException(user.DeviceId); } } else { throw new InactiveApplicationException(user.ApplicationId); } }
public void Save(AppUserSummary entity) { try { IMongoQuery queryBase = Query.And ( Query <AppUserSummary> .EQ <DateTime>(mem => mem.Date, entity.Date), Query <AppUserSummary> .EQ <Guid>(mem => mem.ApplicationId, entity.ApplicationId), Query <AppUserSummary> .EQ <string>(mem => mem.Version, entity.Version), Query <AppUserSummary> .EQ <PlatformType>(mem => mem.PlatformId, entity.PlatformId) ); IMongoUpdate update = Update <AppUserSummary> .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.Users, new List <UserAggregate>()) .Inc(mem => mem.Count, entity.Count); this.GetCollection <AppUserSummary>().FindAndModify(queryBase, SortBy.Null, update, false, true); IMongoQuery queryCheckNotExist = Query.And ( queryBase, Query.NE("Users.Key", BsonValue.Create(entity.Users.First().Key)) ); IMongoUpdate insertUser = Update .Push("Users", entity.Users.First().CopyOnlyKeys().ToBsonDocument()); this.GetCollection <AppUserSummary>().Update(queryCheckNotExist, insertUser); IMongoQuery queryUser = Query.And ( queryBase, Query.EQ("Users.Key", BsonValue.Create(entity.Users.First().Key)) ); IMongoUpdate updateUser = Update .Inc("Users.$.AgeGroup._1_18", entity.Users.First().AgeGroup._1_18) .Inc("Users.$.AgeGroup._19_24", entity.Users.First().AgeGroup._19_24) .Inc("Users.$.AgeGroup._25_35", entity.Users.First().AgeGroup._25_35) .Inc("Users.$.AgeGroup._36_50", entity.Users.First().AgeGroup._36_50) .Inc("Users.$.AgeGroup._51_69", entity.Users.First().AgeGroup._51_69) .Inc("Users.$.AgeGroup._71", entity.Users.First().AgeGroup._71) .Inc("Users.$.Count", 1); this.GetCollection <AppUserSummary>().Update(queryUser, updateUser); } catch (Exception ex) { throw new DataAccessLayerException(ex); } }
public void Save_AppUser_ValuesIncrement() { AppUserMapper appuserMapper = new AppUserMapper(this.client, this.database); Guid applicationId = Guid.NewGuid(); AppUserSummary expected = new AppUserSummary() { ApplicationId = applicationId, Count = 2, Date = date, PlatformId = platform, Version = version, Users = new List <UserAggregate>() { new UserAggregate() { Key = SexType.Male, AgeGroup = new AgeGroup(null) { _25_35 = 2 }, Count = 2 } } }; AppUserSummary summary = new AppUserSummary() { ApplicationId = applicationId, Count = 1, Date = date, PlatformId = platform, Users = new List <UserAggregate>() { new UserAggregate(SexType.Male, 30) }, Version = version }; appuserMapper.Save(summary); appuserMapper.Save(summary); IMongoQuery query = Query.And ( Query <AppUserSummary> .EQ <DateTime>(mem => mem.Date, date), Query <AppUserSummary> .EQ <Guid>(mem => mem.ApplicationId, applicationId), Query <AppUserSummary> .EQ <string>(mem => mem.Version, version), Query <AppUserSummary> .EQ <PlatformType>(mem => mem.PlatformId, platform) ); AppUserSummary actual = this.GetCollection <AppUserSummary>().FindOne(query); actual.ShouldHave().AllPropertiesBut(x => x.Id) .IncludingNestedObjects().EqualTo(expected); }
public void Save(AppUserSummary summary) { this.appUserMapper.Save(summary); }