public async Task AddBidToItemAsync(NewBidDTO dto, string userId) { Item item = await _unitOfWork.ItemRepo.GetById(dto.ItemId); if (item.UserId == userId) { throw new Exception("You cannot add offer to own item."); } var bestBid = await GetBestBidAsync(item.Id); if (bestBid.Username == dto.Username) { throw new Exception("You cannot outbid yourself"); } item.AddBid(new Bid { BidAmount = dto.MyBid, DatePlaced = DateTime.Now, UserId = userId, Username = dto.Username }); _unitOfWork.Save(); }
public async Task <IActionResult> CreateAuctionBid(NewBidViewModel model) { if (!ModelState.IsValid) { ModelState.AddModelError("", "Niepoprawne dane."); return(RedirectToAction("Item", "Item", new { id = model.ItemId, area = "" })); } NewBidDTO dto = _mappper.Map <NewBidViewModel, NewBidDTO>(model); string userId = User.FindFirst(ClaimTypes.NameIdentifier).Value; string username = User.FindFirst(ClaimTypes.Name).Value; dto.Username = username; await _itemService.AddBidToItemAsync(dto, userId); return(RedirectToAction("Index", "Home", new { area = "" })); }
public async Task <NewBidDTO> GetBestBidAsync(int itemid) { var item = await _unitOfWork.ItemRepo.GetById(itemid); List <Bid> bids = item.Bids; if (bids.Count > 0) { var maxBidAmount = bids.Max(m => m.BidAmount); var bestBid = bids.OrderBy(o => o.DatePlaced).First(f => f.BidAmount == maxBidAmount); NewBidDTO dto = _mapper.Map <Bid, NewBidDTO>(bestBid); return(dto); } return(new NewBidDTO { BestBidPrice = 0.00M, ItemId = itemid, ItemName = item.Name, MyBid = 0.00M }); }