Ejemplo n.º 1
0
        public async Task <IActionResult> Edit(int id)
        {
            Psychologist customer = await unitOfWork.Psychologists.Get(id);

            ApplicationUser user = await userManager.FindByEmailAsync(customer.Email);

            var userRoles = await userManager.GetRolesAsync(user);

            var allRoles = roleManager.Roles.ToList();

            if (customer == null)
            {
                return(NotFound());
            }

            var model = new EditPsychologistView
            {
                Id         = customer.PsychologistId,
                FirstName  = customer.Name,
                SecondName = customer.Surname,
                Phone      = customer.PhoneNumber,
                Email      = customer.Email,
                AllRoles   = allRoles,
                UserRoles  = userRoles
            };

            return(View(model));
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> Edit(EditPsychologistView model, List <string> roles)
        {
            if (ModelState.IsValid)
            {
                Psychologist psychologist = await unitOfWork.Psychologists.Get(model.Id);

                ApplicationUser user = await userManager.FindByEmailAsync(psychologist.Email);

                if (user != null && psychologist != null)
                {
                    if (roles.Contains("manager") && model.UnMakePsychologist)
                    {
                        roles.Remove("manager");
                    }
                    user.Email      = model.Email;
                    user.UserName   = model.Email;
                    user.UpdateTime = DateTime.Now;

                    var userRoles = await userManager.GetRolesAsync(user);

                    var allRoles = roleManager.Roles.ToList();

                    var addedRoles   = roles.Except(userRoles);
                    var removedRoles = userRoles.Except(roles);

                    await userManager.AddToRolesAsync(user, addedRoles);

                    await userManager.RemoveFromRolesAsync(user, removedRoles);

                    if (model.UnMakePsychologist)
                    {
                        var customer = new Customer
                        {
                            Name        = model.FirstName,
                            Surname     = model.SecondName,
                            BirthDate   = psychologist.BirthDate,
                            Email       = model.Email,
                            PhoneNumber = model.Phone
                        };

                        await unitOfWork.Customers.Create(customer);

                        await unitOfWork.Psychologists.Delete(psychologist.PsychologistId);

                        await unitOfWork.SaveAsync();
                    }
                    else
                    {
                        psychologist.Name        = model.FirstName;
                        psychologist.Surname     = model.SecondName;
                        psychologist.PhoneNumber = model.Phone;
                        psychologist.Email       = model.Email;

                        unitOfWork.Psychologists.Update(psychologist);
                        await unitOfWork.SaveAsync();

                        var result = await userManager.UpdateAsync(user);

                        if (result.Succeeded)
                        {
                            return(RedirectToAction("Index"));
                        }
                        else
                        {
                            foreach (var error in result.Errors)
                            {
                                ModelState.AddModelError(string.Empty, error.Description);
                            }
                        }
                    }
                }
            }

            return(RedirectToAction("Index", "Psychologist"));
        }