public ActionResult AddProperty([Bind(Include = "Name")] Property property) { if (ModelState.IsValid) { _propertyControl.CreateProperty(property.Name); return(RedirectToAction("Properties")); } return(View(property)); }
// items Location represents file location in the projects folder, images location is the same, the httpbased file is the attached excel file public async Task StartImportingItems(string itemsLocation, HttpPostedFileBase file, string imagesLocation) { await Task.Run(() => { var itemsFileObject = _FileControl.LoadShopItemsFile(itemsLocation, file); var allItems = _ImportControl.ImportItemsFromFile(Path.Combine(itemsLocation, itemsFileObject)); var allCategories = _ItemCategoryControl.GetAllCategories(); for (int i = 0; i < allItems.Count; i++) { if (allItems[i].Name == null) { continue; } if (allItems[i].Headline == null) { continue; } if (allItems[i].SKUCode == null) { continue; } if (allItems[i].Price <= 0) { continue; } var itemPropertiesSet = new HashSet <ItemProperty>(); var properties = allItems[i].ItemProperties.ToList(); Category category = null; if (!(string.IsNullOrWhiteSpace(allItems[i].Category.Name) || string.IsNullOrEmpty(allItems[i].Category.Name)) && (category = allCategories.FirstOrDefault(c => c.Name == allItems[i].Category.Name)) == null) { var itemProperties = new List <int>(); foreach (var property in properties) { Property propertiesInDatabase; if (!(string.IsNullOrEmpty(property.Property.Name) || string.IsNullOrWhiteSpace(property.Property.Name))) { if ((propertiesInDatabase = _IPropertyControl.GetProperty(property.Property.Name)) == null) { _IPropertyControl.CreateProperty(property.Property.Name); propertiesInDatabase = _IPropertyControl.GetProperty(property.Property.Name); } itemProperties.Add(propertiesInDatabase.Id); } } _ItemCategoryControl.CreateCategory(allItems[i].Category.Name, itemProperties); allCategories = _ItemCategoryControl.GetAllCategories(); category = allCategories.FirstOrDefault(c => c.Name == allItems[i].Category.Name); allItems[i].Category = null; } allItems[i].Category = null; allItems[i].CategoryId = category.Id; var categoryProperties = category.Properties; foreach (var categoryProperty in categoryProperties) { var addedProperty = properties.FirstOrDefault(x => x.Property.Name == categoryProperty.Name); if (addedProperty == null) { addedProperty = new ItemProperty(); } itemPropertiesSet.Add(new ItemProperty() { PropertyId = categoryProperty.Id, Value = addedProperty.Value }); } allItems[i].ItemProperties = itemPropertiesSet; try { using (var dbContextScope = _dbContextScopeFactory.Create()) { Item foundItemObject = _ItemRepository.GetItem(allItems[i].Id); if (foundItemObject != null) { throw new Exception("This item already exisst"); } try { if (String.IsNullOrEmpty(allItems[i].ImageUrl) && allItems[i].Image != null) { string[] itemsFileRepository = imagesLocation.Split(Path.DirectorySeparatorChar); string repository = Path.DirectorySeparatorChar + Path.Combine(itemsFileRepository[itemsFileRepository.Length - 2], Path.Combine(itemsFileRepository[itemsFileRepository.Length - 1], _FileControl.LoadShopItemsFile(imagesLocation, allItems[i].Image))); if (!CheckPictureExtension(repository)) { throw new Exception("Picture extension is not supported by the system"); } allItems[i].ImageUrl = repository; } else if ((!String.IsNullOrEmpty(allItems[i].ImageUrl) && allItems[i].Image == null) || (!String.IsNullOrEmpty(allItems[i].ImageUrl) && allItems[i].Image != null)) { if (!CheckPictureExtension(allItems[i].ImageUrl)) { throw new Exception("Picture extension is not supported by the system"); } } else { allItems[i].ImageUrl = null; } } catch { throw; } _ItemRepository.CreateItem(allItems[i]); dbContextScope.SaveChanges(); } } catch (Exception e) { throw new Exception("The following line describes whats wrong " + e.Message); } } }); }