public override T Convert <T, U>(U entity)
        {
            DoctorSpeciality doctorspecility = entity as DoctorSpeciality;

            if (doctorspecility == null)
            {
                return(default(T));
            }

            BO.DoctorSpeciality doctorspecilityBO = new BO.DoctorSpeciality();

            doctorspecilityBO.IsDeleted = doctorspecility.IsDeleted;
            if (doctorspecilityBO.UpdateByUserID.HasValue)
            {
                doctorspecilityBO.UpdateByUserID = doctorspecility.UpdateByUserID.Value;
            }

            BO.Doctor boDoctor = new BO.Doctor();
            using (DoctorRepository sr = new DoctorRepository(_context))
            {
                boDoctor = sr.Convert <BO.Doctor, Doctor>(doctorspecility.Doctor);
                doctorspecilityBO.Doctor = boDoctor;
            }

            BO.Specialty boSpecliality = new BO.Specialty();
            using (SpecialityRepository sr = new SpecialityRepository(_context))
            {
                boSpecliality = sr.Convert <BO.Specialty, Specialty>(doctorspecility.Specialty);
                doctorspecilityBO.Specialty = boSpecliality;
            }

            return((T)(object)doctorspecilityBO);
        }
        public override T ObjectConvert <T, U>(U entity)
        {
            DoctorSpeciality doctorspecility = entity as DoctorSpeciality;

            if (doctorspecility == null)
            {
                return(default(T));
            }

            BO.DoctorSpeciality doctorspecilityBO = new BO.DoctorSpeciality();
            doctorspecilityBO.ID        = doctorspecility.id;
            doctorspecilityBO.IsDeleted = doctorspecility.IsDeleted;
            if (doctorspecilityBO.UpdateByUserID.HasValue)
            {
                doctorspecilityBO.UpdateByUserID = doctorspecility.UpdateByUserID.Value;
            }

            if (doctorspecility.Specialty != null && (doctorspecility.Specialty.IsDeleted.HasValue == false || (doctorspecility.Specialty.IsDeleted.HasValue == true && doctorspecility.Specialty.IsDeleted.Value == false)))
            {
                BO.Specialty boSpecliality = new BO.Specialty();
                using (SpecialityRepository sr = new SpecialityRepository(_context))
                {
                    boSpecliality = sr.Convert <BO.Specialty, Specialty>(doctorspecility.Specialty);
                    doctorspecilityBO.Specialty = boSpecliality;
                }
            }

            return((T)(object)doctorspecilityBO);
        }
        public override List <MIDAS.GBX.BusinessObjects.BusinessValidation> Validate <T>(T entity)
        {
            BO.DoctorSpeciality doctor = (BO.DoctorSpeciality)(object) entity;
            var result = doctor.Validate(doctor);

            return(result);
        }
 public override object Get(int id)
 {
     BO.DoctorSpeciality acc_ = Convert <BO.DoctorSpeciality, DoctorSpeciality>(_context.DoctorSpecialities.Where(p => p.id == id && p.IsDeleted == false).FirstOrDefault <DoctorSpeciality>());
     if (acc_ == null)
     {
         return(new BO.ErrorObject {
             ErrorMessage = "No record found for this Specialty.", errorObject = "", ErrorLevel = ErrorLevel.Error
         });
     }
     return((object)acc_);
 }
        public override object Delete <T>(T entity)
        {
            BO.DoctorSpeciality doctorBO = entity as BO.DoctorSpeciality;

            DoctorSpeciality doctorDB = new DoctorSpeciality();

            doctorDB.id = doctorBO.ID;
            _dbSet.Remove(_context.DoctorSpecialities.Single <DoctorSpeciality>(p => p.id == doctorBO.ID));
            _context.SaveChanges();

            var res = (BO.GbObject)(object) entity;

            return(doctorBO);
        }
        public override object Save <T>(T entity)
        {
            BO.DoctorSpeciality doctorSpecialityBO = (BO.DoctorSpeciality)(object) entity;

            DoctorSpeciality doctorSpecilityDB = null;
            Doctor           doctorDB          = null;
            Specialty        specilityDB       = null;

            var oldDoctorSpecilities = _context.DoctorSpecialities.Where(p => p.DoctorID == doctorSpecialityBO.Doctor.ID && p.IsDeleted == false).ToList <DoctorSpeciality>();

            oldDoctorSpecilities.ForEach(a => { a.IsDeleted = true; a.UpdateDate = DateTime.UtcNow; a.UpdateByUserID = System.Convert.ToInt32(Utility.GetConfigValue("DefaultAdminUserID")); });
            if (oldDoctorSpecilities != null)
            {
                _context.SaveChanges();
            }

            if (doctorSpecialityBO.Specialties.Count() > 0)
            {
                foreach (int item in doctorSpecialityBO.Specialties)
                {
                    doctorDB          = new Doctor();
                    specilityDB       = new Specialty();
                    doctorSpecilityDB = new DoctorSpeciality();

                    #region Doctor
                    doctorSpecilityDB.IsDeleted = doctorSpecialityBO.IsDeleted.HasValue ? doctorSpecialityBO.IsDeleted.Value : false;
                    #endregion
                    //Find existsing record
                    DoctorSpeciality doctor_ = _context.DoctorSpecialities.Where(p => (p.DoctorID == doctorSpecialityBO.Doctor.ID) && (p.SpecialityID == item)).FirstOrDefault <DoctorSpeciality>();
                    if (doctor_ != null)
                    {
                        continue;
                        //return new BO.ErrorObject { ErrorMessage = "Record already exists for this doctor and specility " + item.ToString() + ".", errorObject = "", ErrorLevel = ErrorLevel.Error };
                    }

                    //Find Record By ID
                    Doctor doctor = _context.Doctors.Include("User").Where(p => p.Id == doctorSpecialityBO.Doctor.ID).FirstOrDefault <Doctor>();
                    if (doctor == null)
                    {
                        return new BO.ErrorObject {
                                   ErrorMessage = "Invalid doctor details.", errorObject = "", ErrorLevel = ErrorLevel.Error
                        }
                    }
                    ;

                    doctorSpecilityDB.Doctor     = doctor;
                    _context.Entry(doctor).State = System.Data.Entity.EntityState.Modified;

                    //Find Record By ID
                    Specialty speclity = _context.Specialties.Where(p => p.id == item).FirstOrDefault <Specialty>();
                    if (speclity == null)
                    {
                        return new BO.ErrorObject {
                                   ErrorMessage = "Invalid specility " + item.ToString() + " details.", errorObject = "", ErrorLevel = ErrorLevel.Error
                        }
                    }
                    ;

                    doctorSpecilityDB.Specialty    = speclity;
                    _context.Entry(speclity).State = System.Data.Entity.EntityState.Modified;


                    if (doctorSpecilityDB.id > 0)
                    {
                        //Find Doctor By ID
                        doctor_ = _context.DoctorSpecialities.Where(p => p.id == doctorSpecilityDB.id).FirstOrDefault <DoctorSpeciality>();

                        if (doctor_ != null)
                        {
                            #region Doctor
                            doctor_.id = doctorSpecialityBO.Doctor.ID;
                            doctorSpecilityDB.IsDeleted = doctorSpecialityBO.IsDeleted.HasValue ? doctorSpecialityBO.IsDeleted.Value : false;
                            doctor.UpdateDate           = doctorSpecialityBO.UpdateDate;
                            doctor.UpdateByUserID       = doctorSpecialityBO.UpdateByUserID;
                            #endregion

                            _context.Entry(doctor).State = System.Data.Entity.EntityState.Modified;
                        }
                    }
                    else
                    {
                        doctorSpecilityDB.CreateDate     = doctorSpecialityBO.CreateDate;
                        doctorSpecilityDB.CreateByUserID = doctorSpecialityBO.CreateByUserID;

                        _dbSet.Add(doctorSpecilityDB);
                    }
                }
            }
            _context.SaveChanges();

            var res = Convert <BO.DoctorSpeciality, DoctorSpeciality>(doctorSpecilityDB);
            return((object)res);
        }
        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);
        }