Exemplo n.º 1
0
        public Trade AddAsk(string country, Ask ask, out bool updated)
        {
            using (var database = new RWCDatabaseContext())
            {
                var dbCountry = database.Countries
                    .Include(i => i.Bids)
                    .Include(i => i.Ask)
                    .Include("User.Countries")
                    .First(i => i.Code == country);
                var dbBids = dbCountry.Bids.ToList();
                var dbAsk = dbCountry.Ask;
                updated = false;
                if (dbAsk != null && dbCountry.User.UserID==ask.UserID)
                {
                    dbAsk.TimeStamp = ask.TimeStamp;
                    dbAsk.Price = ask.Price;
                    updated = true;
                    database.SaveChanges();
                    if (!dbBids.Any())
                    {
                        return null;
                    }

                }
                ask.CountryID = dbCountry.CountryID;
                if (!dbBids.Any() && dbAsk==null)
                {
                    dbCountry.Ask = ask;
                    database.SaveChanges();
                    return null;
                }
                var highestBid = dbBids.OrderByDescending(i => i.Price)
                                       .ThenBy(i => i.TimeStamp)
                                       .First();
                if (highestBid.Price >= ask.Price)
                {
                    database.Bids.Remove(highestBid);
                    dbCountry.User.Countries.Remove(dbCountry);
                    dbCountry.User = highestBid.User;
                    dbCountry.Ask = null;
                    var trade = new Trade()
                    {
                        BuyerID = highestBid.UserID,
                        SellerID = ask.UserID,
                        CountryID = dbCountry.CountryID,
                        Price = ask.Price,
                        TimeStamp = DateTime.UtcNow
                    };
                    database.Trades.Add(trade);
                    database.SaveChanges();
                    return trade;
                }
                if (updated)return null;
                dbCountry.Ask = ask;
                database.SaveChanges();
                return null;
            }
        }
Exemplo n.º 2
0
 public Trade AddBid(string country,Bid bid, out bool updated)
 {
     using (var database = new RWCDatabaseContext())
     {
         var dbCountry = database.Countries
             .Include(i=>i.Bids)
             .Include(i=>i.Ask)
             .Include("User.Countries")
             .First(i => i.Code == country);
         var dbBids = dbCountry.Bids.ToList();
         var dbAsk = dbCountry.Ask;
         var existingForUser = dbBids.FirstOrDefault(i => i.User.UserID == bid.UserID);
         updated = false;
         bid.CountryID = dbCountry.CountryID;
         if (existingForUser != null)
         {
             existingForUser.TimeStamp = bid.TimeStamp;
             existingForUser.Price = bid.Price;
             updated = true;
             database.SaveChanges();
             if (dbAsk == null || !dbBids.OrderByDescending(i => i.Price)
                                     .ThenBy(i => i.TimeStamp)
                                     .First()
                                     .Equals(bid))
             {
                 return null;
             }
         }
         if (dbAsk == null)
         {
             dbCountry.Bids.Add(bid);
             database.SaveChanges();
             return null;
         }
         if (bid.Price >= dbAsk.Price)
         {
             var currentSeller = dbCountry.User.UserID;
             dbCountry.Ask = null;
             dbCountry.User.Countries.Remove(dbCountry);
             dbCountry.UserID = bid.UserID;
             if (updated)
             {
                 database.Bids.Remove(existingForUser);
             }
             var trade = new Trade
             {
                 BuyerID = bid.UserID,
                 CountryID = dbCountry.CountryID,
                 Price = bid.Price,
                 SellerID =currentSeller,
                 TimeStamp = DateTime.UtcNow
             };
             database.Trades.Add(trade);
             database.SaveChanges();
             return trade;
         }
         if (updated) return null;
         dbCountry.Bids.Add(bid);
         database.SaveChanges();
         return null;
     }
 }