Exemple #1
0
        public void CreateBud_CallsRepositoryMethod()
        {
            // Arrange
            var      mockRepo = new Mock <IAuktionRepository>();
            var      service  = new AuktionService(mockRepo.Object, null);
            BudModel obj      = null;
            var      bud      = new BiddingViewModel {
                AuktionID = 1, Summa = 2
            };
            var user = new NauktionUser {
                Id = "abcdef"
            };

            mockRepo.Setup(t => t.CreateBudAsync(It.IsAny <BudModel>()))
            .Callback((BudModel o) => obj = o)
            .Returns(Task.CompletedTask).Verifiable();

            // Act
            service.CreateBudAsync(bud, user).GetAwaiter().GetResult();

            // Assert
            Assert.IsNotNull(obj);
            Assert.AreEqual(1, obj.AuktionID);
            Assert.AreEqual(2, obj.Summa);
            Assert.AreEqual(user.Id, obj.Budgivare);
            mockRepo.Verify();
        }
Exemple #2
0
        public async Task <IActionResult> Bid(BiddingViewModel bid)
        {
            NauktionUser currentUser = await _userManager.GetUserAsync(User);

            // Validate
            if (ModelState.IsValid)
            {
                string error = await _service.ValidateBud(bid.AuktionID, bid.Summa, currentUser);

                if (!(error is null))
                {
                    ModelState.AddModelError(nameof(bid.Summa), error);
                }
            }

            // Redirect if invalid
            if (!ModelState.IsValid)
            {
                TempData["BidErrors"] = ModelState[nameof(bid.Summa)].Errors
                                        .Select(e => e.ErrorMessage)
                                        .ToArray();

                return(RedirectToAction("View", new { id = bid.AuktionID }));
            }

            // Valid! Let's create that bid!
            await _service.CreateBudAsync(bid, currentUser);

            TempData["BidSuccess"] = true;
            return(RedirectToAction("View", new { id = bid.AuktionID }));
        }
 public async Task CreateBudAsync(BiddingViewModel model, NauktionUser budgivare)
 {
     await _repository.CreateBudAsync(new BudModel
     {
         AuktionID = model.AuktionID,
         Summa     = model.Summa,
         Budgivare = budgivare.Id
     });
 }
        public async Task <ActionResult> CreateBidding([DataSourceRequest] DataSourceRequest request, BiddingViewModel model)
        {
            if (ModelState.IsValid)
            {
                var userID = User.Identity.GetUserId();
                var person = _edmx.Persons.FirstOrDefault(x => x.UserId == userID);
                model.Person.PersonId = person.PersonId;
                var biddingId = await _biddingProvider.CreateBidding(model);

                var bidding = (await _biddingProvider.GetBiddings(-1)).FirstOrDefault(x => x.BiddingId == biddingId);
                model = bidding;
            }
            return(Json(new[] { model }.ToDataSourceResult(request, ModelState)));
        }
Exemple #5
0
        public async Task <int> CreateBidding(BiddingViewModel model)
        {
            try
            {
                var lotEntity = new Lot
                {
                    Name   = model.Lot.Name,
                    Status = 1,
                    DateOfEndRegistration   = model.Lot.DateOfEndRegistration,
                    DateOfPub               = model.Lot.DateOfPub,
                    DateOfStartRegistration = model.Lot.DateOfStartRegistration,
                    DateOfSummarizing       = model.Lot.DateOfSummarizing,
                    DateOfBidding           = model.Lot.DateOfBidding,
                    DeliveryAddress         = model.Lot.DeliveryAddress
                };
                _edmx.Lots.Add(lotEntity);
                await _edmx.SaveChangesAsync();

                var documentEntity = new Document
                {
                    Name = model.Document.Name,
                    URL  = model.Document.URL
                };
                _edmx.Documents.Add(documentEntity);
                await _edmx.SaveChangesAsync();

                var protocolEntity = new Protocol
                {
                    Name        = model.Protocol.Name,
                    Description = model.Protocol.Desc
                };
                _edmx.Protocols.Add(protocolEntity);
                await _edmx.SaveChangesAsync();

                var countryEntity = new Country
                {
                    Name = model.Country.Name,
                    ISO  = model.Country.ISO
                };
                _edmx.Countries.Add(countryEntity);
                await _edmx.SaveChangesAsync();

                var biddingEntity = new Bidding
                {
                    BiddingTypeId = model.BiddingType.BiddingTypeId,
                    Name          = model.Name,
                    LotId         = lotEntity.LotId,
                    PersonId      = model.Person.PersonId,
                    TenderTypeId  = model.TenderType.TenderTypeId,
                    ProtocolId    = model.ProtocolId,
                    CountryId     = model.Country.CountryId
                };
                _edmx.Biddings.Add(biddingEntity);
                await _edmx.SaveChangesAsync();

                return(biddingEntity.BiddingId);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Exemple #6
0
        public async Task <IActionResult> Bid([FromBody] BiddingViewModel model)
        {
            if (ModelState.IsValid)
            {
                string id            = _signInManager.UserManager.GetUserId(User);
                double startingPrice = 0;
                try
                {
                    startingPrice = _unitOfWork.Auctions.GetStartingPrice(model.AuctionId);
                }
                catch (Exception)
                {
                    return(BadRequest("Your request failed"));
                }
                if (_unitOfWork.Offers.IsThereAnyOffer(model.AuctionId))
                {
                    double highestBid = _unitOfWork.Offers.GetHighestBid(model.AuctionId);
                    if (model.Amount <= highestBid || model.Amount <= startingPrice)
                    {
                        return(BadRequest("Your request failed"));
                    }
                    else
                    {
                        var now      = DateTime.UtcNow;
                        var newOffer = new Offer
                        {
                            Amount    = model.Amount,
                            AuctionId = model.AuctionId,
                            DateTime  = now,
                            UserId    = id
                        };
                        _unitOfWork.Offers.Add(newOffer);
                        await _unitOfWork.Save();

                        await this.hubContext.Clients.All.SendAsync("NewBid", $"Currently the highest bid: <strong>${model.Amount}</strong>", model.AuctionId);

                        await this.hubContext.Clients.All.SendAsync("AddOffer", new {
                            Amount = model.Amount,
                            Time   = now
                        });

                        return(Ok("You have made a new bid"));
                    }
                }
                else
                {
                    if (model.Amount > startingPrice)
                    {
                        var now      = DateTime.UtcNow;
                        var newOffer = new Offer
                        {
                            Amount    = model.Amount,
                            AuctionId = model.AuctionId,
                            DateTime  = now,
                            UserId    = id
                        };
                        _unitOfWork.Offers.Add(newOffer);
                        await _unitOfWork.Save();

                        await this.hubContext.Clients.All.SendAsync("NewBid", $"Currently the highest bid: <strong>${model.Amount}</strong>", model.AuctionId);

                        await this.hubContext.Clients.All.SendAsync("AddOffer", new
                        {
                            Amount = model.Amount,
                            Time   = now
                        });

                        return(Ok("You have made a new bid"));
                    }
                    else
                    {
                        return(BadRequest("Your request failed"));
                    }
                }
            }
            else
            {
                return(BadRequest("Your data is invalid"));
            }
        }