public IActionResult UpdateProfile(int id, [FromBody] ProfileForUpdateDto profile)
        {
            if (profile == null)
            {
                return(BadRequest());
            }

            // check the user exist
            if (!_userRepository.UserExists(id))
            {
                return(NotFound());
            }

            // check the user updating the profile is the current user
            if (Convert.ToInt32(this.User.Identity.Name) != id)
            {
                return(Unauthorized());
            }

            // get current profile from repository
            var profileFromRepo = _userRepository.GetProfileByUserId(id);

            Mapper.Map(profile, profileFromRepo);

            _userRepository.UpdateProfile(profileFromRepo);

            if (!_userRepository.Save())
            {
                throw new Exception($"Updating profile for user with {id} failed on save");
            }

            return(NoContent());
        }
Example #2
0
        //-------------------------------------------------------------------------------------------------

        public Result PutUserProfile(ProfileForUpdateDto newProfile)
        {
            Result res = new Result();

            using (_db = new IdeaManagmentDatabaseEntities())
            {
                var user = _db.USERS.FirstOrDefault(u => u.USERNAME == newProfile.USERNAME);//username is old username and youcan't change it
                if (user == null)
                {
                    res.value   = false;
                    res.content = "کاربر یافت نشد";
                }
                else
                {
                    user.LAST_NAME   = newProfile.LAST_NAME;
                    user.FIRST_NAME  = newProfile.FIRST_NAME;
                    user.EMAIL       = newProfile.EMAIL;
                    user.PASSWORD    = newProfile.PASSWORD;
                    user.MODIFY_DATE = DateTime.Now;
                    _db.SaveChanges();
                    res.value   = true;
                    res.content = "تغییرات اعمال شد";
                }
            }
            return(res);
        }
Example #3
0
        public async Task <IActionResult> Profile(ProfileForUpdateDto profileForUpdateDto)
        {
            if (profileForUpdateDto == null)
            {
                return(BadRequest());
            }

/*             if (!ModelState.IsValid)
 *              return BadRequest(ModelState); */

            var currentUserId   = int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value);
            var profileFromRepo = await _repo.GetUser(currentUserId);

            if (profileFromRepo == null)
            {
                return(NotFound("Could not find user"));
            }

            _mapper.Map(profileForUpdateDto, profileFromRepo);

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

            throw new Exception("Updating profile failed on save");
        }
        public async Task <IActionResult> UpdateProfile(long id, [FromBody] ProfileForUpdateDto profile)
        {
            if (profile == null)
            {
                _logger.LogError("Object cannot be null.");
                return(BadRequest("Object is null."));
            }

            if (profile.Id != id)
            {
                _logger.LogError("Not valid id / object.");
                return(BadRequest("Not valid id / object."));
            }

            if (!ModelState.IsValid)
            {
                _logger.LogError("Object not valid.");
                return(BadRequest(ModelState));
            }

            var profileEntity = await _repository.Profile.GetProfileByIdAsync(id);

            if (profileEntity == null)
            {
                _logger.LogError($"Object with id: {id} has not been found.");
                return(NotFound($"Object with id: {id} has not been found."));
            }

            _mapper.Map(profile, profileEntity);

            _repository.Profile.UpdateProfile(profileEntity);
            await _repository.SaveAsync();

            return(NoContent());
        }
        public HttpResponseMessage PutUserProfile([FromBody] ProfileForUpdateDto newProfile)
        {
            var    queryGenerator = new Business.User();
            Result res            = queryGenerator.PutUserProfile(newProfile);

            if (res.Value == true)
            {
                return(Request.CreateResponse(HttpStatusCode.OK, res.Content));
            }
            else
            {
                return(Request.CreateResponse(HttpStatusCode.PreconditionFailed, res.Content));
            }
        }
        public async Task <IActionResult> UpdateProfile(int id, ProfileForUpdateDto profileForUpdateDto)
        {
            try
            {
                if (id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
                {
                    return(Unauthorized());
                }

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

                _mapper.Map(profileForUpdateDto, userFromRepo);

                _repo.User.UpdateUser(userFromRepo);
                await _repo.User.SaveAllAsync();

                return(NoContent());
            }
            catch (Exception ex)
            {
                _logger.LogError($"Something went wrong inside UpdateProfile endpoint: {ex.Message}");
                return(StatusCode(500, "Internal server error: " + ex.Message));
            }
        }
        //-------------------------------------------------------------------------------------------------

        public Result PutUserProfile(ProfileForUpdateDto newProfile)
        {
            return(_Repository.PutUserProfile(newProfile));
        }