Exemple #1
0
        public void UpdateUser()
        {
            //-- arrange
            var user = new User
            {
                FirstName    = "fname",
                LastName     = "lname",
                EmailAddress = "old@email",
                Password     = "******"
            };

            _repository.AddUser(user);
            _repository.Save();

            user.EmailAddress = "new@email";

            //-- act
            _repository.UpdateUser(user);
            _repository.Save();

            var actual = _repository.GetUser(1);

            //-- assert
            Assert.AreEqual("new@email", actual.EmailAddress);

            //-- cleanup
            _repository.DeleteUser(user);
            _repository.Save();
        }
Exemple #2
0
        public IActionResult UpdateUser(int userId,
                                        [FromBody] UserForUpdateDto user)
        {
            var emailAddress = user.Credentials.EmailAddress;
            var password     = user.Credentials.Password;

            if (!_weblogDataRepository.Authorized(userId, emailAddress, password))
            {
                return(Unauthorized());
            }

            var userFromRepo = _weblogDataRepository.GetUser(userId);

            if (userFromRepo is null)
            {
                return(NotFound());
            }

            _mapper.Map(user, userFromRepo);

            _weblogDataRepository.UpdateUser(userFromRepo);

            try
            {
                _weblogDataRepository.Save();
            }
            catch (ApplicationException ex)
            {
                // changing user with email address that already exists
                ModelState.AddModelError(nameof(user.EmailAddress),
                                         ex.Message + "\n" + ex?.InnerException.Message);

                return(ErrorHandler.UnprocessableEntity(ModelState, HttpContext));
            }

            return(NoContent());
        }