private void CreateProductOrderIngredient(orderline orderline, ingredient ingredient) { productorderingredient productorderingredient = new productorderingredient(); productorderingredient.amount = 1; productorderingredient.ingredient = ingredient; orderline.productorderingredients.Add(productorderingredient); }
private void IncrementOrCreateProductOrderIngredient(orderline orderline, ingredient ingredient) { bool ingredientfound = false; foreach (productorderingredient orderIngredient in orderline.productorderingredients) { if (orderIngredient.ingredient == ingredient) { orderIngredient.amount++; ingredientfound = true; break; } } if (ingredientfound == false) { CreateProductOrderIngredient(orderline, ingredient); } }
protected override int Import(string filePath) { //Winkelnaam;Klantnaam;TelefoonNr;Email;Adres;Woonplaats;Besteldatum;AfleverType;AfleverDatum;AfleverMoment;Product;PizzaBodem;PizzaSaus;Prijs;Bezorgkosten;Aantal;Extra Ingrediënten;Prijs Extra Ingrediënten;Regelprijs;Totaalprijs;Gebruikte Coupon;Coupon Korting;Te Betalen List <order> orders = new List <order>(); List <coupon> coupons = database.coupons.ToList(); using (StreamReader sr = new StreamReader(filePath)) { int row = 1; int count = 0; String line; order order = null; while ((line = sr.ReadLine()) != null) { if (row >= 6 && line != string.Empty) { string[] paths = line.Split(';'); if (paths[0] != string.Empty) { order = new order(); string storeName = paths[0]; string storeNameUppercased = storeName.ToUpper(); store store = stores.SingleOrDefault(i => i.name.ToUpper() == storeNameUppercased); if (store == null) { Logger.Instance.LogError(filePath, string.Format("Store {0} does not exists on line {1}!", storeName, row)); continue; } order.store = store; order.clientname = paths[1]; order.phonenumber = paths[2]; order.email = paths[3].ToLower(); order.address = new address() { countrycode = countrycode.code, street = Regex.Replace(paths[4], "[^A-Z a-z]", ""), number = Regex.Replace(paths[4], "[^0-9]", "") }; try { order.datecreated = GetDateTimeFromLongDateString(paths[6]); } catch { Logger.Instance.LogError(filePath, string.Format("Unable to parse DateTime from datestring {0} on line {1}", paths[6], row)); continue; } string deliveryCostField = paths[14].Trim(); if (!string.IsNullOrEmpty(deliveryCostField)) { order.deliverycost = Decimal.Parse(Regex.Replace(deliveryCostField, "[^0-9.]", "")) / 100; } string deliveryType = paths[7]; string deliveryTypeUppercased = deliveryType.ToUpper(); deliverytype deliverytype = deliveryTypes.SingleOrDefault(i => i.name.ToUpper() == deliveryTypeUppercased); if (deliverytype == null) { Logger.Instance.LogError(filePath, string.Format("Deliverytype {0} does not exists on line {1}!", deliveryType, row)); continue; } order.deliverytype = deliverytype; if (paths[9].Contains("soon")) { order.datedelivered = order.datecreated; } else { try { order.datedelivered = GetDateTimeFromLongDateString(paths[8], paths[9]); } catch { Logger.Instance.LogError(filePath, string.Format("Unable to parse DateTime from datestring {0} on line {1}", paths[6], row)); continue; } } //TODO: Proper order.preferredtime = order.datecreated; string orderPrice = paths[19]; if (!string.IsNullOrEmpty(orderPrice)) { order.price = Decimal.Parse(Regex.Replace(orderPrice, "[^0-9.]", "")) / 100; } string couponField = Encoding.UTF8.GetString(Encoding.Default.GetBytes(paths[20])); if (!string.IsNullOrEmpty(couponField)) { // ToDo change to SingleOrDefault once duplicates are removed from the db. coupon coupon = coupons.SingleOrDefault(i => i.description == couponField); if (coupon == null) { coupon = new coupon(); coupon.description = couponField; coupon.startdate = DateTime.Now; coupon.enddate = DateTime.Now; coupon.code = "0000"; coupons.Add(coupon); } order.coupon = coupon; } orders.Add(order); count++; Console.WriteLine("[INFO] Added order to orders. Current amount: {0}", count); } orderline orderline = new orderline(); //search if product exists string mappedProductName = this.GetMappedValue(paths[10], false); if (mappedProductName == string.Empty) { continue; } // ToDo change to SingleOrDefault once duplicates are removed from the db. product product = products.SingleOrDefault(i => i.name == mappedProductName); if (product == null) { Logger.Instance.LogError(filePath, string.Format("Product {0} does not exists on line {1}!", mappedProductName, row)); continue; } orderline.product = product; string bottomName = paths[11]; if (string.IsNullOrEmpty(bottomName) == false) { string mappedBottomName = this.GetMappedValue(paths[11], false); // ToDo change to SingleOrDefault once duplicates are removed from the db. bottom bottom = bottoms.SingleOrDefault(i => i.name == mappedBottomName); if (bottom == null) { Logger.Instance.LogError(filePath, string.Format("Bottom {0} does not exists on line {1}!", mappedBottomName, row)); continue; } orderline.bottom = bottom; } string sauceName = paths[12]; if (string.IsNullOrEmpty(sauceName) == false) { string mappedSauceName = this.GetMappedValue(paths[12], false); // ToDo change to SingleOrDefault once duplicates are removed from the db. sauce sauce = sauces.SingleOrDefault(i => i.name == mappedSauceName); if (sauce == null) { Logger.Instance.LogError(filePath, string.Format("Sauce {0} does not exists on line {1}!", mappedSauceName, row)); continue; } // If the sauce is not the same as the default sauce, add it to productordersauce if (sauce != product.sauce) { productordersauce productordersauce = new productordersauce(); productordersauce.orderline = orderline; productordersauce.sauce = sauce; productordersauce.amount = 1; orderline.productordersauces.Add(productordersauce); } } orderline.price = Decimal.Parse(Regex.Replace(paths[13], "[^0-9.]", "")) / 100; orderline.amount = Convert.ToInt32(paths[15]); string orderlinePrice = paths[18]; if (!string.IsNullOrEmpty(orderlinePrice)) { orderline.price = Decimal.Parse(Regex.Replace(orderlinePrice, "[^0-9.]", "")) / 100; } string[] extraIngredients = paths[16].Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries); if (extraIngredients.Length > 0) { foreach (var ei in extraIngredients) { string mappedIngredientName = this.GetMappedValue(ei.Trim(), true); ingredient ingredient = ingredients.SingleOrDefault(i => i.name == mappedIngredientName); //TODO: look for mapping, else create new/exception if (ingredient == null) { Logger.Instance.LogError(filePath, string.Format("Ingredient {0} does not exists on line {1}!", mappedIngredientName, row)); continue; } if (orderline.productorderingredients.Count > 0) { IncrementOrCreateProductOrderIngredient(orderline, ingredient); } else { CreateProductOrderIngredient(orderline, ingredient); } } } order.orderlines.Add(orderline); } row++; } } database.orders.AddRange(orders); Console.WriteLine("[INFO] Saving " + orders.Count + " records to database..."); DateTime before = DateTime.Now; database.SaveChanges(); DateTime after = DateTime.Now; TimeSpan ts = after.Subtract(before); Console.WriteLine(string.Format("Completed after {0}:{1}:{2}:{3}", ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds)); //database.BulkInsert<order>(orders, 5000); return(orders.Count); }
protected override int Import(string filePath) { //Winkelnaam;Klantnaam;TelefoonNr;Email;Adres;Woonplaats;Besteldatum;AfleverType;AfleverDatum;AfleverMoment;Product;PizzaBodem;PizzaSaus;Prijs;Bezorgkosten;Aantal;Extra Ingrediënten;Prijs Extra Ingrediënten;Regelprijs;Totaalprijs;Gebruikte Coupon;Coupon Korting;Te Betalen List <order> orders = new List <order>(); using (StreamReader sr = new StreamReader(filePath)) { int row = 1; String line; order order = null; while ((line = sr.ReadLine()) != null) { if (row > 6 && line != string.Empty) { string[] paths = line.Split(';'); if (paths[0] != string.Empty) { order = new order(); store store = database.stores.SingleOrDefault(i => i.name.ToUpper() == paths[0].ToUpper()); order.store = store ?? throw new Exception(string.Format("Store {0} does not exists!", paths[0])); order.clientname = paths[1]; order.phonenumber = paths[2]; //add email field to order table, path[3] //add address entity to order, get postalcode from database by using a lookup query, paths[4],paths[5] order.datecreated = GetDateTimeFromLongDateString(paths[6]); deliverytype deliverytype = database.deliverytypes.SingleOrDefault(i => i.name.ToUpper() == paths[7].ToUpper()); order.deliverytype = deliverytype ?? throw new Exception(string.Format("Deliverytype {0} does not exists!", paths[7])); order.datedelivered = GetDateTimeFromLongDateString(paths[8], paths[9]); //add deliverycosts to order table } orderline orderline = new orderline(); orderline.order = order; //search if product exists product product = database.products.SingleOrDefault(i => i.name.ToUpper() == paths[10].ToUpper()); orderline.product = product ?? throw new Exception(string.Format("Product {0} does not exists!", paths[10])); //after we delete exception, create product and also create/select product type orderline.amount = Convert.ToInt32(paths[15]); string[] extraIngredients = paths[16].Split(','); if (extraIngredients.Length > 0) { foreach (var ei in extraIngredients) { ingredient ingredient = database.ingredients.SingleOrDefault(i => i.name.ToUpper() == ei.Trim().ToUpper()); //TODO: look for mapping, else create new/exception productorderingredient productorderingredient = new productorderingredient(); productorderingredient.ingredient = ingredient ?? throw new Exception(string.Format("Product {0} does not exists!", ei)); productorderingredient.orderline = orderline; } } } row++; } } //database.BulkInsert<order>(orders, ); return(orders.Count); }