public void Save(DeviceSummary entity) { try { IMongoQuery queryBase = Query.And ( Query<DeviceSummary>.EQ<DateTime>(mem => mem.Date, entity.Date), Query<DeviceSummary>.EQ<Guid>(mem => mem.ApplicationId, entity.ApplicationId), Query<DeviceSummary>.EQ<string>(mem => mem.Version, entity.Version), Query<DeviceSummary>.EQ<PlatformType>(mem => mem.PlatformId, entity.PlatformId) ); IMongoUpdate update = Update<DeviceSummary> .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.Locales, new List<Aggregate<string>>()) .SetOnInsert(x => x.Carriers, new List<Aggregate<string>>()) .SetOnInsert(x => x.OperatingSystems, new List<Aggregate<string>>()) .SetOnInsert(x => x.ManufacturerModels, new List<ManufacturerModelAggregate>()) .SetOnInsert(x => x.Resolutions, new List<Resolution>()) .Inc(x => x.Count, 1); this.GetCollection<DeviceSummary>().FindAndModify(queryBase, SortBy.Descending("Date"), update, false, true); this.GetCollection<DeviceSummary>().EnsureIndex(IndexKeys.Descending("Date")); //locales IMongoQuery queryLocaleCheckNotExist = Query.And ( queryBase, Query.NE("Locales.Key", BsonValue.Create(entity.Locales.First().Key)) ); IMongoUpdate insertLocale = Update .Push("Locales", BsonValue.Create(entity.Locales.First().CopyOnlyKeys().ToBsonDocument())); this.GetCollection<DeviceSummary>().Update(queryLocaleCheckNotExist, insertLocale); IMongoQuery queryGetExistingLocale = Query.And ( queryBase, Query.EQ("Locales.Key", BsonValue.Create(entity.Locales.First().Key)) ); IMongoUpdate updateLocales = Update .Inc("Locales.$.Count", 1); this.GetCollection<DeviceSummary>().Update(queryGetExistingLocale, updateLocales); //carriers IMongoQuery queryCarrierNotExist = Query.And ( queryBase, Query.NE("Carriers.Key", BsonValue.Create(entity.Carriers.First().Key)) ); IMongoUpdate insertCarrier = Update .Push("Carriers", BsonValue.Create(entity.Carriers.First().CopyOnlyKeys().ToBsonDocument())); this.GetCollection<DeviceSummary>().Update(queryCarrierNotExist, insertCarrier); IMongoQuery queryGetExistingCarrier = Query.And ( queryBase, Query.EQ("Carriers.Key", BsonValue.Create(entity.Carriers.First().Key)) ); IMongoUpdate updateCarrier = Update .Inc("Carriers.$.Count", 1); this.GetCollection<DeviceSummary>().Update(queryGetExistingCarrier, updateCarrier); //manufacturerModels IMongoQuery queryManufacturerModelExists = Query.And ( queryBase, Query.NE("ManufacturerModels.ManufacturerModel", BsonValue.Create(entity.ManufacturerModels.First().ManufacturerModel)) ); IMongoUpdate insertManufacturerModel = Update .Push("ManufacturerModels", BsonValue.Create(entity.ManufacturerModels.First().CopyOnlyKeys().ToBsonDocument())); this.GetCollection<DeviceSummary>().Update(queryManufacturerModelExists, insertManufacturerModel); IMongoQuery queryGetExistingManufacturer = Query.And ( queryBase, Query.EQ("ManufacturerModels.ManufacturerModel", BsonValue.Create(entity.ManufacturerModels.First().ManufacturerModel)) ); IMongoUpdate updateManufacturer = Update .Inc("ManufacturerModels.$.Count", 1); this.GetCollection<DeviceSummary>().Update(queryGetExistingManufacturer, updateManufacturer); //operating systems IMongoQuery queryOperatingSystemExists = Query.And ( queryBase, Query.NE("OperatingSystems.Key", BsonValue.Create(entity.OperatingSystems.First().Key)) ); IMongoUpdate insertOperatingSystem = Update .Push("OperatingSystems", BsonValue.Create(entity.OperatingSystems.First().CopyOnlyKeys().ToBsonDocument())); this.GetCollection<DeviceSummary>().Update(queryOperatingSystemExists, insertOperatingSystem); IMongoQuery queryGetExistingOperatingSystems = Query.And ( queryBase, Query.EQ("OperatingSystems.Key", BsonValue.Create(entity.OperatingSystems.First().Key)) ); IMongoUpdate updateOperatingSystems = Update .Inc("OperatingSystems.$.Count", 1); this.GetCollection<DeviceSummary>().Update(queryGetExistingOperatingSystems, updateOperatingSystems); //resolutions IMongoQuery queryResolutonsExists = Query.And ( queryBase, Query.NE("Resolutions.WxH", BsonValue.Create(entity.Resolutions.First().WidthxHeight)) ); IMongoUpdate insertResolution = Update .Push("Resolutions", BsonValue.Create(entity.Resolutions.First().CopyOnlyKeys().ToBsonDocument())); this.GetCollection<DeviceSummary>().Update(queryResolutonsExists, insertResolution); IMongoQuery queryGetExistingResolution = Query.And ( queryBase, Query.EQ("Resolutions.WxH", BsonValue.Create(entity.Resolutions.First().WidthxHeight)) ); IMongoUpdate updateResolution = Update .Inc("Resolutions.$.Count", 1); this.GetCollection<DeviceSummary>().Update(queryGetExistingResolution, updateResolution); } catch (Exception ex) { throw new DataAccessLayerException(ex); } }
public void Log(DeviceInfo device, ApplicationInfo applicationInfo) { Application application = this.applicationRepository.Find(applicationInfo.ApplicationId); if (application != null) { device.Guid = Guid.NewGuid(); this.deviceRepository.Save(device); DeviceSummary deviceSum = new DeviceSummary(device, applicationInfo); this.deviceRepository.Save(deviceSum); } else { throw new InactiveApplicationException(applicationInfo.ApplicationId); } }
public void Save(DeviceSummary summary) { this.deviceMapper.Save(summary); }
public void Save_DeviceSummary_ValuesIncrement() { DeviceMapper deviceMapper = new DeviceMapper(this.client, this.database); Guid applicationId = Guid.NewGuid(); Guid deviceId = Guid.NewGuid(); DeviceSummary expected = new DeviceSummary() { ApplicationId = applicationId, Count = 2, Date = date, PlatformId = platform, Version = version, Carriers = new List<Aggregate<string>>() { new Aggregate<string>() { Key = "o2", Count = 2 } }, Locales = new List<Aggregate<string>>() { new Aggregate<string>() { Key = "EN", Count = 2 } }, ManufacturerModels = new List<ManufacturerModelAggregate>() { new ManufacturerModelAggregate("HTC", "OneX") { Count = 2 } }, OperatingSystems = new List<Aggregate<string>>() { new Aggregate<string>() { Key = "2.2.2.2", Count = 2 } }, Resolutions = new List<Resolution>() { new Resolution(900, 300) { Count = 2 } } }; DeviceSummary summary = new DeviceSummary() { ApplicationId = applicationId, Date = date, Version = version, PlatformId = platform, Locales = new List<Aggregate<string>>() { new Aggregate<string>("EN") }, Carriers = new List<Aggregate<string>>() { new Aggregate<string>("o2") }, ManufacturerModels = new List<ManufacturerModelAggregate>() { new ManufacturerModelAggregate("HTC", "OneX") }, OperatingSystems = new List<Aggregate<string>>() { new Aggregate<string>("2.2.2.2") }, Resolutions = new List<Resolution>() { new Resolution(900, 300) } }; deviceMapper.Save(summary); deviceMapper.Save(summary); IMongoQuery query = Query.And ( Query<DeviceSummary>.EQ<DateTime>(mem => mem.Date, date), Query<DeviceSummary>.EQ<Guid>(mem => mem.ApplicationId, applicationId), Query<DeviceSummary>.EQ<string>(mem => mem.Version, version), Query<DeviceSummary>.EQ<PlatformType>(mem => mem.PlatformId, platform) ); DeviceSummary actual = this.GetCollection<DeviceSummary>().FindOne(query); actual.ShouldHave().AllPropertiesBut(x => x.Id) .IncludingNestedObjects().EqualTo(expected); }