public void UpdateDoctorBasic(DoctorBasic doctor, string userName)
 {
     doctor.DateUpdated       = DateTime.Now;
     doctor.UpdatedByUserName = userName;
     _repository.Update(doctor);
 }
 public void DeleteDoctorBasic(DoctorBasic doctor)
 {
     _repository.Delete(doctor);
 }
        public async Task <ActionResult> CreateDoctorsAsync([FromBody] List <DoctorBasicCM> doctorCMs)
        {
            string _             = (await _userManager.GetUserAsync(User)).UserName;
            var    createdDoctor = new List <MyUser>();

            try
            {
                //asp.net user_ create list doctors
                foreach (var doctorCM in doctorCMs)
                {
                    MyUser user = doctorCM.Adapt <MyUser>();
                    user.UserName    = doctorCM.HisCode; //Username sẽ là HisCode
                    user.IsActive    = false;
                    user.DateCreated = DateTime.Now;

                    //Create doctor
                    var currentUser = await _userManager.CreateAsync(user, DefaultPassword.PasswordDoctor);

                    //Create success
                    if (currentUser.Succeeded)
                    {
                        if (!(await _userManager.AddToRoleAsync(user, nameof(UserRoles.Doctor))).Succeeded)
                        {
                            return(BadRequest(user + "\n Add role fail!"));
                        }

                        //create doctor basic
                        DoctorBasic doctorBasic = doctorCM.Adapt <DoctorBasic>();
                        doctorBasic.Id = user.Id;

                        //create info doctor pro
                        doctorBasic.DoctorPro    = doctorCM.Adapt <DoctorPro>();
                        doctorBasic.DoctorPro.Id = user.Id;
                        //create Speciality
                        var specialities = new List <SpecialityDoctor>();
                        foreach (var item in doctorCM.SpecialityIds.ToList())
                        {
                            specialities.Add(new SpecialityDoctor()
                            {
                                SpecialityId  = item,
                                DoctorBasicId = user.Id
                            });
                        }
                        doctorBasic.SpecialityDoctors = specialities;

                        _doctorBasicService.CreateDoctorBasic(doctorBasic, _);
                        createdDoctor.Add(user);
                    }
                    else //Create fail
                    {
                        foreach (var item in createdDoctor)
                        {
                            _userManager.DeleteAsync(item);
                        }
                        return(BadRequest(currentUser.Errors));
                    }
                }
                _doctorBasicService.Save();
                return(Ok());
            }
            catch (Exception e)
            {
                foreach (var item in createdDoctor)
                {
                    _userManager.DeleteAsync(item);
                }
                return(BadRequest(e.Message));
            }
        }
 public void CreateDoctorBasic(DoctorBasic doctor, string userName)
 {
     doctor.DateCreated       = DateTime.Now;
     doctor.CreatedByUserName = userName;
     _repository.Add(doctor);
 }
        public async Task <ActionResult> CreateDoctorAsync([FromBody] DoctorBasicCM doctorCM)
        {
            MyUser user = null;

            try
            {
                //asp.net user_ create account
                user             = doctorCM.Adapt <MyUser>();
                user.IsActive    = false;
                user.DateCreated = DateTime.Now;
                user.UserName    = doctorCM.HisCode;
                var currentUser = await _userManager.CreateAsync(user, DefaultPassword.PasswordDoctor);

                if (currentUser.Succeeded)
                {
                    if (!(await _userManager.AddToRoleAsync(user, nameof(UserRoles.Doctor))).Succeeded)
                    {
                        _userManager.DeleteAsync(user);
                        return(BadRequest(user + "\n Add role fail!"));
                    }

                    //create doctor basic
                    DoctorBasic doctorBasic = doctorCM.Adapt <DoctorBasic>();
                    doctorBasic.Id = user.Id;
                    //create info doctor pro
                    DoctorPro doctorPro = doctorCM.Adapt <DoctorPro>();
                    doctorPro.Id = user.Id;
                    //create Speciality
                    var specialities = new List <SpecialityDoctor>();
                    foreach (var item in doctorCM.SpecialityIds.ToList())
                    {
                        specialities.Add(new SpecialityDoctor()
                        {
                            SpecialityId  = item,
                            DoctorBasicId = user.Id
                        });
                    }
                    doctorBasic.SpecialityDoctors = specialities;

                    //Auto create doctorPro when insert doctorBasic
                    doctorBasic.DoctorPro = doctorPro;

                    _doctorBasicService.CreateDoctorBasic(doctorBasic, (await _userManager.GetUserAsync(User)).UserName);
                }
                else
                {
                    if (user != null)
                    {
                        _userManager.DeleteAsync(user);
                    }
                    return(BadRequest(currentUser.Errors));
                }
                _doctorBasicService.Save();
                return(StatusCode(201));
            }
            catch (Exception e)
            {
                if (user != null)
                {
                    _userManager.DeleteAsync(user);
                }
                return(BadRequest(e.Message));
            }
        }