Esempio n. 1
0
        public Bid AddBid(Bid bid)
        {
            var user = databaseAdapter.GetUser(bid.UserId);

            if (user == null)
            {
                throw new NotFoundException("User", bid.UserId.ToString());
            }

            var(product, existingBid) = databaseAdapter.GetProductBid(bid.ProductId);

            if (product == null)
            {
                throw new NotFoundException("Product", bid.ProductId.ToString());
            }

            if (product.AuctionEnd <= DateTime.UtcNow)
            {
                throw new BadRequestException(product, "Auction expired");
            }

            if (existingBid != null && bid.Price <= existingBid.Price)
            {
                throw new BadRequestException(bid, $"Unsupported price. Should be greater then ${existingBid.Price}");
            }

            bid.Id = Guid.NewGuid();
            databaseAdapter.AddBid(bid);

            return(bid);
        }