public async Task <IActionResult> OnGetAsync(int?id) { if (id == null) { return(NotFound()); } InaccessibleProperty = await _context.InaccessibleProperties .Include(i => i.Territory) .ThenInclude(x => x.StreetTerritory) .AsNoTracking() .FirstOrDefaultAsync(m => m.InaccessiblePropertyId == id); if (InaccessibleProperty.CurrentContactListId != null) { InaccessibleContactList = await _context.InaccessibleContactLists .Include(x => x.Contacts) .ThenInclude(x => x.PhoneType) .AsNoTracking() .FirstOrDefaultAsync(x => x.InaccessibleContactListId == InaccessibleProperty.CurrentContactListId); } if (InaccessibleProperty == null) { return(NotFound()); } return(Page()); }
public async Task <IActionResult> OnPostAsync(int?id) { if (id == null) { return(NotFound()); } var serializer = new JsonSerializer(); using (var memoryStream = new MemoryStream()) { await Upload.CopyToAsync(memoryStream); using (var streamReader = new StreamReader(memoryStream)) using (var jsonTextReader = new JsonTextReader(streamReader)) { await memoryStream.FlushAsync(); memoryStream.Position = 0; var uploadJson = (JArray)JToken.ReadFrom(jsonTextReader); //validate the json var list = new InaccessibleContactList() { InaccessiblePropertyId = id ?? 0, CreateDate = System.DateTime.UtcNow }; foreach (JObject objContact in uploadJson) { PhoneTypeEnum phoneType; switch ((string)objContact["PhoneType"]) { case "C": phoneType = PhoneTypeEnum.Mobile; break; case "M": phoneType = PhoneTypeEnum.Mobile; break; case "L": phoneType = PhoneTypeEnum.Landline; break; case "V": phoneType = PhoneTypeEnum.Voip; break; default: phoneType = PhoneTypeEnum.Landline; break; } list.Contacts.Add( new InaccessibleContact() { FirstName = (string)objContact["FirstName"], LastName = (string)objContact["LastName"], MiddleInitial = (string)objContact["MiddleInitial"], Age = (int)objContact["Age"], MailingAddress1 = (string)objContact["MailingAddress1"], MailingAddress2 = (string)objContact["MailingAddress2"], PostalCode = (string)objContact["PostalCode"], PhoneNumber = (string)objContact["PhoneNumber"], PhoneTypeId = (int)phoneType } ); } await _context.InaccessibleContactLists.AddAsync(list); await _context.SaveChangesAsync(); var property = new InaccessibleProperty() { InaccessiblePropertyId = id ?? 0, CurrentContactListId = list.InaccessibleContactListId }; _context.InaccessibleProperties.Attach(property); _context.Entry(property).Property(X => X.CurrentContactListId).IsModified = true; await _context.SaveChangesAsync(); } } InaccessibleProperty = await _context.InaccessibleProperties .Include(i => i.Territory) .ThenInclude(x => x.StreetTerritory) .AsNoTracking() .FirstOrDefaultAsync(m => m.InaccessiblePropertyId == id); if (InaccessibleProperty.CurrentContactListId != null) { InaccessibleContactList = await _context.InaccessibleContactLists .Include(x => x.Contacts) .AsNoTracking() .FirstOrDefaultAsync(x => x.InaccessibleContactListId == InaccessibleProperty.CurrentContactListId); } if (InaccessibleProperty == null) { return(NotFound()); } return(Page()); }
public void Populate() { // copy inaccessible territories from source var sourceTerritories = _sourceDb.InaccessibleTerritories .Include(x => x.StreetTerritory) .Include(x => x.InaccessibleProperties) .ThenInclude(x => x.ContactLists) .ThenInclude(x => x.Contacts) .ThenInclude(x => x.ContactActivity) .ThenInclude(x => x.Publisher) .AsNoTracking() .ToList(); foreach (var t in sourceTerritories) { var streetTerritoryId = _targetDb.StreetTerritories.Where(x => x.TerritoryCode == t.StreetTerritory.TerritoryCode).Select(x => x.TerritoryId).FirstOrDefault(); var territory = new InaccessibleTerritory { TerritoryCode = t.TerritoryCode, StreetTerritoryId = streetTerritoryId, InActive = t.InActive }; foreach (var p in t.InaccessibleProperties) { var property = new InaccessibleProperty { ResearchedDate = p.ResearchedDate, StreetNumbers = p.StreetNumbers, Street = p.Street, City = p.City, State = p.State, PostalCode = p.PostalCode, EstimatedDwellingCount = p.EstimatedDwellingCount, PropertyName = p.PropertyName, Description = p.Description }; foreach (var l in p.ContactLists) { var list = new InaccessibleContactList { CreateDate = l.CreateDate }; foreach (var c in l.Contacts) { int?assign = null; if (c.AssignPublisherId.HasValue) { assign = mapSourcePublisherTargetPublisher[c.AssignPublisherId.Value]; } var contact = new InaccessibleContact { AssignPublisherId = assign, AssignDate = c.AssignDate, FirstName = c.FirstName, LastName = c.LastName, MiddleInitial = c.MiddleInitial, Age = c.Age, MailingAddress1 = c.MailingAddress1, MailingAddress2 = c.MailingAddress2, City = c.City, State = c.State, PostalCode = c.PostalCode, PhoneTypeId = c.PhoneTypeId, PhoneNumber = c.PhoneNumber, EmailAddresses = c.EmailAddresses, }; foreach (var a in c.ContactActivity) { int pub = mapSourcePublisherTargetPublisher[a.PublisherId]; contact.ContactActivity.Add(new InaccessibleContactActivity { PublisherId = pub, ActivityDate = a.ActivityDate, ContactActivityTypeId = a.ContactActivityTypeId, PhoneCallerIdBlocked = a.PhoneCallerIdBlocked, PhoneResponseTypeId = a.PhoneResponseTypeId, Notes = a.Notes }); } list.Contacts.Add(contact); } property.ContactLists.Add(list); } territory.InaccessibleProperties.Add(property); } _targetDb.Add(territory); } _targetDb.SaveChanges(); //this will set the current contact list for the property _targetDb.Database.ExecuteSqlRaw(@" UPDATE InaccessibleProperties SET CurrentContactListId = ( SELECT MAX(InaccessibleContactListId) FROM InaccessibleContactLists i WHERE i.InaccessiblePropertyId = InaccessibleProperties.InaccessiblePropertyId )" ); var srcTerritories = _sourceDb.InaccessibleTerritories.Include(x => x.Activity); foreach (var srcTerritory in srcTerritories) { var targetTerritory = _targetDb.InaccessibleTerritories.Where(x => x.TerritoryCode == srcTerritory.TerritoryCode).FirstOrDefault(); foreach (var srcActivity in srcTerritory.Activity.OrderBy(x => x.CheckOutDate)) { targetTerritory.Activity.Add(new TerritoryActivity { PublisherId = mapSourcePublisherTargetPublisher[srcActivity.PublisherId], CheckOutDate = srcActivity.CheckOutDate, CheckInDate = srcActivity.CheckInDate, Notes = srcActivity.Notes }); } _targetDb.SaveChanges(); } mapSourceStreetTerritoryTargetStreetTerritory = new Dictionary <int, int>(); foreach (var srcStreetTerritory in _sourceDb.StreetTerritories) { var targetStreetTerritory = _targetDb.StreetTerritories.Where(x => x.TerritoryCode == srcStreetTerritory.TerritoryCode).FirstOrDefault(); mapSourceStreetTerritoryTargetStreetTerritory.Add(srcStreetTerritory.TerritoryId, targetStreetTerritory.TerritoryId); } foreach (var srcDoNotContactLetter in _sourceDb.DoNotContactLetters.OrderBy(x => x.ReportedDate)) { _targetDb.DoNotContactLetters.Add(new DoNotContactLetter { TerritoryId = mapSourceStreetTerritoryTargetStreetTerritory[srcDoNotContactLetter.TerritoryId], PublisherId = mapSourcePublisherTargetPublisher[srcDoNotContactLetter.PublisherId], ReportedDate = srcDoNotContactLetter.ReportedDate, MailingAddress1 = srcDoNotContactLetter.MailingAddress1, MailingAddress2 = srcDoNotContactLetter.MailingAddress2, Notes = srcDoNotContactLetter.Notes }); } foreach (var srcDoNotContactPhone in _sourceDb.DoNotContactPhones.OrderBy(x => x.ReportedDate)) { _targetDb.DoNotContactPhones.Add(new DoNotContactPhone { PublisherId = mapSourcePublisherTargetPublisher[srcDoNotContactPhone.PublisherId], ReportedDate = srcDoNotContactPhone.ReportedDate, PhoneNumber = srcDoNotContactPhone.PhoneNumber, Notes = srcDoNotContactPhone.Notes }); } foreach (var srcDoNotContactStreet in _sourceDb.DoNotContactStreets.OrderBy(x => x.ReportedDate)) { _targetDb.DoNotContactStreets.Add(new DoNotContactStreet { TerritoryId = mapSourceStreetTerritoryTargetStreetTerritory[srcDoNotContactStreet.TerritoryId], PublisherId = mapSourcePublisherTargetPublisher[srcDoNotContactStreet.PublisherId], ReportedDate = srcDoNotContactStreet.ReportedDate, StreetAddress = srcDoNotContactStreet.StreetAddress, Coordinates = srcDoNotContactStreet.Coordinates, Notes = srcDoNotContactStreet.Notes }); } _targetDb.SaveChanges(); }