Пример #1
0
 public void UpdateMainUserInfo(UserMainInfoDto user, string email)
 {
     try
     {
         var existing = _unitOfWork.Users.GetById(id: user.Id);
         if (existing == null)
         {
             throw new ArgumentException("User does not exists");
         }
         if (existing.Email != email)
         {
             throw new ArgumentException("Operation not allowed");
         }
         user.UpdateEntity(existing);
         _activityService.LogActivity(new Activity
         {
             UserId     = existing.Id,
             Importance = ActivityImportance.Medium,
             Type       = ActivityType.UserUpdate,
             Data       = JsonConvert.SerializeObject(existing)
         });
         _unitOfWork.Users.Update(existing);
         _unitOfWork.Save();
     }
     catch (ArgumentException)
     {
         throw;
     }
     catch (Exception e)
     {
         _logger.LogError(e.Message);
         throw new Exception("Error while updating user info. Please try a bit later");
     }
 }
Пример #2
0
 public static void UpdateEntity(this UserMainInfoDto userDto, User user)
 {
     user.Facebook     = userDto.FirstName;
     user.LastName     = userDto.LastName;
     user.Nickname     = userDto.Nickname;
     user.DateOfBirth  = userDto.DateOfBirth;
     user.LastActivity = DateTimeOffset.UtcNow;
 }
Пример #3
0
 public IActionResult UpdateMainUserInfo([FromBody] UserMainInfoDto userDto)
 {
     try
     {
         var email = User.Claims.SingleOrDefault(c => c.Type == "email")?.Value;
         if (string.IsNullOrEmpty(email))
         {
             return(BadRequest("Authorization failed."));
         }
         _accountService.UpdateMainUserInfo(userDto, email);
         return(Ok());
     }
     catch (Exception e)
     {
         return(BadRequest(e.Message));
     }
 }
Пример #4
0
        public async Task <IActionResult> UpdateUser(int id, [FromBody] UserMainInfoDto user)
        {
            var userId = int.Parse(this.User.FindFirst(ClaimTypes.NameIdentifier).Value);

            if (userId != id)
            {
                return(Unauthorized());
            }

            var userFromRepo = await _repo.GetUser(id);

            _mapper.Map(user, userFromRepo);

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

            return(BadRequest());
        }