public Resident GetResident(Guid referenceId) { // Get resident and other objects and assemble here... Resident resident = _GetResident(referenceId); if (resident == null) { return(null); } int residentId = resident.Id; // Get all contacts and addresses var addresses = GetAddressesByResidentId(residentId); var residentContacts = _residentContactDataProvider.GetResidentContactsByResidentId(residentId); //GetResidentContactsByResidentId(residentId); var nextofkins = GetNextOfKinsByResidentId(residentId); var socialWorker = _socialWorkerDataProvider.GetSocialWorkerByResidentId(residentId); // GetSocialWorker(residentId); // assign resident address, email and phone if (addresses.Any()) { resident.Address = addresses.Where(a => a.ResidentId == residentId).FirstOrDefault(); } else { resident.Address = new Address(); } if (residentContacts.Any()) { residentContacts.ForEach(rc => { if (rc.ContactType == CONTACT_TYPE.email.ToString()) { resident.EmailAddress = rc.Data; } if (rc.ContactType == CONTACT_TYPE.phone.ToString()) { resident.PhoneNumber = rc.Data; } }); } if (socialWorker != null) { resident.SocialWorker = new SocialWorker() { ForeName = socialWorker.ForeName, SurName = socialWorker.SurName, EmailAddress = socialWorker.EmailAddress, PhoneNumber = socialWorker.PhoneNumber }; } return(resident); }
public Task <Resident> Update(ResidentRequest resident) { var residentExisting = GetResident(resident.ReferenceId); if (residentExisting == null) { throw new ArgumentNullException(nameof(resident)); } var residentEntity = ConvertToResidentEntity(resident); residentEntity.Id = residentExisting.Id; // Contact Info. Issue: Contact info is separate table but Email and Phone comes as values // Need to find if already exists? if so update else insert.. var existingResidentContacts = _residentContactDataProvider.GetResidentContactsByResidentId(residentEntity.Id); // _residentDataProvider.GetResidentContactsByResidentId(residentEntity.Id); List <ResidentContact> rcs = new List <ResidentContact>(); if (existingResidentContacts.Any()) { // get existing email address or phone existingResidentContacts.ForEach((rc) => { if (!string.IsNullOrEmpty(rc.ContactType) && rc.ContactType == CONTACT_TYPE.email.ToString()) { rc.Id = rc.Id; rc.Data = resident.EmailAddress; } if (!string.IsNullOrEmpty(rc.ContactType) && rc.ContactType == CONTACT_TYPE.phone.ToString()) { rc.Id = rc.Id; rc.Data = resident.PhoneNumber; } rcs.Add(rc); }); } else { // No existing contacts found if (!string.IsNullOrEmpty(residentEntity.EmailAddress)) { ResidentContact rc = new ResidentContact() { ContactType = CONTACT_TYPE.email.ToString(), Data = residentEntity.EmailAddress }; rcs.Add(rc); } if (!string.IsNullOrEmpty(residentEntity.PhoneNumber)) { ResidentContact rc = new ResidentContact() { ContactType = CONTACT_TYPE.phone.ToString(), Data = residentEntity.PhoneNumber }; rcs.Add(rc); } } residentEntity.ResidentContacts = rcs.ToArray(); // SocialWorker Info. Issue: SW info is separate table SocialWorker swToBeUpdIns = new SocialWorker(); if (resident.SocialWorker != null && resident.SocialWorker.ForeName != "") { swToBeUpdIns.ForeName = resident.SocialWorker.ForeName; swToBeUpdIns.SurName = resident.SocialWorker.SurName; swToBeUpdIns.EmailAddress = resident.SocialWorker.EmailAddress; swToBeUpdIns.PhoneNumber = resident.SocialWorker.PhoneNumber; } // Need to find if already exists? if so update else insert.. SocialWorker existingSocialWorker = _socialWorkerDataProvider.GetSocialWorkerByResidentId(residentEntity.Id); //_residentDataProvider.GetSocialWorker(residentEntity.Id); if (existingSocialWorker != null) { swToBeUpdIns.Id = existingSocialWorker.Id; } residentEntity.SocialWorker = swToBeUpdIns; var residentEntityUpdated = _residentDataProvider.Update(residentEntity); // todo: return new resident... var residentCreated = new Resident() { ReferenceId = residentEntity.ReferenceId }; return(Task.FromResult(residentCreated)); }