public ActionResult MakeBid(MakeBidViewModel viewModel) { if (ModelState.IsValid) { var lot = _lotService.GetLotEntity(viewModel.Id); if (lot.EndDate > DateTime.Now && viewModel.MakeBid > viewModel.CurrentBid) { _bidService.CreateBid(new BidEntity() { BidAmount = viewModel.MakeBid, BidTime = DateTime.Now, LotRefId = viewModel.Id, UserRefId = _userService.GetUserEntityByLogin(User.Identity.Name).Id }); } return(RedirectToAction("LotDetails", new { id = viewModel.Id })); } return(RedirectToAction("LotDetails", new { id = viewModel.Id })); }
public async Task <IActionResult> MakeBid(MakeBidViewModel makeBidViewModel) { if (!User.Identity.IsAuthenticated) { // Get the previous page URL. var prevUri = Request.Headers["Referer"].ToString(); var start = prevUri.IndexOf("Lot") - 1; var res = prevUri.Substring(start, (prevUri.Length - start)); return(RedirectToAction("LogIn", "Account", new { returnUrl = res })); } var bid = _maper.Map <MakeBidViewModel, Bid>(makeBidViewModel); bid.AppUserID = User.FindFirstValue(ClaimTypes.NameIdentifier); bid.BidTime = DateTime.Now; var result = await _bidService.CreateBidAsync(bid); if (!result.Succeeded) { TempData["Error"] = result.Error; } return(RedirectToAction("Details", new { id = makeBidViewModel.LotID })); }
public void AuctionManagementService_MakeBidToAuction() { ApplicationDbContext applicationDb = new ApplicationDbContext(); applicationDb.Database.CreateIfNotExists(); OrganizationType organizationType = new OrganizationType() { Id = Guid.NewGuid(), Name = "TstTypeA_02" }; applicationDb.OrganizationTypes.Add(organizationType); applicationDb.SaveChanges(); Organization auctionMaster = new Organization() { Id = Guid.NewGuid(), FullName = "TestCompanyA_02", RegistrationDate = DateTime.Now, OrganizationTypeId = organizationType.Id }; applicationDb.Organizations.Add(auctionMaster); applicationDb.SaveChanges(); Organization auctionParticipant = new Organization() { Id = Guid.NewGuid(), FullName = "TestCompanyA_022", RegistrationDate = DateTime.Now, OrganizationTypeId = organizationType.Id }; applicationDb.Organizations.Add(auctionParticipant); applicationDb.SaveChanges(); AuctionType auctionType = new AuctionType() { Id = Guid.NewGuid(), Name = "TestAuctionType_02" }; applicationDb.AuctionTypes.Add(auctionType); applicationDb.SaveChanges(); Auction auction = new Auction() { Id = Guid.NewGuid(), Description = "test_any description", MinRatingForParticipant = 10.5, StartPrice = 500000M, MinPrice = 100000M, PriceStep = 50000M, StartDate = new DateTime(2019, 4, 26), FinishDate = new DateTime(2019, 5, 22), Status = AuctionStatus.Active, AuctionTypeId = auctionType.Id, OrganizationId = auctionMaster.Id }; applicationDb.Auctions.Add(auction); applicationDb.SaveChanges(); Transaction depositTrans = new Transaction() { Id = Guid.NewGuid(), TransactionType = TransactionType.Deposit, Sum = 400000, TransactionDate = DateTime.Now, OrganizationId = auctionParticipant.Id }; applicationDb.Transactions.Add(depositTrans); applicationDb.SaveChanges(); Transaction withdrawTrans = new Transaction() { Id = Guid.NewGuid(), TransactionType = TransactionType.Withdraw, Sum = 100000, TransactionDate = DateTime.Now, OrganizationId = auctionParticipant.Id }; applicationDb.Transactions.Add(withdrawTrans); applicationDb.SaveChanges(); MakeBidViewModel model = new MakeBidViewModel() { AuctionId = auction.Id.ToString(), OrganizationId = auctionParticipant.Id.ToString(), Price = 200000M }; decimal bidCost = 50000M; AuctionManagementService sut = new AuctionManagementService(); sut.MakeBidToAuction(model, bidCost); Bid bid = applicationDb.Bids .SingleOrDefault(p => p.AuctionId == auction.Id && p.OrganizationId == auctionParticipant.Id); Transaction transaction = applicationDb.Transactions .SingleOrDefault(p => p.OrganizationId == auctionParticipant.Id && p.Sum == bidCost); Assert.IsNotNull(bid); Assert.IsNotNull(transaction); }
public void MakeBidToAuction(MakeBidViewModel model, decimal bidCost) { var bidExists = _applicationDbContext.Bids .Any(p => p.Price == model.Price && p.AuctionId.ToString() == model.AuctionId && p.Description == model.Description && p.OrganizationId.ToString() == model.OrganizationId); if (bidExists) { throw new Exception("Такая ставка уже существует"); } var organization = _applicationDbContext.Organizations.Include("Transactions") .SingleOrDefault(p => p.Id.ToString() == model.OrganizationId); if (organization == null) { throw new Exception("Такой организации в базе нет"); } var auction = _applicationDbContext.Auctions .SingleOrDefault(p => p.Id.ToString() == model.AuctionId); if (auction.OrganizationId.ToString() == model.OrganizationId) { throw new Exception("Создатель аукциона не может подавать ставки для участия в этом аукционе"); } //проверка баланса на наличие средств для оплаты участия в аукционе var organizationTransactions = organization.Transactions.ToList(); if (organizationTransactions.Count == 0) { throw new Exception($"У организации {organization.FullName} нулевой баланс"); } var organizationBalance = organizationTransactions.Where(p => p.TransactionType == TransactionType.Deposit).Sum(p => p.Sum) - organizationTransactions.Where(p => p.TransactionType == TransactionType.Withdraw).Sum(p => p.Sum); if (organizationBalance < bidCost) { throw new Exception($"У организации {organization.FullName} не хватает средств на балансе для оплаты стоимости участия в аукционе"); } //проверка ставки на соответствие мин, макс ценам и шагу цены var inValidPriceRange = _applicationDbContext.Auctions .Where(p => p.Id.ToString() == model.AuctionId && p.MinPrice <model.Price && p.StartPrice> model.Price).ToList(); var inStepRange = inValidPriceRange .Any(p => (p.StartPrice - model.Price) % p.PriceStep == 0); if (!inStepRange) { throw new Exception("Invalid bid according price step"); } var activeBidStatus = _applicationDbContext.BidStatuses.SingleOrDefault(p => p.StatusName == "Active"); if (activeBidStatus == null) { BidStatus status = new BidStatus() { Id = Guid.NewGuid(), StatusName = "Active" }; _applicationDbContext.BidStatuses.Add(status); _applicationDbContext.SaveChanges(); activeBidStatus = status; } //делаем ставку и списываем деньги за участие Bid bid = new Bid() { Id = Guid.NewGuid(), Price = model.Price, IsWin = false, Description = model.Description, AuctionId = new Guid(model.AuctionId), OrganizationId = new Guid(model.OrganizationId), CreatedDate = DateTime.Now, BidStatusId = activeBidStatus.Id }; _applicationDbContext.Bids.Add(bid); _applicationDbContext.SaveChanges(); Transaction transaction = new Transaction() { Id = Guid.NewGuid(), Sum = bidCost, TransactionType = TransactionType.Withdraw, TransactionDate = DateTime.Now, OrganizationId = new Guid(model.OrganizationId), Description = $"Withdraw participation cost for auction {model.AuctionId}" }; _applicationDbContext.Transactions.Add(transaction); _applicationDbContext.SaveChanges(); }