示例#1
0
        //Context.User = NULL!!!!!
        //don't know why...
        public void SendBid(int userID, int auctionID, int amount)
        {
            if (amount > 0)
            {
                User user = db.Users.FirstOrDefault(u => u.ID == userID);
                if (user == null)
                    return;

                Auction auction = db.Auctions.Find(auctionID);
                if (auction == null || auction.StatusID != 2 || auction.Deadline < DateTime.Now)
                    return;

                int highestBid = 0;
                if (auction.AuctionBids.Count > 0)
                    highestBid = auction.AuctionBids.Max(b => b.Amount);
                if (!(amount > highestBid && amount > auction.StartPrice))
                    return;

                try
                {
                    AuctionBid bid = new AuctionBid()
                    {
                        Amount = amount,
                        AuctionID = auctionID,
                        DateCreated = DateTime.Now,
                        UserID = userID
                    };
                    db.AuctionBids.Add(bid);

                    //auction.CurrentPrice += amount;
                    auction.CurrentPrice = amount;
                    db.SaveChanges();

                    ICurrencyService CurrencyService = DependencyResolver.Current.GetService<ICurrencyService>();
                    double dollarRate = CurrencyService.GetCurrencyRate();

                    string priceFormat = XCarsConfiguration.PriceFormat;

                    string PriceUSDStr = (auction.CurrencyID == 1) ? (auction.CurrentPrice.ToString(priceFormat) + " " + CurrencyService.GetByID(1).Symbol) : (((int)(auction.CurrentPrice / dollarRate)).ToString(priceFormat) + " " + CurrencyService.GetByID(1).Symbol);
                    string PriceUAHStr = (auction.CurrencyID == 2) ? (auction.CurrentPrice.ToString(priceFormat) + " " + CurrencyService.GetByID(2).Symbol) : (((int)(auction.CurrentPrice * dollarRate)).ToString(priceFormat) + " " + CurrencyService.GetByID(2).Symbol);

                    string name = user.FirstName + " " + user.LastName;
                    if (string.IsNullOrWhiteSpace(name))
                        name = Resource.NoName;
                    string city = user.City?.Name ?? Resource.NoCity;

                    List<AuctionConnection> currentAuctionConnections = db.AuctionConnections.Where(c => c.AuctionID == auctionID).ToList();
                    foreach (var item in currentAuctionConnections)
                    {
                        Clients.Client(item.Connection).broadcastBid(name, city, /*photoSrc,*/ amount.ToString(XCarsConfiguration.PriceFormat), bid.DateCreated.ToString("dd.MM.yyyy HH:mm"), PriceUSDStr, PriceUAHStr);
                    }
                }
                catch (Exception ex)
                { }
                //Clients.All.broadcastMessage();
            }
        }
示例#2
0
        public void AddConnectionIDforCurrentUser(string email)
        {
            try
            {
                //IUserService userService = DependencyResolver.Current.GetService<IUserService>();


                User user = db.Users.FirstOrDefault(u => u.Email == email);
                //User user1 = userService.GetUserByEmail(email);
                if (user != null)
                {
                    UserConnection connection = db.UserConnections.FirstOrDefault(c => c.UserID == user.ID && c.Connection == Context.ConnectionId);
                    if (connection == null)
                    {
                        user.UserConnections.Add(new UserConnection()
                        {
                            Connection  = Context.ConnectionId,
                            DateCreated = DateTime.Now
                        });
                    }
                    else
                    {
                        connection.DateCreated = DateTime.Now;
                    }

                    db.SaveChanges();

                    foreach (var item in db.UserConnections.Where(c => c.UserID != user.ID))
                    {
                        Clients.Client(item.Connection).userIsOnline(user.ID);
                    }
                }
            }
            catch (Exception ex)
            { }
        }