Beispiel #1
0
        public void BuyNow_generates_valid_events_and_state()
        {
            var user = AuctionTestUtils.CreateUser();

            auction.MarkPendingEventsAsHandled();

            auction.BuyNow(user);

            auction.PendingEvents.Count.Should()
            .Be(1);
            auction.Buyer.Should()
            .Be(user.UserIdentity);
            auction.Completed.Should()
            .BeTrue();
            var ev = auction.PendingEvents.First() as AuctionBought;

            ev.AuctionId.Should()
            .Be(auction.AggregateId);
            ev.UserIdentity.Should()
            .Be(user.UserIdentity);

            user.Credits.Should().Be(1000 - auction.BuyNowPrice);
            user.PendingEvents.Count.Should().Be(1);
            user.PendingEvents.First().Should().BeOfType <CreditsWithdrawn>();
        }
Beispiel #2
0
        public List <Event> BuyNow(Auction auction, User buyer)
        {
            var generatedEvents = new List <Event>();

            foreach (var bid in auction.Bids)
            {
                if (!buyer.UserIdentity.Equals(bid.UserIdentity))
                {
                    var user = _userRepository.FindUser(bid.UserIdentity);
                    user.ReturnCredits(bid.Price);
                    generatedEvents.AddRange(user.PendingEvents);
                    _userRepository.UpdateUser(user);
                }
                else
                {
                    buyer.ReturnCredits(bid.Price);
                }
            }

            auction.BuyNow(buyer);

            return(generatedEvents);
        }