public async Task <IActionResult> CustomBid(CustomBidViewModel cb) { if (!ModelState.IsValid) { return(NotFound()); } var claimsIdentity = (ClaimsIdentity)this.User.Identity; var claim = claimsIdentity.FindFirst(ClaimTypes.NameIdentifier); var a_user = await _db.AuctionUser.FirstOrDefaultAsync(a => a.UserId == claim.Value && a.AuctionId == cb.AuctionId); var auction = await _db.AuctionHeader.FirstOrDefaultAsync(a => a.Id == cb.AuctionId); a_user.LastPriceOffered = cb.CustomBid; if (auction.CurrentPrice < a_user.LastPriceOffered) { var outbided_user = await _db.AuctionUser.FirstOrDefaultAsync(a => a.AuctionId == auction.Id && a.LastPriceOffered == auction.CurrentPrice); if (outbided_user.UserId != claim.Value) { await NotiApi.SendNotiAuction(_db, "You got outbided, check the auction for more details", outbided_user.UserId, auction.Id); } await NotiApi.SendNotiAuction(_db, "You bid for $" + a_user.LastPriceOffered.ToString("F2") + " in an auction, check the auction for more details", a_user.UserId, auction.Id); auction.CurrentPrice = Math.Max(auction.CurrentPrice, a_user.LastPriceOffered); } _db.SaveChanges(); return(RedirectToAction("Details", new { id = cb.AuctionId, status = SD.ActiveStatus, callBack = SD.BidedAuctions })); }
public async Task <IActionResult> JoinAuction(int id, string callBack) { var claimsIdentity = (ClaimsIdentity)this.User.Identity; var claim = claimsIdentity.FindFirst(ClaimTypes.NameIdentifier); var au = await _db.AuctionHeader.FirstOrDefaultAsync(a => a.Id == id); var a_user = new AuctionUser() { UserId = claim.Value, AuctionId = id, LastPriceOffered = au.CurrentPrice + au.PriceStep }; if (au.CurrentPrice < a_user.LastPriceOffered) { var outbided_user = await _db.AuctionUser.FirstOrDefaultAsync(a => a.AuctionId == au.Id && a.LastPriceOffered == au.CurrentPrice); if (outbided_user != null) { await NotiApi.SendNotiAuction(_db, "You were outbided, check the auction for more details", outbided_user.UserId, au.Id); } await NotiApi.SendNotiAuction(_db, "You bid for $" + a_user.LastPriceOffered.ToString("F2") + " in an auction, check the auction for more details", a_user.UserId, au.Id); au.CurrentPrice = Math.Max(au.CurrentPrice, a_user.LastPriceOffered); } _db.AuctionUser.Add(a_user); _db.SaveChanges(); return(RedirectToAction("Details", new{ id = id, status = SD.ActiveStatus, callBack = callBack })); }
private async void Close(AuctionHeader auction) { var winner = await _db.AuctionUser.FirstOrDefaultAsync(u => u.AuctionId == auction.Id && u.LastPriceOffered == auction.CurrentPrice); if (winner == null) { var aproducts = await _db.AuctionProduct.Where(p => p.AuctionId == auction.Id).ToListAsync(); foreach (AuctionProduct ap in aproducts) { ProductSale psale = await _db.ProductSale.Where(ps => ps.ProductId == ap.ProductId && ps.SellerId == auction.SellerId).FirstOrDefaultAsync(); if (psale != null) { psale.Units += ap.Quantity; _db.ProductSale.Update(psale); } } await NotiApi.SendNotiAuction(_db, "Your auction was succefully ignored", auction.SellerId, auction.Id); auction.Seen = true; _db.AuctionHeader.Update(auction); } else { //winner await NotiApi.SendNotiAuction(_db, "Congrats! You win the auction " + auction.Id.ToString() + ".", winner.UserId, auction.Id); await NotiApi.SendNotiAuction(_db, "Your auction was succefully closed, end value was of $" + auction.CurrentPrice.ToString("F2"), auction.SellerId, auction.Id); //Sending Notificaiton to all the losers var losers = await _db.AuctionUser.Where(u => u.AuctionId == auction.Id && u.LastPriceOffered < auction.CurrentPrice).ToListAsync(); foreach (var l in losers) { await NotiApi.SendNotiAuction(_db, "You lose the auction " + auction.Id.ToString() + ".", l.UserId, auction.Id); } auction.Seen = true; _db.AuctionHeader.Update(auction); } // _db.AuctionHeader.Remove(auction); _db.SaveChanges(); }