public void Execute()
        {
            using(var db = new PartyAdvisorEntities())
            {
                //latest position of the user
                var groupedPositions = (from p in db.Positions
                                        group p by p.UserId into grp
                                        select grp.OrderByDescending(g => g.Date).FirstOrDefault());

                var genderList = (from pp in db.PositionPlaces
                                  join p in groupedPositions on pp.PositionId equals p.Id
                                  join u in db.Users on p.UserId equals u.Id
                                  where pp.PlaceId == Args.PlaceId
                                  && p.Date > DbFunctions.AddMinutes(Args.Date, -Args.MinutesDifference)
                                  group u by u.Gender into g
                                  select new
                                  {
                                      name = g.Key,
                                      count = g.Count()
                                  }
                              ).ToDictionary(x => x.name, x => x.count);

                Results = new GetGenderStatsCommandResults()
                {
                    MaleUsers = genderList.ContainsKey(Const.MaleString) ? genderList[Const.MaleString] : 0,
                    FemaleUsers = genderList.ContainsKey(Const.FemaleString) ? genderList[Const.FemaleString] : 0,
                    MinutesRequest = Args.MinutesDifference
                };
            }
        }
        public void Execute()
        {
            using (var db = new PartyAdvisorEntities())
            {
                //not authentified user
                if(!db.Users.Any(x => x.FacebookToken == Args.AccessToken))
                {
                    //TODO add some logs here;

                    ILog logger = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
                    logger.Warn(FormatHelper.FormatCommandToLog(this, Args));

                    Result = new List<PlaceViewModel>();
                    return;
                }

                Result = db.Places.Where(x=>x.Published).OrderBy(x=>x.Order).Select(x => new PlaceViewModel()
                {
                    Id = x.Id,
                    Name = x.Name,
                    Latitude = x.Latitude,
                    Longitude = x.Longitude,
                    Address = x.Address,
                    ImagePath = x.ImagePath,
                    FacebookId = x.FacebookId
                }).ToList();
            }
        }
 public void Execute()
 {
     using(var db = new PartyAdvisorEntities())
     {
         Result = db.Places.Max(x => x.DateUpdated);
     }
 }
 //this function should be executed ONLY when new places are added to the database
 public static void RefreshPlaces()
 {
     lock (syncRoot)
     {
         using (var db = new PartyAdvisorEntities())
         {
             _places = db.Places.ToList();
         }
     }
 }
        public void Execute()
        {
            List<int> userIds = new List<int>();

            using (var db = new PartyAdvisorEntities())
            {
                var user = db.Users.SingleOrDefault(x => x.FacebookToken == Args.AccessToken);

                //request from an unauthentified user
                if(user == null)
                {
                    //TODO insert log here

                    ILog logger = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
                    logger.Warn(FormatHelper.FormatCommandToLog(this, Args));

                    Results = new List<SearchUserViewModel>();
                    return;
                }

                var place = db.Places.Find(Args.PlaceId);

                var placePosition = new TorontoPartyAdvisor.Commands.Helpers.MathPosition(place.Latitude, place.Longitude);

                //filter by time
                var userPositions = db.Positions.Where(x => DbFunctions.AddMinutes(x.Date, -(Args.Minutes)) > DateTimeOffset.Now).ToList();

                //filter by distance
                foreach(var userPosition in userPositions)
                {
                    var userMathPosition = new TorontoPartyAdvisor.Commands.Helpers.MathPosition(userPosition.Latitude, userPosition.Longitude);
                    if (placePosition.HasPositionInItsRadius(userMathPosition, place.RadiusMeters))
                    {
                        if (!userIds.Contains(userPosition.UserId))
                        {
                            userIds.Add(userPosition.UserId);
                        }
                    }
                }

                //we get the list of id of users at the place, now we get more infos

                var res = (from x in userIds
                           join y in db.Users on x equals y.Id
                           select new SearchUserViewModel()
                           {
                               Id = y.Id,
                               Gender = y.Gender
                           }).ToList();

                Results = res;
                return;
            }
        }
        public void Execute()
        {
            using(var db = new PartyAdvisorEntities())
            {
                var fbUser = db.Users.SingleOrDefault(x => x.FacebookId == Args.FacebookId);

                //User already exists
                if (fbUser != null)
                {
                    fbUser.FacebookToken = Args.FacebookAccessToken;
                    fbUser.LastLoginDate = DateTimeOffset.Now;
                    db.Entry(fbUser).State = System.Data.Entity.EntityState.Modified;
                    db.SaveChanges();
                }

                else
                {
                    //new user
                    var user = new User()
                    {
                        FirstName = Args.FirstName,
                        LastName = Args.LastName,
                        Gender = Args.Gender,
                        FacebookToken = Args.FacebookAccessToken,
                        Email = Args.Email,
                        FacebookId = Args.FacebookId,
                        Timezone = Args.TimeZone,
                        Locale = Args.Locale,
                        DateCreated = DateTimeOffset.Now,
                        LastLoginDate = DateTimeOffset.Now,
                        Points = 0
                    };

                    db.Entry(user).State = System.Data.Entity.EntityState.Added;
                    db.SaveChanges();

                }

                Result = new SuccessBox();
                Result.AddData("AccessToken", Args.FacebookAccessToken);
            }
        }
        public void Execute()
        {
            using(var db = new PartyAdvisorEntities())
            {
                //no user authentified
                if (!db.Users.Any(x => x.FacebookToken == Args.AccessToken))
                {
                    //TODO: add log here

                    ILog logger = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
                    logger.Warn(FormatHelper.FormatCommandToLog(this, Args));

                    Results = new List<RankingUserViewModel>();
                    return;
                }

                var dbResults = db.Users.OrderByDescending(x => x.Points).Take(Args.NumberResults).Select(x => new RankingUserViewModel()
                {
                    FirstName = x.FirstName,
                    LastName = x.LastName,
                    FacebookId = x.FacebookId,
                    Score = x.Points
                }).ToList();

                if(dbResults.Count < Args.NumberResults)
                {
                    var nbResToAdd = Args.NumberResults - dbResults.Count;

                    for(int i=0;i<nbResToAdd;i++)
                    {
                        dbResults.Add(new RankingUserViewModel()
                        {

                        });
                    }
                }

                Results = dbResults;
            }
        }
        public void Execute()
        {
            using (var db = new PartyAdvisorEntities())
            {
                var user = db.Users.SingleOrDefault(x => x.FacebookToken == AccessTokenArgs);

                if(user == null)
                {
                    Results = null;
                    return;
                }

                Results = new GetUserInfoCommandResults()
                {
                    FacebookId = user.FacebookId,
                    FirstName = user.FirstName,
                    LastName = user.LastName,
                    Points = user.Points,
                    Gender = user.Gender
                };
            }
        }