Example #1
0
        public async Task TryBidding(decimal basePrice, decimal?topPrice, decimal biddingPrice, bool success)
        {
            var state = new AuctionItemState
            {
                StartTime = DateTimeOffset.Parse("2020-01-01 19:30:00"),
                EndTime   = DateTimeOffset.Parse("2020-01-01 20:30:00"),
                BasePrice = basePrice,
            };
            var design = ActorTestHelper.GetDesign(typeof(AuctionItemActor));

            using var mocker = design.CreateAutoMock("1", state);
            mocker.Mock <IClock>()
            .Setup(x => x.UtcNow)
            .Returns(new DateTime(2020, 01, 01, 20, 0, 0));

            mocker.Mock <IClock>()
            .Setup(x => x.UtcNow)
            .Returns(new DateTime(2020, 01, 01, 20, 0, 0));

            if (topPrice != null)
            {
                state.InitBiddingRecords();
                state.BiddingRecords[topPrice.Value] = new BiddingRecord
                {
                    Price  = topPrice.Value,
                    UserId = 11
                };
            }

            var auctionItemActor = mocker.Create <AuctionItemActor>();
            // act
            var input = new TryBiddingInput
            {
                UserId = 777,
                Price  = biddingPrice
            };
            var result = await auctionItemActor.TryBidding(input);

            // assert
            var nowPrice = Math.Max(biddingPrice, basePrice);

            if (topPrice.HasValue)
            {
                nowPrice = Math.Max(nowPrice, topPrice.Value);
            }

            var expectedResult = new TryBiddingResult
            {
                Success           = success,
                NowPrice          = nowPrice,
                UserId            = input.UserId,
                AuctionItemStatus = AuctionItemStatus.OnSell
            };

            result.Should().BeEquivalentTo(expectedResult);
        }
        public async Task <IActionResult> TryBidding([FromBody] TryBiddingWebApiInput webApiInput)
        {
            var input = new TryBiddingInput
            {
                Price  = webApiInput.Price,
                UserId = webApiInput.UserId,
            };
            var itemId           = webApiInput.ItemId;
            var auctionItemActor = _actorProxyFactory.GetClaptrap <IAuctionItemActor>(itemId.ToString());
            var result           = await auctionItemActor.TryBidding(input);

            return(Ok(result));
        }
        public Task <TryBiddingResult> TryBidding(TryBiddingInput input)
        {
            var status = GetStatusCore();

            if (status != AuctionItemStatus.OnSell)
            {
                return(Task.FromResult(CreateResult(false)));
            }

            if (input.Price <= GetTopPrice())
            {
                return(Task.FromResult(CreateResult(false)));
            }

            return(HandleCoreAsync());

            async Task <TryBiddingResult> HandleCoreAsync()
            {
                var dataEvent = this.CreateEvent(new NewBidderEvent
                {
                    Price  = input.Price,
                    UserId = input.UserId
                });
                await Claptrap.HandleEventAsync(dataEvent);

                return(CreateResult(true));
            }

            TryBiddingResult CreateResult(bool success)
            {
                return(new()
                {
                    Success = success,
                    NowPrice = GetTopPrice(),
                    UserId = input.UserId,
                    AuctionItemStatus = status
                });
            }

            decimal GetTopPrice()
            {
                return(StateData.BiddingRecords?.Any() == true
                    ? StateData.BiddingRecords.First().Key
                    : StateData.BasePrice);
            }
        }