public async Task <IActionResult> Create(CreateCustomerView model)
        {
            if (ModelState.IsValid)
            {
                ApplicationUser user = new ApplicationUser
                {
                    Email      = model.Email,
                    UserName   = model.FirstName + model.SecondName,
                    CreateDate = DateTime.Now,
                    UpdateTime = DateTime.Now
                };

                Customer customer = new Customer
                {
                    FirstName  = model.FirstName,
                    SecondName = model.SecondName,
                    Phone      = model.Phone,
                    Email      = model.Email
                };

                if (userManager.Users.Where(u => u.Email == model.Email).Count() > 0)
                {
                    var configuration = new ConfigurationManager();
                    var section       = configuration.Configuration.GetSection("ErrorMessages");

                    ViewBag.Message = section["EmailExistAlready"];

                    return(View("ErrorPage"));
                }

                var addResult = await userManager.CreateAsync(user, model.Password);

                if (addResult.Succeeded)
                {
                    if (await roleManager.FindByNameAsync("customer") == null)
                    {
                        await roleManager.CreateAsync(new IdentityRole("customer"));
                    }

                    await userManager.AddToRoleAsync(user, "customer");

                    await unitOfWork.Customers.Create(customer);

                    await unitOfWork.SaveAsync();

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

            return(View(model));
        }
Example #2
0
 /// <summary>
 /// Shows the create customer view and updates the view
 /// </summary>
 private void createCustomer( )
 {
     try
     {
         CreateCustomerView ccv = new CreateCustomerView();
         ccv.ShowDialog();
         updateView();
     }
     catch (Exception ex)
     {
         ExceptionHelper.Handle(ex);
     }
 }
Example #3
0
        public async Task <IActionResult> Create(CreateCustomerView model)
        {
            if (ModelState.IsValid)
            {
                ApplicationUser user = new ApplicationUser
                {
                    Email      = model.Email,
                    UserName   = model.FirstName + model.SecondName,
                    CreateDate = DateTime.Now,
                    UpdateTime = DateTime.Now
                };

                Psychologist customer = new Psychologist
                {
                    Name        = model.FirstName,
                    Surname     = model.SecondName,
                    PhoneNumber = model.Phone,
                    Email       = model.Email,
                    BirthDate   = model.BirthDate
                };

                if (userManager.Users.Where(u => u.Email == model.Email).Count() > 0)
                {
                    ViewBag.Message = "User with that email already exist";

                    return(View("ErrorPage"));
                }

                var addResult = await userManager.CreateAsync(user, model.Password);

                if (addResult.Succeeded)
                {
                    await userManager.AddToRoleAsync(user, "customer");

                    await unitOfWork.Psychologists.Create(customer);

                    await unitOfWork.SaveAsync();

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

            return(View(model));
        }