private IList <CategorySolytronViewModel> GetSubCategories(string name) { XDocument doc = XDocument.Load("https://solytron.bg/products/xml/catalog-category.xml?j_u=cavescomputers&j_p=Magurata2000"); IEnumerable <XElement> categories = doc.Descendants("productCategory") .Where(x => (string)x.Attribute("name") == name) .Elements("productGroup"); IList <CategorySolytronViewModel> subCategories = new List <CategorySolytronViewModel>(); foreach (var cat in categories) { var categoryName = cat.Attribute("name").Value; var link = (string)cat.Element(XName.Get("link", "https://www.w3.org/2005/Atom")).Attribute("href"); CategorySolytronViewModel newCategory = new CategorySolytronViewModel { Name = categoryName, CategoryLink = link }; subCategories.Add(newCategory); } return(subCategories); }
private void GetProductsFromSubCategory(string stantekCategory, CategorySolytronViewModel subCategory) { Category category = this.categoriesService.GetByName(stantekCategory); ICollection <ProductSolytronViewModel> products = GetProductsFromSubCategories(subCategory, category); //Add products to DB AddProductsToDb(products); }
private ICollection <ProductSolytronViewModel> GetProductsFromSubCategories(CategorySolytronViewModel subCategory, Category category) { XDocument doc = XDocument.Load(subCategory.CategoryLink); ICollection <ProductSolytronViewModel> products = new List <ProductSolytronViewModel>(); IEnumerable <XElement> productElement = doc.Descendants("productSet") .Elements("product"); foreach (var item in productElement) { //Check if product is availible string availability = string.Empty; if (item.Descendants("stockInfoValue").Any()) { availability = item.Element("stockInfoValue").Value; } if (availability == "OnOrder" || availability == string.Empty) { continue; } //Check for End User Price if (!item.Descendants("priceEndUser").Any()) { continue; } //Get Name string name = item.Element("name").Value; //var isExist = this.productsService.GetByName(name.Substring(20)) // .Where(x => x.Supplier == Supplier.Stantek && x.IsPublished == true) // .FirstOrDefault(); //if (isExist != null) //{ // continue; //} string codeId = item.Attribute("codeId").Value; string groupId = item.Attribute("groupId").Value; string fullInfoUrl = "https://solytron.bg/products/xml/product.xml?codeId=" + codeId + "&groupId=" + groupId + "&j_u=cavescomputers&j_p=Magurata2000"; ProductSolytronViewModel newProduct = GetFullInfo(fullInfoUrl); string vendor = item.Element("vendor").Value; string nameAndVendor = string.Empty; //Check name whether start with vendor if (vendor.Length > name.Length || vendor.ToLower() != name.Substring(0, vendor.Length).ToLower()) { nameAndVendor = vendor + " " + name; } else { nameAndVendor = name; } if (nameAndVendor.Length > ValidationConstants.StandartMaxLength) { nameAndVendor = nameAndVendor.Substring(0, ValidationConstants.StandartMaxLength); } newProduct.Name = nameAndVendor; //Get Price string currency = item.Element("priceEndUser").Attribute("currency").Value; decimal priceValue = decimal.Parse(item.Element("priceEndUser").Value); if (currency == "BGN") { newProduct.Price = priceValue; } else if (currency == "EUR") { decimal price = priceValue * Constants.EURValue; newProduct.Price = Math.Round(price, 2); } else { decimal price = priceValue * Constants.USDValue; newProduct.Price = Math.Round(price, 2); } //Get warranty //if (item.Descendants("warrantyUnit").Any()) //{ // string warrantyType = item.Element("warrantyUnit").Value; // string warrantyValue = item.Element("warrantyQty").Value; // if (warrantyType == "2") // { // string warranty = "<p><b>Гаранция: </b>" + warrantyValue + " месеца</p>"; // newProduct.FullDescription = newProduct.FullDescription + warranty; // } //} newProduct.Category = category; //Add Id??? products.Add(newProduct); } return(products); }