コード例 #1
0
        public async Task <FriendshipRequestResult> SendFriendshipRequest(SendFriendshipRequestDto friendshipRequestDto)
        {
            var validationResult = AnnotationValidator.TryValidate(friendshipRequestDto);

            if (validationResult.Failed)
            {
                if (friendshipRequestDto.From.NameTag == friendshipRequestDto.ToNameTag)
                {
                    return(FriendshipRequestResult.Fail(validationResult.Error.ToErrorList().AddError("ToNameTag", "Cannot send a friendship request to yourself.")));
                }
                return(FriendshipRequestResult.Fail(validationResult.Error.ToErrorList()));
            }

            if (friendshipRequestDto.From.NameTag == friendshipRequestDto.ToNameTag)
            {
                return(FriendshipRequestResult.Fail(new ErrorList().AddError("ToNameTag", "Cannot send a friendship request to yourself.")));
            }

            var nameTag     = friendshipRequestDto.ToNameTag.Split('#');
            var displayName = nameTag[0];
            var tag         = DiscriminatorTag.Parse(nameTag[1]);
            var toUser      = await _accountRepository.GetFirstAsync(new NameTagEqualSpecification(tag, displayName));

            if (toUser == null)
            {
                return(FriendshipRequestResult.Fail(new ErrorList().AddError("ToNameTag", "User does not exists.")));
            }

            var isAlreadyFriend = await _friendshipRepository.GetFirstAsync(new FriendshipExistsSpecification(friendshipRequestDto.From.Id, toUser.Id));

            if (isAlreadyFriend != null)
            {
                var errorMessage = isAlreadyFriend.Accepted
                    ? "You are already friends with this user"
                    : isAlreadyFriend.FromId == friendshipRequestDto.From.Id
                        ? "You have already sent a friendship request to this user."
                        : "The user has already sent a friendship request to you.";
                return(FriendshipRequestResult.Fail(new ErrorList().AddError("Friendship", errorMessage)));
            }

            var friendship = new Friendship(friendshipRequestDto.From, toUser);

            await _friendshipRepository.InsertAsync(friendship);

            await _friendshipRepository.CommitAsync();

            return(FriendshipRequestResult.Ok(friendship));
        }
コード例 #2
0
        public async Task <FriendshipRequestResult> DeclineFriendshipRequestAsync(ApplicationUser user, Guid fromUserId)
        {
            var friendshipRequest = await _friendshipRepository.GetFirstAsync(new FriendshipExistsSpecification(fromUserId, user.Id));

            if (friendshipRequest == null)
            {
                return(FriendshipRequestResult.Fail(new ErrorList().AddError("Friendship", "Friendship request does not exist.")));
            }
            if (friendshipRequest.Accepted)
            {
                return(FriendshipRequestResult.Fail(new ErrorList().AddError("Friendship", "Friendship request already accepted.")));
            }

            await _friendshipRepository.DeleteAsync(friendshipRequest);

            await _friendshipRepository.CommitAsync();

            return(FriendshipRequestResult.Ok(friendshipRequest));
        }