public async Task <IActionResult> Add(AddItemViewModel model) { ShopItem newItem = new ShopItem(model.Name, model.Price, model.Description); if (!string.IsNullOrWhiteSpace(model.Categories)) { string[] categoryNames = model.Categories.Split(','); foreach (var categoryName in categoryNames) { string trimmedCategoryName = categoryName.Trim(); trimmedCategoryName = trimmedCategoryName.ToLower(); //if it isnt empty or null if (!string.IsNullOrWhiteSpace(trimmedCategoryName)) { ShopItemCategory existingCategory = _repository.GetCategoryByName(trimmedCategoryName); if (existingCategory == null) { ShopItemCategory newCategory = new ShopItemCategory(trimmedCategoryName); _repository.AddCategory(newCategory); newItem.Categories.Add(newCategory); } else { newItem.Categories.Add(existingCategory); } } } } _repository.AddItem(newItem); return(RedirectToAction("Index")); }
public async Task <IActionResult> ItemsByCategory(Guid categoryId) { List <ItemViewModel> itemViewModels = new List <ItemViewModel>(); ShopItemCategory category = _repository.GetCategoryById(categoryId); List <ShopItem> items = _repository.GetFilteredByCategory(category); foreach (var item in items) { ItemViewModel itemViewModel = new ItemViewModel(item.Id, item.Name, item.Price); itemViewModels.Add(itemViewModel); } ShopIndexViewModel filteredIndexViewModel = new ShopIndexViewModel(itemViewModels); return(View(filteredIndexViewModel)); }
public async Task <IActionResult> AddCategory(AddCategoryViewModel model) { string newCategoryName = model.Name.Trim().ToLower(); ShopItemCategory existingCategory = _repository.GetCategoryByName(newCategoryName); if (existingCategory == null) { ShopItemCategory newCategory = new ShopItemCategory(newCategoryName); _repository.AddCategory(newCategory); } else { return(RedirectToAction("AddCategory")); } return(RedirectToAction("CategoryIndex")); }
public async Task <IActionResult> EditCategory(ShopItemCategory category) { category.Name = category.Name.Trim().ToLower(); _repository.UpdateCategory(category); return(RedirectToAction("CategoryIndex")); }
public async Task <IActionResult> EditCategory(Guid categoryId) { ShopItemCategory category = _repository.GetCategoryById(categoryId); return(View(category)); }