示例#1
0
        protected override Task <RequestStatus> HandleCommand(BuyNowCommand request, CancellationToken cancellationToken)
        {
            var user = _userRepository.FindUser(request.SignedInUser);

            if (user == null)
            {
                _logger.LogError("BuyNowCommandHandler cannot find user {@user}", request.SignedInUser);
                throw new CommandException($"Cannot find user: {user.UserIdentity}");
            }

            var auction = _auctionRepository.FindAuction(request.AuctionId);

            if (auction == null)
            {
                throw new CommandException($"Invalid auction id: {request.AuctionId}");
            }

            var buyNowService = new BuyNowService(_userRepository);

            var generatedEvents = buyNowService.BuyNow(auction, user);

            generatedEvents.AddRange(auction.PendingEvents);
            generatedEvents.AddRange(user.PendingEvents);

            _auctionRepository.UpdateAuction(auction);
            _userRepository.UpdateUser(user);

            _eventBusService.Publish(generatedEvents, request.CommandContext.CorrelationId, request);

            return(Task.FromResult(RequestStatus.CreateFromCommandContext(request.CommandContext, Status.PENDING)));
        }
示例#2
0
        public void BuyNow_when_auction_have_bids_returns_credits_to_users_and_generates_valid_events()
        {
            auction.Raise(users[0], 1);
            var bid1 = auction.Bids.Last();

            auction.Raise(users[1], 2);
            var bid2 = auction.Bids.Last();

            auction.Raise(users[2], 3);
            var bid3 = auction.Bids.Last();

            auction.MarkPendingEventsAsHandled();
            foreach (var user in users)
            {
                user.MarkPendingEventsAsHandled();
            }

            var generatedEvents = service.BuyNow(auction, users[2]);

            generatedEvents.Count.Should().Be(2);


            foreach (var user in users.Take(2))
            {
                user.Credits.Should().Be(1000);
                user.PendingEvents.Count.Should().Be(1);
                user.PendingEvents.Last().Should().BeOfType <CreditsReturned>();
            }

            users[2].Credits.Should().Be(1000 - auction.BuyNowPrice);
            users[2].PendingEvents.Count.Should().Be(2);
            users[2].PendingEvents.First().Should().BeOfType <CreditsReturned>();
            users[2].PendingEvents.Last().Should().BeOfType <CreditsWithdrawn>();
        }