public IActionResult AddStoreToProduct() { string uNameId = ""; string uName = ""; uNameId = (_unitOfWork.ApplicationUser.GetAll().Where(q => q.UserName == User.Identity.Name).Select(q => q.Id)).FirstOrDefault(); uName = (_unitOfWork.ApplicationUser.GetAll().Where(q => q.UserName == User.Identity.Name).Select(q => q.UserName)).FirstOrDefault(); SellersInventoryVM sellersInventoryVM = new SellersInventoryVM() { SellersInventory = new SellersInventory(), ProductList = _unitOfWork.Product.GetAll().Where(a => a.AvailableForSellers). OrderBy(a => a.ProductName). Select(i => new SelectListItem { Text = i.ProductName, Value = i.Id.ToString() }), StoresList = _unitOfWork.UserStoreName.GetAll().Where(q => q.UserNameId == uNameId).Select(i => new SelectListItem { Text = i.StoreName, Value = i.Id.ToString() }) }; ViewBag.uNameID = uNameId; ViewBag.uName = uName; ViewBag.getStoreName = new Func <int, string>(returnStoreName); return(View(sellersInventoryVM)); }
public IActionResult AddStoreToProduct(SellersInventoryVM sellersInventoryVM) { if (ModelState.IsValid) { if (sellersInventoryVM.updateAllStores) { string uNameId = (_unitOfWork.ApplicationUser.GetAll().Where(q => q.UserName == User.Identity.Name).Select(q => q.Id)).FirstOrDefault(); //get all stores of user var StoresIdList = _unitOfWork.UserStoreName.GetAll().Where(q => q.UserNameId == uNameId).Select(i => i.Id); foreach (var storeId in StoresIdList) { if (_unitOfWork.SellersInventory.GetAll().Where(q => q.ProductId == sellersInventoryVM.SellersInventory.ProductId). Where(a => a.StoreNameId == storeId).Count() == 0) { sellersInventoryVM.SellersInventory.Id = 0; sellersInventoryVM.SellersInventory.StoreNameId = storeId; _unitOfWork.SellersInventory.Add(sellersInventoryVM.SellersInventory); _unitOfWork.Save(); } } } else { bool existStore = _unitOfWork.SellersInventory.GetAll().Any(a => a.UserNameId == returnUserNameId() && a.StoreNameId == sellersInventoryVM.SellersInventory.StoreNameId && a.ProductId == sellersInventoryVM.SellersInventory.ProductId); if (!existStore) { _unitOfWork.SellersInventory.Add(sellersInventoryVM.SellersInventory); _unitOfWork.Save(); } } return(RedirectToAction(nameof(Index))); } return(View(sellersInventoryVM.SellersInventory)); }