private static List <City> ImrportCities(string[] shops) { var citiesList = shops.Select(shop => shop.Split(';')[5]).ToList(); var groupedList = citiesList.GroupBy(x => x).Select(x => x.Key).ToList(); groupedList = groupedList.Where(g => !string.IsNullOrWhiteSpace(g)).Select(s => s).ToList(); var domainList = new List <City>(); using (RaterPriceContext context = RaterPriceContext.Create()) { foreach (var gr in groupedList) { var domainItem = new City() { Name = gr.Trim() }; if (!context.Cities.Any(g => g.Name.Equals(domainItem.Name))) { domainItem.Id = context.Cities.Add(domainItem).Id; context.SaveChanges(); Console.WriteLine("City has been imported: " + domainItem.Name); } else { domainItem.Id = context.Cities.Where(p => p.Name.Equals(domainItem.Name)).Select(p => p.Id).First(); } domainList.Add(domainItem); } } return(domainList); }
private static List <Weekday> ImportWeekDays() { var days = Enum.GetValues(typeof(DaysOfWeek)).Cast <DaysOfWeek>().ToList(); var domainList = days.Select(d => new Weekday() { Id = (int)d, Name = d.ToString() }).ToList(); domainList = domainList.OrderBy(d => d.Id).ToList(); foreach (var d in domainList) { using (RaterPriceContext context = RaterPriceContext.Create()) { if (context.Weekdays.Any(da => da.Name == d.Name)) { continue; } d.Id = context.Weekdays.Add(d).Id; context.SaveChanges(); Console.WriteLine("Working day has been imported: " + d.Name); } } return(domainList); }
static void Main(string[] args) { var connectionString = ConfigurationManager.ConnectionStrings["raterPriceConnectionString"].ConnectionString; using (RaterPriceContext context = RaterPriceContext.Create()) { var goodsIdsWithPrices = context.Prices.Select(p => p.GoodId); var goodsWithoutPrice = context.Goods.Where(g => !goodsIdsWithPrices.Contains(g.Id)).Select(g => g).AsNoTracking().ToList(); if (!goodsWithoutPrice.Any()) { goodsWithoutPrice = context.Goods.Select(g => g).AsNoTracking().ToList(); } foreach (var good in goodsWithoutPrice) { var shopsIdsWithPrices = context.Prices.Select(p => p.ShopId); var shops = context.Shops.Where(s => !shopsIdsWithPrices.Contains(s.Id)).Select(s => s); if (shops.Count() < 2) { shops = context.Shops.Select(s => s); } var r1 = new Random(); var take = r1.Next(1, shops.Count() - 1); var skip = r1.Next(0, take - 1); var workingRange = shops.OrderBy(s => s.Id).Skip(() => skip).Take(() => take).AsNoTracking().ToList(); foreach (var shop in workingRange) { if (context.Prices.Any(p => p.ShopId == shop.Id && p.GoodId == good.Id)) { continue; } var r = new Random(); var price = new Price() { GoodId = good.Id, ShopId = shop.Id, DateUpdated = DateTime.UtcNow, PriceValue = Convert.ToDecimal(r.Next(1, 1000) + r.NextDouble()), UserId = r.Next() }; context.Prices.Add(price); context.SaveChanges(); Console.WriteLine(price.PriceValue + "->" + shop.Name + "->" + good.Name); } } } }
public void GetShopsTest() { var connectionString = GetConnectionString(); const int cityId = 8; const string querySearch = "О"; var url = GetBaseUrl() + $"api/shops/search?cityId={cityId}&querySearch={querySearch}"; var r = Get(url); var list = new JavaScriptSerializer().Deserialize <List <Shop> >(r); using (RaterPriceContext context = RaterPriceContext.Create()) { var shops = context.Shops.Where(s => s.CityId == cityId && s.Name.IndexOf(querySearch) > -1).Select(s => s); Assert.AreEqual(list.Count(), shops.Count()); foreach (var s in shops) { var shopId = s.Id; var shopCityId = s.CityId; var shopName = s.Name; Assert.AreEqual(list.Any(x => x.Name == shopName && x.Id == shopId && x.CityId == shopCityId), true); } } }
private static List <ShopGroup> ImrportGroups(string[] shops) { var groupsList = new List <string>(); foreach (var shop in shops) { var groupsForOneItem1 = shop.Split(';')[2]; var groupsForOneItem2 = shop.Split(';')[3]; groupsList.Add(groupsForOneItem1); groupsList.Add(groupsForOneItem2); } var groupedList = groupsList.GroupBy(x => x).Select(x => x.Key).ToList(); groupedList = groupedList.Where(g => !string.IsNullOrWhiteSpace(g)).Select(s => s).ToList(); var domainList = new List <ShopGroup>(); using (RaterPriceContext context = RaterPriceContext.Create()) { foreach (var gr in groupedList) { var domainItem = new ShopGroup() { Name = gr.Trim() }; if (!context.ShopGroups.Any(g => g.Name.Equals(domainItem.Name))) { domainItem.Id = context.ShopGroups.Add(domainItem).Id; context.SaveChanges(); Console.WriteLine("Group has been imported: " + domainItem.Name); } else { domainItem.Id = context.ShopGroups.Where(p => p.Name.Equals(domainItem.Name)).Select(p => p.Id).First(); } domainList.Add(domainItem); } } return(domainList); }
private static List <PaymentType> ImrportPaymentTypes(string[] shops) { var paymentTypeList = new List <string>(); foreach (var shop in shops) { var paymentTypesForOneItem = shop.Split(';')[30].Split(','); paymentTypeList.AddRange(paymentTypesForOneItem); } var groupedList = paymentTypeList.GroupBy(x => x).Select(x => x.Key).ToList(); groupedList = groupedList.Where(g => !string.IsNullOrWhiteSpace(g)).Select(s => s).ToList(); var domainList = new List <PaymentType>(); using (RaterPriceContext context = RaterPriceContext.Create()) { foreach (var pt in groupedList) { var domainItem = new PaymentType() { Name = pt.Trim() }; if (!context.PaymentTypes.Any(p => p.Name.Equals(domainItem.Name))) { domainItem.Id = context.PaymentTypes.Add(domainItem).Id; context.SaveChanges(); Console.WriteLine("Payment type has been imported: " + domainItem.Name); } else { domainItem.Id = context.PaymentTypes.Where(p => p.Name.Equals(domainItem.Name)).Select(p => p.Id).First(); } domainList.Add(domainItem); } } return(domainList); }
private static List <Shop> ImportShops(string[] shopsInStrings, List <Weekday> weekdays, List <PaymentType> paymentTypes, List <ShopGroup> groups, List <City> cities) { var domainShops = new List <Shop>(); foreach (var shopStr in shopsInStrings) { var domainShop = MapOneShopFromString(shopStr, groups, cities); using (RaterPriceContext context = RaterPriceContext.Create()) { domainShop.Id = context.Shops.Add(domainShop).Id; context.SaveChanges(); var paymentTypesForShop = GetPaymentTypesForShop(shopStr, paymentTypes, domainShop.Id); context.ShopPaymentTypes.AddRange(paymentTypesForShop); context.SaveChanges(); var shopWorkingDays = GetShopWeekdays(shopStr, weekdays, domainShop.Id); context.ShopWeekdays.AddRange(shopWorkingDays); context.SaveChanges(); } domainShops.Add(domainShop); Console.WriteLine("Shop has been imported: " + domainShop.Name); } return(domainShops); }