public override T Convert <T, U>(U entity) { Doctor doctor = entity as Doctor; if (doctor == null) { return(default(T)); } BO.Doctor doctorBO = new BO.Doctor(); doctorBO.ID = doctor.Id; doctorBO.LicenseNumber = doctor.LicenseNumber; doctorBO.WCBAuthorization = doctor.WCBAuthorization; doctorBO.WcbRatingCode = doctor.WcbRatingCode; doctorBO.NPI = doctor.NPI; doctorBO.Title = doctor.Title; doctorBO.TaxType = (BO.GBEnums.TaxType)doctor.TaxTypeId; if (doctor.IsDeleted.HasValue) { doctorBO.IsDeleted = doctor.IsDeleted.Value; } if (doctor.UpdateByUserID.HasValue) { doctorBO.UpdateByUserID = doctor.UpdateByUserID.Value; } doctorBO.IsCalendarPublic = doctor.IsCalendarPublic; if (doctor.User != null) { if (doctor.User.IsDeleted.HasValue == false || (doctor.User.IsDeleted.HasValue == true && doctor.User.IsDeleted.Value == false)) { BO.User boUser = new BO.User(); using (UserRepository sr = new UserRepository(_context)) { boUser = sr.Convert <BO.User, User>(doctor.User); doctorBO.user = boUser; } if (doctor.DoctorSpecialities != null) { List <BO.DoctorSpeciality> lstDoctorSpecility = new List <BO.DoctorSpeciality>(); foreach (var item in doctor.DoctorSpecialities) { if (item.IsDeleted == false) { using (DoctorSpecialityRepository sr = new DoctorSpecialityRepository(_context)) { lstDoctorSpecility.Add(sr.ObjectConvert <BO.DoctorSpeciality, DoctorSpeciality>(item)); } } } doctorBO.DoctorSpecialities = lstDoctorSpecility; } if (doctor.DoctorRoomTestMappings != null) { List <BO.DoctorRoomTestMapping> lstDoctorRoomTestMapping = new List <BO.DoctorRoomTestMapping>(); foreach (var item in doctor.DoctorRoomTestMappings) { if (item.IsDeleted == false) { BO.DoctorRoomTestMapping doctorRoomTestMappingBO = new BO.DoctorRoomTestMapping(); doctorRoomTestMappingBO.ID = item.Id; doctorRoomTestMappingBO.IsDeleted = item.IsDeleted; if (doctorRoomTestMappingBO.UpdateByUserID.HasValue) { doctorRoomTestMappingBO.UpdateByUserID = item.UpdateByUserID.Value; } if (item.RoomTest != null && (item.RoomTest.IsDeleted.HasValue == false || (item.RoomTest.IsDeleted.HasValue == true && item.RoomTest.IsDeleted.Value == false))) { BO.RoomTest boRoomTest = new BO.RoomTest(); using (RoomTestRepository sr = new RoomTestRepository(_context)) { boRoomTest = sr.Convert <BO.RoomTest, RoomTest>(item.RoomTest); doctorRoomTestMappingBO.RoomTest = boRoomTest; } } lstDoctorRoomTestMapping.Add(doctorRoomTestMappingBO); } } doctorBO.DoctorRoomTestMappings = lstDoctorRoomTestMapping; } //if (doctor.User.UserCompanies != null && doctorBO.user.UserCompanies != null && doctorBO.user.UserCompanies.Count <= 0) if (doctor.User.UserCompanies != null) { List <BO.UserCompany> lstUserCompany = new List <BO.UserCompany>(); foreach (var item in doctor.User.UserCompanies) { if (item.IsDeleted.HasValue == false || (item.IsDeleted.HasValue == true && item.IsDeleted.Value == false)) { using (UserCompanyRepository sr = new UserCompanyRepository(_context)) { BO.UserCompany BOUserCompany = new BO.UserCompany(); BOUserCompany = sr.Convert <BO.UserCompany, UserCompany>(item); BOUserCompany.User = null; lstUserCompany.Add(BOUserCompany); } } } doctorBO.user.UserCompanies = lstUserCompany; } if (doctor.DoctorLocationSchedules != null) { List <BO.DoctorLocationSchedule> lstDoctorLocationSchedule = new List <BO.DoctorLocationSchedule>(); foreach (var item in doctor.DoctorLocationSchedules) { if (item.IsDeleted.HasValue == false || (item.IsDeleted.HasValue == true && item.IsDeleted.Value == false)) { using (DoctorLocationScheduleRepository sr = new DoctorLocationScheduleRepository(_context)) { BO.DoctorLocationSchedule BODoctorLocationSchedule = new BO.DoctorLocationSchedule(); BODoctorLocationSchedule = sr.Convert <BO.DoctorLocationSchedule, DoctorLocationSchedule>(item); BODoctorLocationSchedule.doctor = null; BODoctorLocationSchedule.schedule = null; lstDoctorLocationSchedule.Add(BODoctorLocationSchedule); } } } doctorBO.DoctorLocationSchedules = lstDoctorLocationSchedule; } } } return((T)(object)doctorBO); }
public override object Save <T>(T entity) { BO.Doctor doctorBO = (BO.Doctor)(object) entity; BO.ErrorObject errObj = new BO.ErrorObject(); BO.User userBO = new BO.User(); Doctor doctorDB = new Doctor(); User userDB = new User(); List <DoctorSpeciality> lstDoctorSpecility = new List <DoctorSpeciality>(); List <DoctorRoomTestMapping> lstDoctorRoomTestMapping = new List <DoctorRoomTestMapping>(); doctorDB.Id = doctorBO.ID; using (var dbContextTransaction = _context.Database.BeginTransaction()) { ////Find Record By ID User user_ = _context.Users.Include("UserCompanyRoles").Include("AddressInfo").Include("ContactInfo").Where(p => p.id == doctorBO.user.ID).FirstOrDefault <User>(); if (user_ != null) { BO.AddUser updUserBO = new BO.AddUser(); updUserBO.user = doctorBO.user; updUserBO.user.UserName = string.IsNullOrEmpty(user_.UserName) ? user_.UserName : doctorBO.user.UserName; updUserBO.user.FirstName = string.IsNullOrEmpty(user_.FirstName) ? user_.FirstName : doctorBO.user.FirstName; updUserBO.user.LastName = string.IsNullOrEmpty(user_.LastName) ? user_.LastName : doctorBO.user.LastName; updUserBO.user.MiddleName = string.IsNullOrEmpty(user_.MiddleName) ? user_.MiddleName: doctorBO.user.MiddleName; updUserBO.user.Gender = doctorBO.user.Gender; updUserBO.user.UserType = !Enum.IsDefined(typeof(BO.GBEnums.UserType), doctorBO.user.UserType) ? (BO.GBEnums.UserType)user_.UserType : doctorBO.user.UserType; updUserBO.user.ImageLink = string.IsNullOrEmpty(doctorBO.user.ImageLink) ? user_.ImageLink : doctorBO.user.ImageLink; updUserBO.user.C2FactAuthEmailEnabled = doctorBO.user.C2FactAuthEmailEnabled == true || doctorBO.user.C2FactAuthEmailEnabled == false ? doctorBO.user.C2FactAuthEmailEnabled : (bool)user_.C2FactAuthEmailEnabled; updUserBO.user.C2FactAuthEmailEnabled = doctorBO.user.C2FactAuthSMSEnabled == true || doctorBO.user.C2FactAuthSMSEnabled == false ? doctorBO.user.C2FactAuthSMSEnabled : (bool)user_.C2FactAuthSMSEnabled; updUserBO.user.ID = doctorBO.user.ID; updUserBO.user.Roles = doctorBO.user.Roles; updUserBO.company = doctorBO.user.UserCompanies.ToList().Select(p => p.Company).FirstOrDefault(); updUserBO.role = doctorBO.user.Roles.ToArray(); // if (doctorBO.DoctorSpecialities.Count > 0) updUserBO.DoctorSpecialities = doctorBO.user.DoctorSpecialities; if (doctorBO.user.AddressInfo != null && doctorBO.user.AddressInfo.ID > 0) { updUserBO.address = doctorBO.user.AddressInfo; } if (doctorBO.user.ContactInfo != null && doctorBO.user.ContactInfo.ID > 0) { updUserBO.contactInfo = doctorBO.user.ContactInfo; } using (UserRepository userRepo = new UserRepository(_context)) { object obj = userRepo.Save <BO.AddUser>(updUserBO); if (obj.GetType() == errObj.GetType()) { errObj = (BO.ErrorObject)obj; dbContextTransaction.Rollback(); return(new BO.ErrorObject { ErrorMessage = errObj.ErrorMessage, errorObject = "", ErrorLevel = ErrorLevel.Error }); } else { userBO = (BO.User)obj; } doctorDB.User = _context.Users.Include("UserCompanyRoles").Include("UserCompanies").Where(p => p.id == doctorBO.user.ID).FirstOrDefault <User>(); } //_context.Entry(user_).State = System.Data.Entity.EntityState.Modified; } else { BO.AddUser addUserBO = new BO.AddUser(); addUserBO.user = doctorBO.user; // addUserBO.user.DoctorSpecialities = doctorBO.DoctorSpecialities; addUserBO.user.Roles = doctorBO.user.Roles; addUserBO.company = doctorBO.user.UserCompanies.ToList().Select(p => p.Company).FirstOrDefault(); addUserBO.role = doctorBO.user.Roles.ToArray(); addUserBO.address = doctorBO.user.AddressInfo; addUserBO.contactInfo = doctorBO.user.ContactInfo; using (UserRepository userRepo = new UserRepository(_context)) { object obj = userRepo.Save <BO.AddUser>(addUserBO); if (obj.GetType() == errObj.GetType()) { errObj = (BO.ErrorObject)obj; dbContextTransaction.Rollback(); return(new BO.ErrorObject { ErrorMessage = errObj.ErrorMessage, errorObject = "", ErrorLevel = ErrorLevel.Error }); } else { userBO = (BO.User)obj; } doctorBO.user.ID = userBO.ID; doctorDB.User = _context.Users.Include("UserCompanyRoles").Include("UserCompanies").Where(p => p.id == doctorBO.user.ID && p.UserCompanyRoles.Any(x => x.RoleID == (int)BO.GBEnums.RoleType.Doctor)).FirstOrDefault <User>(); } } if (doctorBO.DoctorSpecialities.Count > 0) { _dbSetDocSpecility.RemoveRange(_context.DoctorSpecialities.Where(c => c.DoctorID == doctorBO.user.ID)); _context.SaveChanges(); Specialty specilityDB = null; DoctorSpeciality doctorSpecilityDB = null; foreach (var item in doctorBO.DoctorSpecialities) { BO.DoctorSpeciality doctorSpecialityBO = (BO.DoctorSpeciality)(object) item; specilityDB = new Specialty(); doctorSpecilityDB = new DoctorSpeciality(); doctorSpecilityDB.IsDeleted = doctorSpecialityBO.IsDeleted.HasValue ? doctorSpecialityBO.IsDeleted.Value : false; doctorSpecilityDB.Doctor = doctorDB; //Find Record By ID Specialty speclity = _context.Specialties.Where(p => p.id == doctorSpecialityBO.ID).FirstOrDefault <Specialty>(); if (speclity == null) { dbContextTransaction.Rollback(); return(new BO.ErrorObject { ErrorMessage = "Invalid specility " + item.ToString() + " details.", errorObject = "", ErrorLevel = ErrorLevel.Error }); } if (!lstDoctorSpecility.Select(p => p.Specialty).Contains(speclity)) { doctorSpecilityDB.Specialty = speclity; _context.Entry(speclity).State = System.Data.Entity.EntityState.Modified; lstDoctorSpecility.Add(doctorSpecilityDB); } ; } } doctorDB.DoctorSpecialities = lstDoctorSpecility; if (doctorBO.DoctorRoomTestMappings != null) { if (doctorBO.DoctorRoomTestMappings.Count > 0) { _dbSetDocRoomTestMapping.RemoveRange(_context.DoctorRoomTestMappings.Where(c => c.DoctorId == doctorBO.user.ID)); _context.SaveChanges(); RoomTest roomTestDB = null; DoctorRoomTestMapping doctorRoomTestMappingDB = null; foreach (var item in doctorBO.DoctorRoomTestMappings) { BO.DoctorRoomTestMapping doctorRoomTestMappingBO = (BO.DoctorRoomTestMapping)(object) item; roomTestDB = new RoomTest(); doctorRoomTestMappingDB = new DoctorRoomTestMapping(); doctorRoomTestMappingDB.IsDeleted = doctorRoomTestMappingBO.IsDeleted.HasValue ? doctorRoomTestMappingBO.IsDeleted.Value : false; doctorRoomTestMappingDB.Doctor = doctorDB; //Find Record By ID RoomTest roomTest = _context.RoomTests.Where(p => p.id == doctorRoomTestMappingBO.ID).FirstOrDefault <RoomTest>(); if (roomTest == null) { dbContextTransaction.Rollback(); return(new BO.ErrorObject { ErrorMessage = "Invalid specility " + item.ToString() + " details.", errorObject = "", ErrorLevel = ErrorLevel.Error }); } if (!lstDoctorRoomTestMapping.Select(p => p.RoomTest).Contains(roomTest)) { doctorRoomTestMappingDB.RoomTest = roomTest; _context.Entry(roomTest).State = System.Data.Entity.EntityState.Modified; lstDoctorRoomTestMapping.Add(doctorRoomTestMappingDB); } ; } } doctorDB.DoctorRoomTestMappings = lstDoctorRoomTestMapping; } if (doctorDB.Id > 0) { //Find Doctor By ID Doctor doctor = _context.Doctors.Where(p => p.Id == doctorBO.user.ID && (p.IsDeleted.HasValue == false || (p.IsDeleted.HasValue == true && p.IsDeleted.Value == false))).FirstOrDefault <Doctor>(); if (doctor == null) { doctorDB = new Doctor(); } #region Doctor doctorDB.Id = doctorBO.ID; doctorDB.LicenseNumber = string.IsNullOrEmpty(doctorBO.LicenseNumber) ? doctor.LicenseNumber : doctorBO.LicenseNumber; doctorDB.WCBAuthorization = string.IsNullOrEmpty(doctorBO.WCBAuthorization) ? doctor.WCBAuthorization : doctorBO.WCBAuthorization; doctorDB.WcbRatingCode = string.IsNullOrEmpty(doctorBO.WcbRatingCode) ? doctor.WcbRatingCode : doctorBO.WcbRatingCode; doctorDB.NPI = string.IsNullOrEmpty(doctorBO.NPI) ? doctor.NPI : doctorBO.NPI; doctorDB.Title = string.IsNullOrEmpty(doctorBO.Title) ? doctor.Title : doctorBO.Title; doctorDB.TaxTypeId = !Enum.IsDefined(typeof(BO.GBEnums.TaxType), doctorBO.TaxType) ? System.Convert.ToByte((BO.GBEnums.TaxType)doctor.TaxTypeId) : System.Convert.ToByte(doctorBO.TaxType); doctorDB.IsDeleted = doctorBO.IsDeleted.HasValue ? doctorBO.IsDeleted : (doctorBO.IsDeleted.HasValue ? doctor.IsDeleted : false); doctorDB.UpdateDate = doctorBO.UpdateDate; doctorDB.UpdateByUserID = doctorBO.UpdateByUserID; doctorDB.IsCalendarPublic = doctorBO.IsCalendarPublic; #endregion // doctorDB = doctor; // _context.Entry(doctorDB).State = System.Data.Entity.EntityState.Modified; doctorDB = _context.Doctors.Add(doctorDB); //else //{ // dbContextTransaction.Rollback(); // return new BO.ErrorObject { ErrorMessage = "Please pass valid doctor details.", errorObject = "", ErrorLevel = ErrorLevel.Error }; //} } else { doctorDB.LicenseNumber = doctorBO.LicenseNumber; doctorDB.WCBAuthorization = doctorBO.WCBAuthorization; doctorDB.WcbRatingCode = doctorBO.WcbRatingCode; doctorDB.NPI = doctorBO.NPI; doctorDB.Title = doctorBO.Title; doctorDB.TaxTypeId = System.Convert.ToByte(doctorBO.TaxType); doctorDB.IsDeleted = doctorBO.IsDeleted.HasValue ? doctorBO.IsDeleted : false; doctorDB.UpdateDate = doctorBO.UpdateDate; doctorDB.UpdateByUserID = doctorBO.UpdateByUserID; doctorDB.CreateDate = doctorBO.CreateDate; doctorDB.CreateByUserID = doctorBO.CreateByUserID; doctorDB.IsCalendarPublic = doctorBO.IsCalendarPublic; _dbSet.Add(doctorDB); } _context.SaveChanges(); dbContextTransaction.Commit(); } var res = Convert <BO.Doctor, Doctor>(doctorDB); return((object)res); }