public async Task <IActionResult> Create(LotEditVM vm) { if (_context.Lot.Where(u => u.LotNumber == vm.Lot.LotNumber).Any()) { ModelState.AddModelError("LotNumber", "A lot already exists with that lot number"); } if (_context.Lot.Where(u => u.TaxId == vm.Lot.TaxId).Any()) { ModelState.AddModelError("TaxId", "A lot already exists with that tax id"); } if (ModelState.IsValid) { var identityUser = await _userManager.GetUserAsync(HttpContext.User); var loggedInUser = _context.Owner.Find(identityUser.OwnerId); // We can auto fill these details because all lots will have same city/state/zip var address = new Address { StreetAddress = vm.Address.StreetAddress, City = "Some City", State = "Utah", Zip = "12345", LastModifiedBy = loggedInUser.FullName, LastModifiedDate = DateTime.Now }; _context.Add(address); await _context.SaveChangesAsync(); var lot = new Lot { LotNumber = vm.Lot.LotNumber, TaxId = vm.Lot.TaxId, Address = address, LastModifiedBy = loggedInUser.FullName, LastModifiedDate = DateTime.Now }; _context.Add(lot); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } //ViewData["AddressId"] = new SelectList(_context.Address, "Id", "Id", lot.AddressId); return(View(vm)); }
public async Task <IActionResult> Edit(int?id) { if (id == null) { return(NotFound()); } var lot = await _context.Lot.Include(u => u.Address).SingleOrDefaultAsync(u => u.LotId == id); if (lot == null) { return(NotFound()); } var currPrimary = await GetPrimaryOwnerAsync(id.Value); var currOwnerIds = await _context.OwnerLot .Include(u => u.Owner) .Where(u => u.LotId == id) .Where(u => !u.IsArchive) .Select(u => u.Owner.OwnerId) .ToListAsync(); var lotItems = await _context.LotInventory .Include(u => u.Inventory) .Where(u => u.LotId == id) .Select(u => u.Inventory.InventoryId) .ToListAsync(); var vm = new LotEditVM { Lot = lot, Address = lot.Address, OwnerId = currPrimary?.OwnerId ?? -1, OwnerIds = currOwnerIds, SelectedItems = lotItems }; ViewData["OwnerList"] = _context.Owner.Where(u => u.FullName != "Super Admin").ToList(); ViewData["Inventory"] = _context.Inventory.ToList(); return(View(vm)); }
public async Task <IActionResult> Edit(int id, LotEditVM vm) { if (id != vm.Lot.LotId) { return(NotFound()); } if (vm.OwnerIds != null && !vm.OwnerIds.Contains(vm.OwnerId)) { // This shouldn't come up, BUT JUST IN CASE ModelState.AddModelError("OwnerIds", "List of owners must contain the primary owner"); } if (ModelState.IsValid) { var identityUser = await _userManager.GetUserAsync(HttpContext.User); var loggedInUser = _context.Owner.Find(identityUser.OwnerId); try { // Update Address entry var addr = _context.Address.Find(vm.Address.Id); addr.StreetAddress = vm.Address.StreetAddress; addr.LastModifiedBy = loggedInUser.FullName; addr.LastModifiedDate = DateTime.Now; _context.Update(addr); // Update Lot entry var lot = _context.Lot.Find(vm.Lot.LotId); lot.LotNumber = vm.Lot.LotNumber; lot.TaxId = vm.Lot.TaxId; lot.LastModifiedBy = loggedInUser.FullName; lot.LastModifiedDate = DateTime.Now; _context.Update(lot); // Check OwnerLot entry for this owner/lot combination, create if necessary var currOwnerLot = await _context.OwnerLot .Where(u => u.LotId == id) .Where(u => !u.IsArchive) .Where(u => u.IsPrimary) .FirstOrDefaultAsync(); // Nobody owns this lot right now and we have owners if (currOwnerLot == null && vm.OwnerIds != null) { // Create a new entry for every owner foreach (var oid in vm.OwnerIds) { var newOwnerLot = new OwnerLot { OwnerId = vm.OwnerId, LotId = id, IsPrimary = oid == vm.OwnerId, StartDate = DateTime.Now }; _context.Add(newOwnerLot); } } // There is at least one existing relationship else if (currOwnerLot != null && vm.OwnerIds != null) { var prevOwnerLots = await _context.OwnerLot .Where(u => u.LotId == id) .Where(u => !u.IsArchive) .ToListAsync(); // Check previous OwnerLot entities to see if any were removed foreach (var ol in prevOwnerLots) { // User was removed from the user list, end the relationship if (!vm.OwnerIds.Contains(ol.OwnerId)) { ol.EndDate = DateTime.Now; ol.IsArchive = true; } // The user already has a relationship - don't do anything in the next step else { // Make sure that the primary user is set correctly ol.IsPrimary = ol.OwnerId == vm.OwnerId; vm.OwnerIds.Remove(ol.OwnerId); } } // Create new relationships if necessary foreach (var oid in vm.OwnerIds) { var newOwnerLot = new OwnerLot { OwnerId = oid, LotId = id, IsPrimary = oid == vm.OwnerId, StartDate = DateTime.Now }; _context.Add(newOwnerLot); } } // Need to check the inventory relationships var currItems = _context.LotInventory .Where(u => u.LotId == id) .ToList(); foreach (var item in currItems) { // Item is still selected - remove it for next step if (vm.SelectedItems.Contains(item.InventoryId)) { vm.SelectedItems.Remove(item.InventoryId); } // Item is not selected anymore - need to remove the relationship else { _context.LotInventory.Remove(item); } } // Any items still in SelectedItems need to be added in the relationship table if (vm.SelectedItems != null) { foreach (var invId in vm.SelectedItems) { _context.LotInventory.Add(new LotInventory { LotId = id, InventoryId = invId, LastModifiedBy = loggedInUser.FullName, LastModifiedDate = DateTime.Now }); } } await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!LotExists(vm.Lot.LotId)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } //ViewData["AddressId"] = new SelectList(_context.Address, "Id", "Id", lot.AddressId); //ViewData["Owner"] = new SelectList(_context.Owner, "OwnerId", "FullName"); ViewData["OwnerList"] = _context.Owner.Where(u => u.FullName != "Super Admin").ToList(); return(View(vm)); }