Ejemplo n.º 1
0
        public async Task <IActionResult> Register([FromBody] RegisterViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            var user = new BarberUser()
            {
                UserName       = string.Concat(model.FirstName, model.LastName),
                FirstName      = model.FirstName,
                LastName       = model.LastName,
                CreateDateTime = DateTime.UtcNow,
                CreateUser     = string.Concat(model.FirstName, model.LastName),
                Email          = model.Email
            };
            var result = await _userManager.CreateAsync(user, model.Password);

            if (result.Succeeded)
            {
                return(Ok(result));
            }

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

            return(BadRequest(result.Errors));
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> Modify([FromBody] ModifyUserViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            var user = new BarberUser()
            {
                UserName         = string.Concat(model.FirstName, model.LastName),
                FirstName        = model.FirstName,
                LastName         = model.LastName,
                ModifiedDateTime = DateTime.UtcNow,
                ModifiedBy       = model.User,
                Email            = model.Email,
                Address          = model.Address,
                DOB          = model.DOB,
                PasswordHash = HashPassword(model.Password)
            };


            var result = await _userManager.UpdateAsync(user);

            if (result.Succeeded)
            {
                return(Ok(result));
            }

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

            return(Ok(new
            {
                user = _mapper.Map <BarberUser, UserViewModel>(user)
            }));
        }