public async Task <IActionResult> OnPostAsync(int?id) { var uId = HttpContext.Session.GetInt32("SessionUserID"); if (uId == null) { return(RedirectToPage("./User/Login")); } if (id == null) { return(NotFound()); } if (!ModelState.IsValid) { return(Redirect("./Index")); } var user = _context.Owner.FirstOrDefault(x => x.User.UserID == HttpContext.Session.GetInt32("SessionUserID")); Key.LastModifiedBy = user != null ? user.Initials : "SYS"; Key.LastModifiedDate = DateTime.Now; Key.KeyHistory.Status = "Returned"; Key.KeyHistory.DateReturned = DateTime.Now; _context.Attach(Key).State = EntityState.Modified; if (KeyHistoryExists(Key.KeyID, Key.KeyHistory.KeyHistoryID)) { Key.KeyHistory.LastModifiedBy = user != null ? user.Initials : "SYS"; Key.KeyHistory.LastModifiedDate = DateTime.Now; Key.KeyHistory.Status = "Returned"; _context.Attach(Key.KeyHistory).State = EntityState.Modified; } else { Key.KeyHistory = null; } try { await _context.SaveChangesAsync().ConfigureAwait(false); } catch (DbUpdateConcurrencyException) { if (!KeyExists(Key.KeyID)) { return(NotFound()); } else { throw; } } return(Redirect("./Index")); }
public async Task <IActionResult> OnPostAsync(int?id, string ownerName = "") { if (id == null) { return(NotFound()); } var user = _context.Owner.FirstOrDefault(x => x.User.UserID == HttpContext.Session.GetInt32("SessionUserID")); Key.LastModifiedBy = user != null ? user.Initials : "SYS"; Key.LastModifiedDate = DateTime.Now; _context.Attach(Key).State = EntityState.Modified; if (KeyHistoryExists(Key.KeyID, Key.KeyHistory.KeyHistoryID)) { Key.LastModifiedBy = user != null ? user.Initials : "SYS"; Key.KeyHistory.LastModifiedDate = DateTime.Now; if ((Key.KeyHistory?.Status == "Returned" || Key.KeyHistory?.Status == "Lost") && Key.KeyHistory?.DateReturned == null) { Key.KeyHistory.DateReturned = DateTime.Now; } if (Key.KeyHistory?.Status == "Active") { Key.KeyHistory.DateReturned = null; } Key.KeyHistory.OwnerID = _context.Owner.FirstOrDefault(x => x.FullName == ownerName).OwnerID; _context.Attach(Key.KeyHistory).State = EntityState.Modified; } else { KeyHistory keyHistory = Key.KeyHistory; _context.KeyHistory.Add(keyHistory); } try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!KeyExists(Key.KeyID)) { return(NotFound()); } else { throw; } } return(RedirectToPage("./Index")); }
public async Task <IActionResult> OnPostAsync(int?id, string ownerName, string lotNumber) { if (!ModelState.IsValid) { return(Page()); } if (ownerName == null) { ModelState.AddModelError("Account", "You must specify an owner."); return(await OnGetAsync(id)); } var ownerLotNumbers = _context.Lots .Where(x => x.Owner.FullName == ownerName).Select(x => x.LotNumber).ToList(); if (!ownerLotNumbers.Contains(lotNumber)) { ModelState.AddModelError("LotNumber", "That lot does not currently belog to that owner."); return(await OnGetAsync(id)); } var user = _context.Owner.FirstOrDefault(x => x.User.UserID == HttpContext.Session.GetInt32("SessionUserID")); OwnerHistory.LastModifiedBy = user != null ? user.Initials : "SYS"; OwnerHistory.LastModifiedDate = DateTime.Now; OwnerHistory.OwnerID = _context.Owner.FirstOrDefault(o => o.FullName == ownerName).OwnerID; if (!string.IsNullOrEmpty(lotNumber)) { OwnerHistory.LotID = _context.Lots.FirstOrDefault(l => l.LotNumber == lotNumber).LotID; } else { OwnerHistory.LotID = null; } _context.Attach(OwnerHistory).State = EntityState.Modified; try { await _context.SaveChangesAsync().ConfigureAwait(false); } catch (DbUpdateConcurrencyException) { if (!OwnerHistoryExists(OwnerHistory.OwnerHistoryID)) { return(NotFound()); } else { throw; } } return(RedirectToPage("./Index")); }
public async Task <IActionResult> OnPostAsync(int?id, string[] selectedContacts, int?isAdmin, string coowner = "") { if (!ModelState.IsValid) { return(Page()); } if (selectedContacts[0] == null || selectedContacts[2] == null) { ModelState.AddModelError("Contacts", "You must enter both a primary phone and email."); await OnGetAsync(id); return(Page()); } Models.Owner ownerToUpdate = await _context.Owner .Include(o => o.Address) .Include(o => o.OwnerContactType).ThenInclude(c => c.ContactType) .FirstOrDefaultAsync(m => m.OwnerID == id); var user = _context.Owner.FirstOrDefault( x => x.User.UserID == HttpContext.Session.GetInt32("SessionUserID")); ownerToUpdate.LastModifiedBy = user != null ? user.Initials : "SYS"; ownerToUpdate.LastModifiedDate = DateTime.Now; ownerToUpdate.IsHoaOwner = true; ownerToUpdate.IsPrimary = true; var owners = _context.Owner.Where(x => x.IsHoaOwner == true).Select(x => x.FullName).ToList(); if (!owners.Contains(coowner) && !string.IsNullOrEmpty(coowner)) { CreateCoOwner(coowner); } ownerToUpdate.CoOwnerID = _context.Owner.FirstOrDefault(x => x.FullName == coowner)?.OwnerID; selectedContacts[0] = selectedContacts[0] != null?Extensions.CleanPhone(selectedContacts[0]) : null; selectedContacts[1] = selectedContacts[1] != null?Extensions.CleanPhone(selectedContacts[1]) : null; selectedContacts[3] = selectedContacts[3] != null?Extensions.CleanPhone(selectedContacts[3]) : null; if (await TryUpdateModelAsync <Models.Owner>( ownerToUpdate, "Owner", o => o.Birthday, o => o.FirstName, o => o.LastName, o => o.Address, o => o.Occupation, o => o.IsPrimary, o => o.IsHoaOwner, o => o.EmergencyContactName, o => o.EmergencyContactPhone, o => o.LastModifiedBy, o => o.LastModifiedDate)) { if (string.IsNullOrWhiteSpace(ownerToUpdate.Address?.FullAddress)) { ownerToUpdate.Address = null; } ownerToUpdate.EmergencyContactPhone = Extensions.CleanPhone(ownerToUpdate.EmergencyContactPhone); var email = selectedContacts[2]; //TODO: not super safe(will break if not in expected order UpdateUser(ownerToUpdate.OwnerID, isAdmin, email); //saves address _context.Attach(ownerToUpdate).State = EntityState.Modified; UpdateOwnerContacts(_context, selectedContacts, ownerToUpdate, user); await _context.SaveChangesAsync(); return(RedirectToPage("./Index")); } UpdateOwnerContacts(_context, selectedContacts, ownerToUpdate, user); PopulateAssignedOwnerContacts(_context, ownerToUpdate); return(Page()); }