Example #1
0
        public async Task <Models.User> UpdateUser(ViewModels.UserProfile user, int userId)
        {
            Models.User existingUser = await _context.Users.SingleOrDefaultAsync(u => u.ID == userId);

            if (!String.IsNullOrEmpty(user.Login))
            {
                existingUser.Login = user.Login;
            }
            if (!String.IsNullOrEmpty(user.Email))
            {
                existingUser.Email = user.Email;
            }
            if (!String.IsNullOrEmpty(user.Password))
            {
                existingUser.Password = new PasswordHasher <Models.User>()
                                        .HashPassword(existingUser, user.Password);
            }

            //_context.Entry(existingUser).CurrentValues.SetValues(user);

            EntityEntry <Models.User> updatedUser = _context.Users.Update(existingUser);
            await _context.SaveChangesAsync();

            return(updatedUser.Entity);
        }
Example #2
0
        public async Task <IActionResult> PutUser([FromRoute] int id, [FromBody] ViewModels.UserProfile user)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            if (!(int.Parse(User?.FindFirst("userId")?.Value)).Equals(id))
            {
                return(Unauthorized());
            }

            Models.User updatedUser = await _userService.UpdateUser(user, id);

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

            return(Ok(new ViewModels.User
            {
                ID = updatedUser.ID,
                Email = updatedUser.Email,
                Login = updatedUser.Login
            }));
            //_context.Entry(user).State = EntityState.Modified;

            //try
            //{
            //    await _context.SaveChangesAsync();
            //}
            //catch (DbUpdateConcurrencyException)
            //{
            //    if (!UserExists(id))
            //    {
            //        return NotFound();
            //    }
            //    else
            //    {
            //        throw;
            //    }
            //}

            //return NoContent();
        }