Exemple #1
0
        public async Task <IActionResult> PostFollowUserAsync(NetworkUserViewModel userToFollowDetails)
        {
            try
            {
                var requestingUser = await _userManager.GetUserWithNetworkAsync(User.Identity.Name);

                if (requestingUser is null)
                {
                    _logger.LogError(LoggingEvents.UpdateItem, "Requesting user not found");
                    return(NotFound("Requesting user not found"));
                }

                var userToFollow = await _userManager.GetUserWithNetworkAsync(userToFollowDetails.UserName);

                if (userToFollow is null)
                {
                    _logger.LogError(LoggingEvents.UpdateItem, "User to follow not found");
                    return(NotFound("User to follow not found"));
                }

                if (requestingUser == userToFollow)
                {
                    return(BadRequest("Trying to follow yourself"));
                }

                _networkRepository.Follow(requestingUser, userToFollow);

                await _unitOfWork.CompleteAsync();

                var viewModel = _mapper.Map <ApplicationUser, NetworkUserViewModel>(userToFollow);

                viewModel.IsFollowing = UserNetworkHelpers.UpdateIsFollowing(viewModel.UserName, requestingUser.Following);

                return(Ok(viewModel));
            }
            catch (Exception ex)
            {
                _logger.LogError(LoggingEvents.Exception, ex, $"an unexpected error occurred");
                return(StatusCode(500, "an unexpected error occurred"));
            }
        }