Exemplo n.º 1
0
        public async Task <ActionResult> Update(int?id, [FromBody] UpdateProfileResource updateProfile)
        {
            if (id == null)
            {
                return(NotFound(new { message = "User Not Found Bro" }));
            }

            var dbUser = _context.Users.FirstOrDefault(x => x.Id == id);

            if (updateProfile.Email != dbUser.Email)
            {
                if (_context.Users.Any(x => x.Email == updateProfile.Email))
                {
                    return(Conflict(new
                    {
                        message = "This Email Alredy Exists"
                    }));
                }
            }

            dbUser.Name    = updateProfile.Name;
            dbUser.Surname = updateProfile.Surname;
            dbUser.Email   = updateProfile.Email;

            var userResource = _mapper.Map <User, UserResource>(dbUser);

            await _context.SaveChangesAsync();

            return(Ok(userResource));
        }
        public async Task <IActionResult> Update([FromBody] UpdateProfileResource userResource)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var user = await _userManager.FindByEmailAsync(userResource.Email);

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

            _mapper.Map <UpdateProfileResource, ApplicationUser>(userResource, user);

            await _unitOfWork.CompleteAsync();

            user = await _userManager.Users
                   .Include(u => u.Role)
                   .Include(u => u.Language)
                   .SingleOrDefaultAsync(u => u.NormalizedEmail == userResource.Email.ToUpper());

            var updatedUserResource = _mapper.Map <UserResource>(userResource);

            updatedUserResource = _mapper.Map <ApplicationUser, UserResource>(user, updatedUserResource);

            return(Ok(updatedUserResource));
        }
Exemplo n.º 3
0
        public async Task <IActionResult> UpdateProfileAsync([FromBody] UpdateProfileResource updateProfileResource)
        {
            try
            {
                var user = _mapper.Map <UpdateProfileResource, User>(updateProfileResource);
                user.Id       = _userDetails.Id;
                user.UserRole = _userDetails.UserRole;
                var result = await _userService.UpdateProfile(user);

                if (!result.Success)
                {
                    return(BadRequest(new FailureResponse(result.Message)));
                }
                var userDeatils = _mapper.Map <User, UserDetails>(result.Resource);
                return(Ok(new CResponse <UserDetails>(result.Success, userDeatils)));
            }
            catch (Exception ex)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError, new FailureResponse($"Internal Server Error: {ex.Message}")));
            }
        }