public async Task CheckLot() { var lots = repository.NotCheckedLots(); foreach (var lot in lots) { if (lot.Ending < DateTime.UtcNow) { if (lot.WinnerName != null) { var winner = await userManager.FindByNameAsync(lot.WinnerName); var message = $"{winner.UserName}, вы успешно приобрели лот: {lot.Name}"; await email.SendEmailAsync(winner.Email, "AuctionCars", message); var messageToOwner = $"{lot.User.UserName}, ваш лот {lot.Name} был приобретён пользователем {winner.UserName}"; await email.SendEmailAsync(lot.User.Email, "AuctionCars", messageToOwner); } else { var user = await userManager.FindByNameAsync(lot.User.UserName); var message = $"{user.UserName}, никто не купил ваш лот: {lot.Name}"; await email.SendEmailAsync(user.Email, "AuctionCars", message); } lot.Ended = true; repository.UpdateLot(lot); } } }
public async Task <IActionResult> Bet(LotDetailtViewModel model) { CarLot lot = carLotsRepository.GetDetailLot(model.BetId); User currentUser = await _userManager.GetUserAsync(HttpContext.User); if (lot.Price > model.BetPrice || lot.StartPrice > model.BetPrice) { return(RedirectToAction("Detail", new { id = lot.Id })); } if (ModelState.IsValid) { User user = await _userManager.GetUserAsync(HttpContext.User); Bet bet = new Bet { User = user, NewPrice = model.BetPrice, CarLot = lot, Time = DateTime.UtcNow }; lot.WinnerName = user.UserName; if (lot.Bets.Count > 1) { currentUser = lot.Bets.Last().User; } betRep.AddBet(bet); lot.Price = model.BetPrice; carLotsRepository.UpdateLot(lot); await updateHub.Clients.Group(lot.Id.ToString()).SendAsync("UpdateTable", lot.Id, bet.User.Id, bet.User.UserName, bet.NewPrice, bet.Time); if (lot.Bets.Count >= 1 && currentUser.Email != user.Email) { await Email.SendEmailAsync(currentUser.Email, "CarLots", $"Ваша ставка на товар {lot.Name} была перебита ставкой в {lot.Price} пользователем {user.UserName}"); } return(RedirectToAction("Detail", new { id = lot.Id })); } return(RedirectToAction("Detail", new { id = lot.Id })); }