コード例 #1
0
        public async Task PutItemOnActionAsync(Guid itemId, decimal value, DateTime expiryDate, Guid userId)
        {
            var item = await this.itemRepository.GetByIdAsync(itemId)
                       ?? throw new Exception("Item not found");

            if (item.OwnerId != userId)
            {
                throw new Exception("Only owner can put item on the auction");
            }

            var auctionItem = await this.auctionItemRepository.GetByItemIdAsync(item.Id);

            if (auctionItem != null)
            {
                throw new Exception("The item has already been auctioned");
            }

            auctionItem = new AuctionItem
            {
                ItemId         = item.Id,
                OwnerId        = userId,
                AuctionStart   = DateTime.UtcNow,
                AuctionFinish  = expiryDate,
                Value          = value,
                TopBidderId    = Guid.Empty,
                TopBidderValue = value
            };

            if (auctionItem.IsExpired())
            {
                throw new Exception("Expiry date is in the past");
            }

            await this.auctionItemRepository.AddAsync(auctionItem);
        }