public async Task <string> Create(EvacuationFile evacuationFile) { VerifyEvacuationFileInvariants(evacuationFile); var primaryContact = essContext.contacts.Where(c => c.statecode == (int)EntityState.Active && c.contactid == Guid.Parse(evacuationFile.PrimaryRegistrantId)).SingleOrDefault(); if (primaryContact == null) { throw new ArgumentException($"Primary registrant {evacuationFile.PrimaryRegistrantId} not found"); } var file = mapper.Map <era_evacuationfile>(evacuationFile); file.era_evacuationfileid = Guid.NewGuid(); essContext.AddToera_evacuationfiles(file); essContext.SetLink(file, nameof(era_evacuationfile.era_EvacuatedFromID), essContext.LookupJurisdictionByCode(file._era_evacuatedfromid_value?.ToString())); AssignPrimaryRegistrant(file, primaryContact); AssignToTask(file, evacuationFile.TaskId); AddPets(file); AddNeedsAssessment(file, file.era_CurrentNeedsAssessmentid); await essContext.SaveChangesAsync(); essContext.Detach(file); //get the autogenerated evacuation file number var essFileNumber = essContext.era_evacuationfiles.Where(f => f.era_evacuationfileid == file.era_evacuationfileid).Select(f => f.era_name).Single(); essContext.DetachAll(); return(essFileNumber); }
private void VerifyEvacuationFileInvariants(EvacuationFile evacuationFile, era_evacuationfile currentFile = null) { //Check invariants if (string.IsNullOrEmpty(evacuationFile.PrimaryRegistrantId)) { throw new ArgumentNullException($"The file has no associated primary registrant"); } if (evacuationFile.NeedsAssessment == null) { throw new ArgumentNullException($"File {evacuationFile.Id} must have a needs assessment"); } if (evacuationFile.Id == null) { if (evacuationFile.NeedsAssessment.HouseholdMembers.Count(m => m.IsPrimaryRegistrant) != 1) { throw new InvalidOperationException($"File {evacuationFile.Id} must have a single primary registrant household member"); } } else { if (evacuationFile.NeedsAssessment.HouseholdMembers.Count(m => m.IsPrimaryRegistrant) > 1) { throw new InvalidOperationException($"File {evacuationFile.Id} can not have multiple primary registrant household members"); } if (currentFile != null && currentFile.era_era_evacuationfile_era_householdmember_EvacuationFileid != null && currentFile.era_era_evacuationfile_era_householdmember_EvacuationFileid.Any(m => m.era_isprimaryregistrant == true) && evacuationFile.NeedsAssessment.HouseholdMembers.Any(m => m.IsPrimaryRegistrant && string.IsNullOrEmpty(m.Id))) { throw new InvalidOperationException($"File {evacuationFile.Id} can not have multiple primary registrant household members"); } } }
private void VerifyEvacuationFileInvariants(EvacuationFile evacuationFile) { //Check invariants if (string.IsNullOrEmpty(evacuationFile.PrimaryRegistrantId)) { throw new Exception($"The file has no associated primary registrant"); } if (evacuationFile.NeedsAssessment == null) { throw new Exception($"File {evacuationFile.Id} must have a needs assessment"); } if (evacuationFile.Id == null) { if (evacuationFile.NeedsAssessment.HouseholdMembers.Count(m => m.IsPrimaryRegistrant) != 1) { throw new Exception($"File {evacuationFile.Id} must have a single primary registrant household member"); } } else { if (evacuationFile.NeedsAssessment.HouseholdMembers.Count(m => m.IsPrimaryRegistrant) > 1) { throw new Exception($"File {evacuationFile.Id} can not have multiple primary registrant household members"); } } }
public async Task <string> Update(EvacuationFile evacuationFile) { VerifyEvacuationFileInvariants(evacuationFile); var currentFile = essContext.era_evacuationfiles .Where(f => f.era_name == evacuationFile.Id).SingleOrDefault(); if (currentFile == null) { throw new Exception($"Evacuation file {evacuationFile.Id} not found"); } var primaryContact = essContext.contacts.Where(c => c.statecode == (int)EntityState.Active && c.contactid == Guid.Parse(evacuationFile.PrimaryRegistrantId)).SingleOrDefault(); if (primaryContact == null) { throw new Exception($"Primary registrant {evacuationFile.PrimaryRegistrantId} not found"); } RemovePets(currentFile); essContext.Detach(currentFile); var file = mapper.Map <era_evacuationfile>(evacuationFile); file.era_evacuationfileid = currentFile.era_evacuationfileid; essContext.AttachTo(nameof(essContext.era_evacuationfiles), file); essContext.SetLink(file, nameof(era_evacuationfile.era_EvacuatedFromID), essContext.LookupJurisdictionByCode(file._era_evacuatedfromid_value?.ToString())); foreach (var member in file.era_era_evacuationfile_era_householdmember_EvacuationFileid) { if (member.era_householdmemberid.HasValue) { //update member essContext.AttachTo(nameof(essContext.era_householdmembers), member); essContext.UpdateObject(member); AssignHouseholdMember(file, member); } } essContext.UpdateObject(file); AssignPrimaryRegistrant(file, primaryContact); AssignToTask(file, evacuationFile.TaskId); AddPets(file); await essContext.SaveChangesAsync(); essContext.DetachAll(); essContext.AttachTo(nameof(essContext.era_evacuationfiles), file); AddNeedsAssessment(file, file.era_CurrentNeedsAssessmentid); await essContext.SaveChangesAsync(); essContext.DetachAll(); return(file.era_name); }
public async Task <string> Update(EvacuationFile file) { if (string.IsNullOrEmpty(file.PrimaryRegistrantId)) { throw new Exception($"File {file.Id} has no associated primary registrant"); } var existingEvacuationFile = essContext.era_evacuationfiles.Where(e => e.era_name == file.Id).SingleOrDefault(); if (existingEvacuationFile == null) { throw new Exception($"File {file.Id} not found"); } var primaryContact = essContext.contacts.Where(c => c.contactid == Guid.Parse(file.PrimaryRegistrantId)).SingleOrDefault(); if (primaryContact == null) { throw new Exception($"Primary registrant {file.PrimaryRegistrantId} not found"); } var updatedEvacuationFile = mapper.Map <era_evacuationfile>(file); updatedEvacuationFile.era_evacuationfileid = existingEvacuationFile.era_evacuationfileid; essContext.Detach(existingEvacuationFile); essContext.AttachTo(nameof(essContext.era_evacuationfiles), updatedEvacuationFile); essContext.UpdateObject(updatedEvacuationFile); essContext.AddLink(primaryContact, nameof(contact.era_evacuationfile_Registrant), updatedEvacuationFile); essContext.SetLink(updatedEvacuationFile, nameof(era_evacuationfile.era_Registrant), primaryContact); essContext.AddLink(essContext.LookupJurisdictionByCode(file.EvacuatedFromAddress.Community), nameof(era_jurisdiction.era_evacuationfile_Jurisdiction), updatedEvacuationFile); foreach (var needsAssessment in file.NeedsAssessments) { if (needsAssessment.HouseholdMembers.Count(m => m.IsPrimaryRegistrant && m.LinkedRegistrantId == null) == 1) { throw new Exception($"File {file.Id} must have a single primary registrant household member"); } var eraNeedsAssessment = mapper.Map <era_needassessment>(needsAssessment); var members = mapper.Map <IEnumerable <era_householdmember> >(needsAssessment.HouseholdMembers); var pets = mapper.Map <IEnumerable <era_needsassessmentanimal> >(needsAssessment.Pets); CreateNeedsAssessment(updatedEvacuationFile, eraNeedsAssessment, members, pets); } await essContext.SaveChangesAsync(); essContext.DetachAll(); return(updatedEvacuationFile.era_name); }
public async Task <string> Create(EvacuationFile file) { if (string.IsNullOrEmpty(file.PrimaryRegistrantId)) { throw new Exception($"The file has no associated primary registrant"); } var primaryContact = essContext.contacts.Where(c => c.statecode == (int)EntityState.Active && c.contactid == Guid.Parse(file.PrimaryRegistrantId)).SingleOrDefault(); if (primaryContact == null) { throw new Exception($"Primary registrant {file.PrimaryRegistrantId} not found"); } var eraEvacuationFile = mapper.Map <era_evacuationfile>(file); eraEvacuationFile.era_evacuationfileid = Guid.NewGuid(); eraEvacuationFile.era_evacuationfiledate = DateTimeOffset.UtcNow; essContext.AddToera_evacuationfiles(eraEvacuationFile); essContext.AddLink(primaryContact, nameof(primaryContact.era_evacuationfile_Registrant), eraEvacuationFile); essContext.SetLink(eraEvacuationFile, nameof(era_evacuationfile.era_Registrant), primaryContact); essContext.AddLink(essContext.LookupJurisdictionByCode(file.EvacuatedFromAddress.CommunityCode), nameof(era_jurisdiction.era_evacuationfile_Jurisdiction), eraEvacuationFile); foreach (var needsAssessment in file.NeedsAssessments) { if (needsAssessment.HouseholdMembers.Count(m => m.IsPrimaryRegistrant && m.LinkedRegistrantId == null) == 1) { throw new Exception($"File {file.Id} must have a single primary registrant household member"); } var eraNeedsAssessment = mapper.Map <era_needassessment>(needsAssessment); var members = mapper.Map <IEnumerable <era_householdmember> >(needsAssessment.HouseholdMembers); var pets = mapper.Map <IEnumerable <era_needsassessmentanimal> >(needsAssessment.Pets); CreateNeedsAssessment(eraEvacuationFile, eraNeedsAssessment, members, pets); } await essContext.SaveChangesAsync(); essContext.Detach(eraEvacuationFile); var createdFile = essContext.era_evacuationfiles.Where(f => f.era_evacuationfileid == eraEvacuationFile.era_evacuationfileid).Single(); var essFileNumber = createdFile.era_name; essContext.DetachAll(); return(essFileNumber); }