Exemplo n.º 1
0
        public void RequestFriendship(Friendship friendship, string userPseudo)
        {
            Errors.Clear();

            using (var scope = new TransactionScope())
            {
                try
                {
                    // Saving friendship
                    _friendshipRepository.SaveFriendship(friendship);

                    if (_friendshipRepository.HasErrors)
                    {
                        MergeErrors(_friendshipRepository);
                        return;
                    }

                    // retrieving friend's account
                    var friend = _userRepository.GetUserInfo(friendship.FriendName);

                    if (_userRepository.HasErrors || friend == null)
                    {
                        Errors.Add("Unable to find friend's account");
                        MergeErrors(_userRepository);
                        return;
                    }

                    // Creating opposite friendship
                    var otherFriendship = friendship.Clone() as Friendship;

                    if (otherFriendship == null)
                    {
                        Errors.Add("Unexpected error occured, please call support");
                        return;
                    }

                    otherFriendship.UserId      = friend.Id;
                    otherFriendship.FriendName  = userPseudo;
                    otherFriendship.IsRequested = false;
                    otherFriendship.IsWaiting   = true;

                    //saving other friendship
                    _friendshipRepository.SaveFriendship(otherFriendship);

                    if (_friendshipRepository.HasErrors)
                    {
                        MergeErrors(_friendshipRepository);
                        return;
                    }

                    scope.Complete();
                }
                catch (Exception ex)
                {
                    HandleException(ex);
                }
            }
        }