public IActionResult CreateCustomer([FromBody] CustomerCreateModel customer)
        {
            if (customer == null || !ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            var newCustomer = new CustomerDataTransferObject
            {
                Name     = customer.Name,
                Adress   = customer.Adress,
                Code     = customer.Code,
                Discount = customer.Discount
            };

            var newCustomerId = _customerService.CreateCustomer(newCustomer);

            var customerUser = new CustomerInfoDataTransferObject
            {
                UserName   = customer.Email,
                Email      = customer.Email,
                CustomerId = newCustomerId
            };

            _customerService.AddCustomerUser(_userManager, customerUser, customer.Password).Wait();

            return(CreatedAtRoute("GetCustomer", new { id = newCustomerId },
                                  new CustomerGetModel
            {
                Id = newCustomerId,
                Name = newCustomer.Name,
                Code = newCustomer.Code,
                Adress = newCustomer.Adress,
                Discount = newCustomer.Discount
            }));
        }
Пример #2
0
        public async Task <IActionResult> CreateUser(RegisterUserViewModel registerModel, int customerId)
        {
            var departments = _departmentService.GetDepartments();

            if (ModelState.IsValid)
            {
                var  registerDepartment = departments.First(e => e.Id == registerModel.DepartmentId);
                User user = new User
                {
                    UserName   = registerModel.UserName,
                    Department = registerDepartment,
                    Email      = registerModel.Email,
                    Mobile     = registerModel.Mobile,
                    Name       = registerModel.UserName
                };
                var result = await _userDbService.AddUser(user, registerModel.Password);

                var updatedUserInfo = await _userManager.FindByNameAsync(registerModel.UserName);

                _customerService.AddCustomerUser(customerId, updatedUserInfo);


                foreach (var error in result.Errors)
                {
                    ModelState.AddModelError("", error.Description);
                }

                if (result.Succeeded)
                {
                    return(RedirectToAction("index", "userinfo", new { customerId = customerId }));
                }
            }

            ViewBag.DepartmentsList = new SelectList(departments.ToList(), "Id", "Name");
            return(View("~/Views/Admin/UserInfoPage/CreateUser.cshtml", registerModel));
        }