public async Task <IActionResult> UpdateUser(int id, UserForUpdateDTO userForUpdateDTO)
        {
            // if the current user that is passed to our server matches the id
            // FindFirst - Retrieves the first claim with the specified claim type.
            // the ClaimType is connected to the ones declared in AuthController
            // https://docs.microsoft.com/en-us/dotnet/api/system.security.claims.claimsidentity.findfirst?view=netframework-4.7.2#System_Security_Claims_ClaimsIdentity_FindFirst_System_String_
            if (id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            var userFromRepo = await _datingRepo.GetUser(id, true);

            _mapper.Map(userForUpdateDTO, userFromRepo);

            // if save is successful then return no content
            if (await _datingRepo.SaveAll())
            {
                return(NoContent());
            }

            // $ - string interpolation - i.e. instead of the usual '{0}...id we do in string formatting,
            // we can directly interpolate id as below
            // https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/tokens/interpolated
            throw new Exception($"Updating user {id} failed on save");
        }
        public async Task <IActionResult> UpdateUser(int id, [FromBody] UserForUpdateDTO userForUpdate)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var curentUserId = int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value);

            var userFromRepo = await _repo.GetUser(id);

            if (userFromRepo == null)
            {
                return(NotFound("Nie znaleziono usera o takim ID"));
            }

            if (curentUserId != userFromRepo.Id)
            {
                return(Unauthorized());
            }

            _mapper.Map(userForUpdate, userFromRepo);

            if (await _repo.SaveAll())
            {
                return(NoContent());
            }


            throw new Exception("Update Failed");
        }
Example #3
0
        public string UpdateUser(UserForUpdateDTO user, int id)
        {
            if (user == null)
            {
                throw new ArgumentNullException(nameof(user));
            }

            var result = string.Empty;

            try
            {
                var userData = this.dataContext.Users.FirstOrDefault(x => x.Id == id);
                userData.Interests    = user.Interests;
                userData.Country      = user.Country;
                userData.Introduction = user.Introduction;
                userData.LookingFor   = user.LookingFor;


                userData.City = user.City;
                this.dataContext.SaveChanges();
            }
            catch (Exception e)
            {
                result = string.Format("Update User - {0} , {1}", e.Message,
                                       e.InnerException != null ? e.InnerException.Message : "");
            }

            //registrationId = newRecord.RegistrationId;
            return(result);;
        }
Example #4
0
        public async Task <IActionResult> UpdateUser(int id, UserForUpdateDTO user)
        {
            //verificando se o ID informado é o mesmo do ID do corpo
            if (id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized("Usuário diferente do ID fornecido no token"));
            }



            var userFromRepo = await repository.GetUser(id);

            Console.WriteLine("antes do mapping");
            Console.WriteLine(userFromRepo.Username);

            mapper.Map(user, userFromRepo);

            Console.WriteLine("depois do mapping");
            Console.WriteLine(userFromRepo.Username);

            if (await repository.SaveAll())
            {
                return(NoContent());
            }

            throw new System.Exception($"Falha ao atualizar o Usuário com ID {id}");
        }
        public async Task <IActionResult> UpdateAccount(int id, UserForUpdateDTO userForUpdateDto)
        {
            if (id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            var userFromRepo = await _adminService.GetUser(id);

            _mapper.Map(userForUpdateDto, userFromRepo);

            var result = await _userManager.UpdateAsync(userFromRepo);

            if (result.Succeeded)
            {
                result = await _userManager.ChangePasswordAsync(userFromRepo, userForUpdateDto.OldPassword, userForUpdateDto.Password);

                if (result.Succeeded)
                {
                    return(NoContent());
                }
                else
                {
                    throw new Exception($"Updating user {id} failed on save!");
                }
            }
            else
            {
                throw new Exception($"Updating user {id} failed on save!");
            }
        }
Example #6
0
        public async Task <IActionResult> UpdateUser(int id, UserForUpdateDTO model)
        {
            if (id == 0 || !ModelState.IsValid)
            {
                return(BadRequest());
            }
            var user = await _repository.GetUser(id);

            if (user == null)
            {
                return(NotFound());
            }
            user.Name     = model.Name;
            user.Surname  = model.Surname;
            user.UserName = model.UserName;
            user.Email    = model.Email;
            user.Phone    = model.Phone;
            user.BirthDay = model.BirthDay;
            user.Address  = model.Address;
            var result = await _repository.SaveChanges();

            if (result)
            {
                return(StatusCode(201));
            }
            return(BadRequest());
        }
Example #7
0
        public IActionResult UpdateUser(Guid id, [FromBody] UserForUpdateDTO users)
        {
            if (User == null)
            {
                _logger.LogError("UserForUpdateDTO object sent from client is null.");
                return(BadRequest("UserForUpdateDTO object is null"));
            }
            if (!ModelState.IsValid)
            {
                _logger.LogError("Invalid model state for the UserForUpdateDTO object");
                return(UnprocessableEntity(ModelState));
            }
            var userEntity = _repository.User.GetUser(id, trackChanges: true);

            if (userEntity == null)
            {
                _logger.LogInfo($"Users with id: {id} doesn't exist in the database.");
                return(NotFound());
            }

            _mapper.Map(User, userEntity);
            _repository.Save();

            return(NoContent());
        }
Example #8
0
        public async Task <IActionResult> UpdateUser(int id, [FromBody] UserForUpdateDTO userForUpdateDTO)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var currentUserId = int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value);

            var userFromRepo = await _repo.GetUser(id);

            if (userFromRepo == null)
            {
                return(NotFound($"Could not find user with an ID of {id}"));
            }

            if (currentUserId != userFromRepo.Id)
            {
                return(Unauthorized());
            }

            _mapper.Map(userForUpdateDTO, userFromRepo);

            if (await _repo.SaveAll())
            {
                return(NoContent());
            }

            throw new Exception($"Updating user {id} failed on save!");
        }
        public async Task <IActionResult> UpdateUser(int id, UserForUpdateDTO userForUpdateDTO)
        {
            var userFromRepo = await _repo.GetUser(id);

            _mapper.Map(userForUpdateDTO, userFromRepo);
            if (await _repo.SaveAll())
            {
                return(Ok());
            }
            throw new Exception($"حدثت مشكلة في تعديل بيانات التاجر رقم {id}");
        }
Example #10
0
        public async Task <Result> UpdateAsync(int id, UserForUpdateDTO model)
        {
            var user = await this.data.Users
                       .Where(u => u.Id == id)
                       .FirstOrDefaultAsync();

            if (user == null)
            {
                return("Потребителят не е намерен");
            }

            this.mapper.Map(model, user);

            return(true);
        }
Example #11
0
        public ActionResult UpdateUser(int id, [FromBody] UserForUpdateDTO userForUpdateDTO)
        {
            if (id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            var user = _userService.GetUser(id);

            user = _mapper.Map(userForUpdateDTO, user);

            _userService.UpdateUser();

            return(NoContent());
        }
Example #12
0
        public async Task <IActionResult> UpdateUser(int id, UserForUpdateDTO userForUpdateDTO)
        {
            if (id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }
            var userFromRepo = await _repo.GetUser(id);

            _mapper.Map(userForUpdateDTO, userFromRepo);
            if (await _repo.SaveAll())
            {
                return(NoContent());
            }
            throw new Exception($"Updating user {id} failed on save");
        }
Example #13
0
        public async Task <IActionResult> UpdateUser(int id, UserForUpdateDTO userForUpdateDTO)
        {
            if (id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            var user = this.IdatingService.UpdateUser(userForUpdateDTO, id);

            if (user != null)
            {
                return(Ok(user));
            }

            return(Ok());
        }
Example #14
0
        public async Task <IActionResult> UpdateUser(long id, UserForUpdateDTO userForUpdateDTO)
        {
            if (id != long.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(StatusCode(StatusCodes.Status401Unauthorized));
            }

            var user = await _datingRepository.GetUser(id);

            _mapper.Map(userForUpdateDTO, user);
            if (await _datingRepository.SaveAll())
            {
                return(StatusCode(StatusCodes.Status204NoContent));
            }

            throw new System.Exception($"Updating user {id} failed on save");
        }
Example #15
0
        public async Task <IActionResult> UpdateUser(int id, UserForUpdateDTO userForUpdateDTO)
        {
            // if the id of user to update does not match the current user's id encrypted in the token,
            // it means he should not be able to update other user's profile
            if (id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }
            var userToUpdate = await _datingRepository.GetUserById(id);

            _mapper.Map(userForUpdateDTO, userToUpdate);
            if (await _datingRepository.SaveAll())
            {
                return(NoContent());
            }
            throw new Exception("Updating user failed on save");
        }
Example #16
0
        public async Task <IActionResult> UpdateUser(int id, UserForUpdateDTO userForUpdateDTO)
        {
            if (id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            var userFromRepo = await _userRepository.GetUser(id);

            _mapper.Map(userForUpdateDTO, userFromRepo);

            if (await _userRepository.SaveAll())
            {
                return(NoContent());
            }
            throw new Exception($"Update (id:{id}) failed. Database error!");
        }
Example #17
0
        public async Task <IActionResult> UpdateUser(int id, UserForUpdateDTO userForUpdateDTO)
        {
            if (IsAuthenticated(id))
            {
                return(Unauthorized());
            }

            var userFromRepo = await _repo.GetById(id);

            _mapper.Map(userForUpdateDTO, userFromRepo);

            if (await _repo.SaveAll())
            {
                return(NoContent());
            }

            throw new Exception($"Updatind user {id} failed");
        }
        public async Task <IActionResult> UpdateUser(int id, UserForUpdateDTO userForUpdateDTO)
        {
            if (id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            var userfromdb = await _repository.GetUser(id);

            _mapper.Map(userForUpdateDTO, userfromdb);

            if (await _repository.SaveAll())
            {
                return(NoContent());
            }

            throw new Exception($"An Error Occured while updating the entries for User {id}");
        }
        public async Task <IActionResult> UpdateUser(Guid UserUniqueIdentity, UserForUpdateDTO updateUser)
        {
            var claim = User.FindFirst(ClaimTypes.NameIdentifier).Value;

            if (UserUniqueIdentity.ToString() != claim)
            {
                return(Unauthorized("Invalid user Access"));
            }

            var userFromRepo = await _repo.GetUser(UserUniqueIdentity);

            _mapper.Map(updateUser, userFromRepo);
            if (await _repo.SaveAll())
            {
                return(NoContent());
            }
            return(StatusCode(403));
        }
        public async Task <IActionResult> UpdateUser(int id, UserForUpdateDTO userForUpdateDTO)
        {
            if (id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) // Get Id From Token
            {
                return(Unauthorized());
            }

            var userFromRepo = await _datingRepository.GetUser(id);

            _mapper.Map(userForUpdateDTO, userFromRepo);

            if (await _datingRepository.SaveAll())
            {
                return(NoContent());
            }

            return(BadRequest("Fallo en la actualización"));
        }
        public async Task <IActionResult> UpdateUser(int id, UserForUpdateDTO userForUpdate)
        {
            if (id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            var userRepo = await _repo.GetUser(id);

            var userReturn = _mapper.Map(userForUpdate, userRepo);

            if (await _repo.SaveAll())
            {
                return(NoContent());
            }

            throw new Exception($"Alterando usuário id {id} falhou ao salvar");
        }
Example #22
0
        public async Task <ActionResult> UpdateUser(int id, UserForUpdateDTO model)
        {
            if (id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }
            var userFromRepo = await _repo.GetUser(id);

            _mapper.Map(model, userFromRepo);

            if (await _repo.SaveAll())
            {
                return(NoContent());
            }
            else
            {
                return(BadRequest($"Updating user {id} failed on save"));
            }
        }
        public async Task <IActionResult> UpdateUser(int userId, [FromBody] UserForUpdateDTO userForUpdate)
        {
            // Check if the user is the current user that is passing this token
            if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            var user = await _repository.GetUser(userId, true);

            _mapper.Map(userForUpdate, user);

            if (await _repository.SaveAll())
            {
                return(NoContent());
            }

            throw new Exception($"Updating user {userId} failed on save");
        }
Example #24
0
        public async Task <IActionResult> UpdateUser(int id, UserForUpdateDTO userForUpdateDTO)
        {
            if (id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            var userFromRepo = await this._repo.GetUser(id);

            _mapper.Map(userForUpdateDTO, userFromRepo);

            // if all right
            if (await this._repo.SaveAll())
            {
                return(NoContent());
            }

            return(BadRequest(new Error(HttpStatusCode.BadRequest.ToString(), "Some problem updating user {UserForUpdateDTO.id}!")));
        }
Example #25
0
        public async Task <IActionResult> UpdateUser(int id, UserForUpdateDTO userForUpdateDTO)
        {
            //
            if (id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            var userFromRepo = await _dateRepository.GetUser(id);

            _mapper.Map(userForUpdateDTO, userFromRepo);

            if (await _dateRepository.SaveAll())
            {
                return(NoContent());
            }

            throw new Exception($"Error while updating user with id: {id}");
        }
Example #26
0
        public async Task <ActionResult> UpdateUser(int id, UserForUpdateDTO userForUpdateDTO)
        {
            //if now user id != modify user id ==> no right to change
            if (id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            var userFromRep = await _datingRepository.GetUser(id);

            _mapper.Map(userForUpdateDTO, userFromRep);

            if (await _datingRepository.SaveAll())
            {
                return(NoContent());
            }

            throw new Exception($"User {id} is failed on Save");
        }
        public async Task <IActionResult> UpdateUser(string id, [FromBody] UserForUpdateDTO user)
        {
            if (user == null)
            {
                return(BadRequest());
            }

            if (!ModelState.IsValid)
            {
                return(new UnprocessableEntityObjectResult(ModelState));
            }

            var userFromRepo = _userRepository.GetById(id);

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

            Mapper.Map(user, userFromRepo);

            var operation = await _userRepository.UpdateUser(userFromRepo);

            if (!operation.Succeeded)
            {
                throw new Exception($"Updating user failed. {operation.Errors.FirstOrDefault().Description}");
            }

            var currentUserRoles = _userRepository.GetRolesForUser(userFromRepo);
            var rolesToRemove    = currentUserRoles.Except(user.UserRoles.Select(ur => ur.Name));
            var rolesToAdd       = user.UserRoles.Select(ur => ur.Name).Except(currentUserRoles);
            var addOperation     = await _userRepository.AddRolesAsync(userFromRepo, rolesToAdd.ToList());

            var removeOperation = await _userRepository.RemoveRolesAsync(userFromRepo, rolesToRemove.ToList());

            if (!addOperation.Succeeded || !removeOperation.Succeeded)
            {
                throw new Exception($"Updating user failed. {addOperation.Errors.FirstOrDefault().Description} {removeOperation.Errors.FirstOrDefault().Description}");
            }

            return(NoContent());
        }
Example #28
0
        public async Task <IActionResult> UpdateUser(int id, UserForUpdateDTO updateDTo)
        {
            // checks if ID matches token id
            if (id != int.Parse(User.FindFirstValue(ClaimTypes.NameIdentifier)))
            {
                return(Unauthorized());
            }

            var userFromRepo = await _repo.GetUser(id);

            // updates values from Dto and writes them to user
            _mapper.Map(updateDTo, userFromRepo);

            if (await _repo.SaveAll())
            {
                return(NoContent());
            }

            throw new Exception($"Updating user {id} failed on save");
        }
        public async Task <IActionResult> UpdateUser(int id, UserForUpdateDTO model)
        {
            if (id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(BadRequest("Not valid request"));
            }
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            var user = await _repository.GetUser(id);

            _mapper.Map(model, user);

            if (await _repository.SaveChanges())
            {
                return(Ok());
            }
            throw new System.Exception("an error occurred during the update!");
        }
Example #30
0
        public async Task <IActionResult> UpdateUser(int id, UserForUpdateDTO userFromModel)
        {
            if (!ValidateAuthenticationUserId(id))
            {
                return(Unauthorized());
            }

            var userFromRepo = await _repo.GetUser(id);

            _mapper.Map(userFromModel, userFromRepo);

            if (await _repo.SaveAll())
            {
                return(NoContent());
            }
            else
            {
                throw new Exception($"Updating user {id} failed on save");
            }
        }