예제 #1
0
        /// <inheritdoc />
        public async Task <Result> Handle(InviteFriendToGroupEventCommand request, CancellationToken cancellationToken)
        {
            // TODO: Consider moving this into domain service? Potential for some interesting business rules.
            Maybe <GroupEvent> maybeGroupEvent = await _groupEventRepository.GetByIdAsync(request.GroupEventId);

            if (maybeGroupEvent.HasNoValue)
            {
                return(Result.Failure(DomainErrors.GroupEvent.NotFound));
            }

            GroupEvent groupEvent = maybeGroupEvent.Value;

            if (groupEvent.UserId != _userIdentifierProvider.UserId)
            {
                return(Result.Failure(DomainErrors.User.InvalidPermissions));
            }

            Maybe <User> maybeUser = await _userRepository.GetByIdAsync(groupEvent.UserId);

            if (maybeUser.HasNoValue)
            {
                return(Result.Failure(DomainErrors.GroupEvent.UserNotFound));
            }

            Maybe <User> maybeFriend = await _userRepository.GetByIdAsync(request.FriendId);

            if (maybeFriend.HasNoValue)
            {
                return(Result.Failure(DomainErrors.GroupEvent.FriendNotFound));
            }

            User user = maybeUser.Value;

            User friend = maybeFriend.Value;

            if (!await _friendshipRepository.CheckIfFriendsAsync(user, friend))
            {
                return(Result.Failure(DomainErrors.GroupEvent.NotFriends));
            }

            Result <Invitation> invitationResult = await groupEvent.InviteAsync(friend, _invitationRepository);

            if (invitationResult.IsFailure)
            {
                return(Result.Failure(invitationResult.Error));
            }

            Invitation invitation = invitationResult.Value;

            _invitationRepository.Insert(invitation);

            await _unitOfWork.SaveChangesAsync(cancellationToken);

            return(Result.Success());
        }
예제 #2
0
        /// <summary>
        /// Creates a new friendship request for the specified friend.
        /// </summary>
        /// <param name="friend">The friend.</param>
        /// <param name="friendshipRepository">The friendship repository.</param>
        /// <param name="friendshipRequestRepository">The friendship request repository.</param>
        /// <returns>The result that contains a friendship request or an error.</returns>
        public async Task <Result <FriendshipRequest> > SendFriendshipRequestAsync(
            User friend,
            IFriendshipRepository friendshipRepository,
            IFriendshipRequestRepository friendshipRequestRepository)
        {
            if (await friendshipRepository.CheckIfFriendsAsync(this, friend))
            {
                return(Result.Failure <FriendshipRequest>(DomainErrors.FriendshipRequest.AlreadyFriends));
            }

            if (await friendshipRequestRepository.CheckForPendingFriendshipRequestAsync(this, friend))
            {
                return(Result.Failure <FriendshipRequest>(DomainErrors.FriendshipRequest.PendingFriendshipRequest));
            }

            var friendshipRequest = new FriendshipRequest(this, friend);

            AddDomainEvent(new FriendshipRequestSentDomainEvent(friendshipRequest));

            return(friendshipRequest);
        }
예제 #3
0
        /// <summary>
        /// Removes the friendship between the specified users.
        /// </summary>
        /// <param name="userId">The user identifier.</param>
        /// <param name="friendId">The friend identifier.</param>
        /// <returns>The result of the operation.</returns>
        public async Task <Result> RemoveFriendshipAsync(Guid userId, Guid friendId)
        {
            Maybe <User> maybeUser = await _userRepository.GetByIdAsync(userId);

            if (maybeUser.HasNoValue)
            {
                return(Result.Failure(DomainErrors.Friendship.UserNotFound));
            }

            Maybe <User> maybeFriend = await _userRepository.GetByIdAsync(friendId);

            if (maybeFriend.HasNoValue)
            {
                return(Result.Failure(DomainErrors.Friendship.FriendNotFound));
            }

            User user = maybeUser.Value;

            User friend = maybeFriend.Value;

            if (!await _friendshipRepository.CheckIfFriendsAsync(user, friend))
            {
                return(Result.Failure(DomainErrors.Friendship.NotFriends));
            }

            var userToFriendFriendship = new Friendship(user, friend);

            var friendToUserFriendship = new Friendship(friend, user);

            // This will add the appropriate domain event that will be published after saving the changes.
            user.RemoveFriendship(userToFriendFriendship);

            _friendshipRepository.Remove(userToFriendFriendship);

            _friendshipRepository.Remove(friendToUserFriendship);

            return(Result.Success());
        }