Esempio n. 1
0
        public async Task <BidResponse> Place(BidRequestDto bidRequestDto)
        {
            var bidResponse = await ValidateBid(bidRequestDto);

            if (!bidResponse.Success)
            {
                return(bidResponse);
            }

            try
            {
                var bid     = _mapper.Map <BidRequestDto, Bid>(bidRequestDto);
                var bidGuid = await Task.Run(() => _bidRepository.Place(bid));

                var responseBidDto = _mapper.Map <Bid, BidRequestDto>(bid);
                responseBidDto.Guid = bidGuid;

                bidResponse.bidDto          = responseBidDto;
                bidResponse.CurrentBidCount = _bidRepository.GetCountByAuctionItemId(bidRequestDto.AuctionItemId);
                bidResponse.Message         = $"${bidRequestDto.Amount} bid received.";

                return(bidResponse);
            }
            catch (Exception e)
            {
                return(new BidResponse
                {
                    Success = false,
                    Message = $"An error occurred while placing bid : {e.Message}"
                });
            }
        }
Esempio n. 2
0
        private async Task <BidResponse> ValidateBid(BidRequestDto bidRequestDto)
        {
            var auctionItem = await _auctionRepository.GetAsync(bidRequestDto.AuctionItemId);

            var lastBidItem = _bidRepository.GetLastBidByAuctionItemId(bidRequestDto.AuctionItemId);

            if (auctionItem == null)
            {
                return(new BidResponse
                {
                    Success = false,
                    Message = "Cannot find auction item."
                });
            }

            if (bidRequestDto.Amount == 0)
            {
                return(new BidResponse
                {
                    Success = false,
                    Message = "Bid amount is required."
                });
            }

            if (lastBidItem != null && bidRequestDto.Amount <= lastBidItem.Amount)
            {
                return(new BidResponse
                {
                    Success = false,
                    Message = $"Last bid amount is ${lastBidItem.Amount}. Please bid higher."
                });
            }

            if (bidRequestDto.Amount <= auctionItem.StartPrice)
            {
                return(new BidResponse
                {
                    Success = false,
                    Message = $"Start price for this item is ${auctionItem.StartPrice}."
                });
            }

            if (auctionItem.EndTime < bidRequestDto.BidTime)
            {
                return(new BidResponse
                {
                    Success = false,
                    Message = "This auction is closed."
                });
            }

            return(new BidResponse
            {
                Success = true
            });
        }
Esempio n. 3
0
        public async Task <ActionResult <BidResponse> > PostAsync([FromBody] BidRequestDto bidRequestDto)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState.GetErrorMessages()));
            }

            var bidResponse = await _bidService.Place(bidRequestDto);

            if (!bidResponse.Success)
            {
                return(BadRequest(bidResponse.Message));
            }

            return(Ok(bidResponse));
        }