public void SaveNewArticle(Product Record, int countryId) { country cou = _context.country.Where(c => c.id == countryId).FirstOrDefault(); webshop webshop = _context.webshop.Where(w => w.url == Record.Webshop).FirstOrDefault(); if (webshop == default(webshop)) { return; } if (cou == default(country)) { Console.WriteLine("Could not find country id {0}, aborting the save.", countryId); return; } article art = new article { description = Record.Description, brand = Record.Brand, image_loc = Record.Image_Loc }; // Do not modify this as this is neccessary to get the last id. _context.article.Add(art); ean ean = new ean { ean1 = Record.EAN, article_id = art.id }; _context.ean.Add(ean); title title = new title { title1 = Record.Title, country_id = (short)countryId, article_id = art.id, country = cou }; _context.title.Add(title); title_synonym ts = new title_synonym { title = Record.Title, title_id = title.id, occurrences = 1 }; _context.title_synonym.Add(ts); if (Record.SKU != "") { sku sku = new sku { sku1 = Record.SKU, article_id = art.id }; _context.sku.Add(sku); } decimal castedShipCost; decimal castedPrice; if (!(decimal.TryParse(Record.DeliveryCost, NumberStyles.Any, CultureInfo.InvariantCulture, out castedShipCost))) Console.WriteLine("Cannot cast shipping cost " + Record.DeliveryCost + " to decimal."); if (!(decimal.TryParse(Record.Price, NumberStyles.Any, CultureInfo.InvariantCulture, out castedPrice))) Console.WriteLine("Cannot cast price " + Record.Price + " to decimal."); product product = new product { article_id = art.id, ship_cost = castedShipCost, ship_time = Record.DeliveryTime, price = castedPrice, webshop_url = webshop.url, direct_link = Record.Url, affiliate_name = Record.Affiliate, affiliate_unique_id = Record.AffiliateProdID }; _context.product.Add(product); _context.SaveChanges(); }
public void SaveMatch(Product Record, int matchedArticleID, int countryID) { // First get all data needed for matching. Ean, sku and title_synonym are seperate because they can store multiple values. article articleTable = _context.article.Where(a => a.id == matchedArticleID).Include(a => a.ean) .Include(a => a.sku) .Include(t => t.title.Select(ts => ts.title_synonym)) .FirstOrDefault(); // Loop through ean and sku collections to check if the ean or sku already exists. If not, add it if (!(articleTable.ean.Any(e => e.ean1 == Record.EAN)) && Record.EAN != "") _context.ean.Add(new ean { ean1 = Record.EAN, article_id = matchedArticleID }); if (!(articleTable.sku.Any(s => s.sku1 == Record.SKU)) && Record.SKU != "") _context.sku.Add(new sku { sku1 = Record.SKU, article_id = matchedArticleID }); title title = articleTable.title.Where(t => t.article_id == matchedArticleID && t.country_id == countryID).FirstOrDefault(); if (title == default(title)) { title addedTitle = new title { title1 = Record.Title, country_id = (short)countryID, article_id = matchedArticleID }; _context.title.Add(addedTitle); _context.title_synonym.Add(new title_synonym { occurrences = 1, title = Record.Title, title_id = addedTitle.id }); } else { // If any title synonym matches the title, up the occurences. if (articleTable.title.Any(t => t.title_synonym.Any(ts => ts.title.ToLower().Trim() == Record.Title.ToLower().Trim()))) { title_synonym ts = _context.title_synonym.Where(innerTs => innerTs.title.ToLower().Trim() == Record.Title.ToLower().Trim()).FirstOrDefault(); ts.occurrences++; // _context.Entry(ts).State = EntityState.Modified; if (ts.occurrences > articleTable.title.Max(t => t.title_synonym.Max(ts2 => ts2.occurrences))) { UpdateTitle(title.id, ts.title); } } // else, add the title to the synonyms. else { title_synonym ts = new title_synonym { occurrences = 1, title = Record.Title, title_id = title.id }; _context.title_synonym.Add(ts); } } _context.SaveChanges(); }