public bool addUserLocation(string userId, string windowsPhoneId, string latitude, string longitude,string code)
        {
            string firstCode = codeServices.findFirstCode();
            double lat, lng;
            int idOfUser = 0;
            UserLocations userLocation = new UserLocations();
            DateTime requestTime;
            requestTime = DateTime.Now;

            latitude = latitude.Replace(',','.');
            longitude = longitude.Replace(',', '.');

            if (code == firstCode)
            {
                lat = serializer.Deserialize<double>(latitude);
                lng = serializer.Deserialize<double>(longitude);
                idOfUser = serializer.Deserialize<int>(userId);
                userLocation.latitude = lat;
                userLocation.longitude = lng;
                userLocation.requestTime = requestTime;
                userLocation.windowsPhoneId = windowsPhoneId;
                userLocationServices.addUserLocationOrUpdatetime(userLocation);

                return true;
            }

            return false;
        }
        public List<UserLocations> findUserByUserLocationAndDistance(UserLocations userLocation, double distance)
        {
            List<UserLocations> usersLocation = (from t in db.userLocation
                                                 where (t.latitude <= userLocation.latitude + distance && t.latitude >= userLocation.latitude - distance &&
                                                 t.longitude <= userLocation.longitude + distance && t.longitude >= userLocation.longitude - distance)
                                                 select t).ToList();

            return usersLocation;
        }
 public void addUserLocationOrUpdatetime(UserLocations userLocation)
 {
     bool foundUserByWindowsPhone = false;
     foundUserByWindowsPhone = findUserLocationByWindowsPhoneId(userLocation.windowsPhoneId);
     if (foundUserByWindowsPhone == true)
     {
         userLocation = getUserLocationByWindowsPhoneId(userLocation.windowsPhoneId);
         userLocation.requestTime = DateTime.Now;
         db.SaveChanges();
     }
     else
     {
         addUserLocation(userLocation);
     }
 }
        public string findClosestUserLocationByUserLocation(string userLocationJson ,string code)
        {
            UserLocations userLocation = new UserLocations();
            UserLocations userLocationMin = new UserLocations();
            userLocation = serializer.Deserialize<UserLocations>(userLocationJson);
            double distanceMin = 0 ,distanceAux = 0;
            string talk="";
            string audioString = "";

            double distance = 1;

            List<UserLocations> usersLocation = userLocationServices.findUserByUserLocationAndDistance(userLocation, distance);

            if (usersLocation.Count > 0)
            {
                userLocationMin = usersLocation.First();
                foreach(UserLocations user in usersLocation)
                {
                    distanceAux = distanceTo(userLocation.latitude, userLocation.longitude, user.longitude, user.latitude);
                    if (distanceAux < distanceMin)
                    {
                        distanceMin = distanceAux;
                        userLocationMin = user;
                    }
                }

                if(distanceMin == 0)
                {
                    talk = "There are no close users";
                }
                else
                {
                    talk = "The closest user is " + distanceMin.ToString() + " meters";
                }

                audioString = speechWebServices.talkThis(talk,"wonders");

                return audioString;

            }

            return "";
        }
 public void findClosestUserLocationByUserLocationTest()
 {
     List<UserLocations> list = new List<UserLocations>();
     UserLocations user = new UserLocations();
     user.latitude = 10;
     user.longitude = 20;
     user.id = 3;
     user.userId = 3;
     user.windowsPhoneId = "3";
     iCodeServices.Setup(m => m.findFirstCode()).Returns("1");
     iUserLocationServices.Setup(m => m.findUserByUserLocationAndDistance(It.IsAny<UserLocations>(), It.IsAny<double>())).Returns(list);
     iCommentServices.Setup(m => m.findCommentByIdComment(It.IsAny<int>()));
     UserLocationWebServicesController target = new UserLocationWebServicesController(iCodeServices.Object, iUserLocationServices.Object, iCommentServices.Object);
     string userLocationJson = serializer.Serialize(user);
     string code = "1";
     string actual;
     actual = target.findClosestUserLocationByUserLocation(userLocationJson, code);
     Assert.AreEqual("", actual);
     user.latitude = 15;
     list.Add(user);
     iUserLocationServices.Setup(m => m.findUserByUserLocationAndDistance(It.IsAny<UserLocations>(), It.IsAny<double>())).Returns(list);
     target = new UserLocationWebServicesController(iCodeServices.Object, iUserLocationServices.Object, iCommentServices.Object);
     actual = target.findClosestUserLocationByUserLocation(userLocationJson, code);
     Assert.IsNotNull(actual);
 }
 public void addUserLocation(UserLocations userLocation)
 {
     db.userLocation.Add(userLocation);
     db.SaveChanges();
 }
 public void sendEmailForFolloUserByWindowsPhoneId(string windowsPhoneId,string email)
 {
     UserLocations userLocation = userLocationServices.findUserLocationByPhoneId(windowsPhoneId);
     if (userLocation == null)
     {
         userLocation = new UserLocations();
         userLocation.latitude = 0.0;
         userLocation.longitude = 0.0;
         userLocation.windowsPhoneId = windowsPhoneId;
         userLocation.requestTime = DateTime.Now;
         userLocationServices.addUserLocation(userLocation);
         userLocation = userLocationServices.findUserLocationByPhoneId(windowsPhoneId);
     }
     EmailController emailController = new EmailController();
     userLocation.hash = cryptographyController.getMD5Hash(userLocation.id.ToString());
     userLocationServices.updateHasById(userLocation.id, userLocation.hash);
     emailController.sendEmailToFollowUser(email, userLocation.hash);
 }