Beispiel #1
0
        public void testAddBid()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(databaseName: "Add_bid_to_db")
                          .Options;

            int    price       = 230;
            string description = "This is how much you owe me";
            var    start       = new DateTime(2017 / 01 / 12);
            var    end         = new DateTime(2017 / 01 / 12);
            Bid    bid         = new Bid();

            bid.bidPrice    = price;
            bid.description = description;
            bid.startDate   = start;
            bid.endDate     = end;

            using (ApplicationDbContext context = new ApplicationDbContext(options))
            {
                IBidRepository bidRepo = new BidRepository(context);
                bidRepo.AddBid(bid);
                bidRepo.Save();
            }

            // Use a separate instance of the context to verify correct data was saved to database
            using (var context = new ApplicationDbContext(options))
            {
                IBidRepository bidRepo     = new BidRepository(context);
                var            bidRecieved = bidRepo.GetBidByID(bid.bidId);
                Assert.True(bidRecieved.bidPrice == price);
            }
        }
Beispiel #2
0
        public void testDeleteBid()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(databaseName: "Delete_bid_from_db")
                          .Options;


            int    price       = 230;
            string description = "This is how much you owe me";
            var    start       = new DateTime(2017 / 01 / 12);
            var    end         = new DateTime(2017 / 01 / 12);
            Bid    bid         = new Bid();

            bid.bidPrice    = price;
            bid.description = description;
            bid.startDate   = start;
            bid.endDate     = end;

            using (ApplicationDbContext context = new ApplicationDbContext(options))
            {
                IBidRepository bidRepo = new BidRepository(context);
                bidRepo.AddBid(bid);
                bidRepo.Save();
            }

            // Use a seperate instance of the context to verify correct data was saved to the db
            using (var context = new ApplicationDbContext(options))
            {
                Assert.True(context.Bid.Count() == 1);
            }
            using (var context = new ApplicationDbContext(options))
            {
                IBidRepository bidRepo = new BidRepository(context);
                bidRepo.DeleteBid(bid.bidId);
                bidRepo.Save();
            }
            using (var context = new ApplicationDbContext(options))
            {
                Assert.True(context.Bid.Count() == 0);
            }
        }
Beispiel #3
0
        public int PostBid(Guid auctionId, Guid userId)
        {
            using (var tx = new TransactionScope())
            {
                try
                {
                    MarkExpiredAsCompleted();
                    var auction = _auctionRepository.GetById(auctionId);
                    if (auction.ExpiresAt <= DateTime.Now)
                    {
                        // aukcija se vec zavrsila
                        tx.Complete();
                        return(-1);
                    }
                    if (auction.UserId == userId)
                    {
                        // korisnik je vlasnik aukcije
                        tx.Complete();
                        return(-2);
                    }
                    var oldBid = _bidRepository.GetTopBidForAuction(auctionId);
                    if (oldBid != null && oldBid.UserId == userId)
                    {
                        // korisnik vec vodi na licitaciji
                        tx.Dispose();
                        return(-3);
                    }
                    var user = _userRepository.GetById(userId);
                    if ((user.TokenCount - (auction.Price)) < 0)
                    {
                        // korisnik nema dovoljno sredstava za licitiranje na datoj aukciji
                        tx.Dispose();
                        return(-4);
                    }

                    if (oldBid != null)
                    {
                        var oldUser = _userRepository.GetById(oldBid.UserId);
                        oldUser.TokenCount += oldBid.BidAmount;
                        _userRepository.Save(oldUser);
                    }
                    auction.Price++;
                    var bid = new Bid
                    {
                        Id        = Guid.NewGuid(),
                        UserId    = userId,
                        AuctionId = auctionId,
                        Timestamp = DateTime.Now,
                        BidAmount = auction.Price
                    };
                    _bidRepository.Save(bid);
                    _auctionRepository.Save(auction);
                    user.TokenCount -= auction.Price;
                    _userRepository.Save(user);
                    tx.Complete();
                    return(0);
                }
                catch
                {
                    // doslo je do izuzetka, vracamo -5
                    tx.Dispose();
                    return(-5);
                }
            }
        }
Beispiel #4
0
 public void AddBidding(Paper paper, BidEnum bidenum, User user)
 {
     bidRepo.Save(new Bid(user, bidenum, paper));
 }