Ejemplo n.º 1
0
        /// <summary>
        /// Creates the user bid with server-side validation.
        /// </summary>
        /// <param name="bid"></param>
        /// <returns>
        /// The BidResult object with success state and errors if occured.
        /// </returns>
        public async Task <BidResult> CreateBidAsync(Bid bid)
        {
            if (bid == null)
            {
                throw new ArgumentNullException(nameof(bid));
            }

            var lot = await _lotRepository.GetByIdAsync(bid.LotID);

            if (lot == null)
            {
                throw new ArgumentNullException(nameof(lot));
            }

            var soldLotState = await _lotStateRepository.GetStateByNameAsync("Sold");

            if (soldLotState == null)
            {
                throw new ArgumentNullException(nameof(soldLotState));
            }

            if (lot.LotStateID == soldLotState.LotStateID)
            {
                return(AddError("The lot has been sold."));
            }

            if (lot.Bids.Any())
            {
                var maxBid = lot.Bids.Max(b => b.Amount);

                if (bid.Amount < maxBid + lot.MinBid)
                {
                    return(AddError("Entered value doesn't match minimum bid (" +
                                    (maxBid + lot.MinBid) + ")! Don't be greedy ;)"));
                }
                else
                {
                    await _bidRepository.AddAsync(bid);
                }
            }
            else
            {
                if (bid.Amount < lot.StartPrice + lot.MinBid)
                {
                    return(AddError("Entered value doesn't match minimum bid (" +
                                    (lot.StartPrice + lot.MinBid) + ")! Don't be greedy ;)"));
                }
                else
                {
                    await _bidRepository.AddAsync(bid);
                }
            }

            await _unitOfWork.CompleteAsync();

            return(new BidResult {
                Succeeded = true
            });
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Gets the lot with bids checking the state and time left.
        /// Actually the lot selling occures here by marking the lot as sold.
        /// </summary>
        /// <param name="id"></param>
        /// <returns>
        /// Lot domain object.
        /// </returns>
        public async Task <Lot> GetLotWithBidsAsync(int id)
        {
            var lot = await _lotRepository.GetLotWithBidsAsync(id);

            if (lot == null)
            {
                throw new LotNotFoundException(id);
            }

            var activeLotState = await _lotStateRepository.GetStateByNameAsync("Active");

            // The second check is for preventing double marking sold lot.
            if (DateTime.Now > lot.EndTime && lot.LotStateID == activeLotState.LotStateID)
            {
                await MarkLotAsSold(lot);

                await _unitOfWork.CompleteAsync();
            }

            return(lot);
        }