public ActionResult AddBid(BidCreateDto Bid) { var userId = HttpContext.User.Identity.Name; Bid.UserId = Guid.Parse(userId); var result = _repository.AddBid(Bid); if (result != BidCode.Success) { return(BadRequest(result.GetDescription())); } return(Ok(new { message = result.GetDescription() })); }
public BidCode AddBid(BidCreateDto Bid) { var bid = _mapper.Map <Bid>(Bid); bid.BidDate = DateTime.UtcNow; bid.BidId = Guid.NewGuid(); var latestBid = _context.Bid.OrderByDescending(x => x.BidAmount).FirstOrDefault(x => x.ProductId == bid.ProductId); if (latestBid != null) { var product = _context.Product.Include("Bids.User").FirstOrDefault(x => x.ProductId == bid.ProductId); if (product == null) { return(BidCode.Null); } if (bid.BidAmount <= latestBid.BidAmount) { return(BidCode.PriceTooLow); } if (bid.UserId == latestBid.UserId) { return(BidCode.HighestBid); } var bidders = product.Bids.Select(x => x.User).Distinct(); foreach (var bidder in bidders) { if (bidder.UserId != Bid.UserId) { var message = $"<h3>A new bid has been made for {product.ProductName}!</h3><p>Current highest bid amount:${bid.BidAmount}</p><p>Submit a higher bid in order to win the auction!</p>"; var mail = _emailService.NewMail(bidder.Username, $"New Bid for {product.ProductName}", message); Task.Factory.StartNew(() => _emailService.SendEmail(mail)); } else { var message = $"<h3>Your bid for {product.ProductName} is a success!</h3><p>Your bid amount:${bid.BidAmount}</p><p>Submit a higher bid in order to win the auction!</p>"; var mail = _emailService.NewMail(bidder.Username, $"Bid success for {product.ProductName}", message); Task.Factory.StartNew(() => _emailService.SendEmail(mail)); } } } _context.Bid.Add(bid); _context.SaveChanges(); _bidHubContext.Clients.All.SendAsync("ReceiveBid", _mapper.Map <BidReadDto>(bid)); return(Codes.BidCode.Success); }