Exemplo n.º 1
0
        public static GymSearchListResponse searchByLocationImplementation(SearchGymRequest request)
        {
            var location = System.Data.Entity.Spatial.DbGeography.FromText(String.Format("POINT({0} {1})", request.longitude, request.latitude));

            GymSearchListResponse rv;

            using (var db = new UniversalGymEntities())
            {
                var searchRequest = new Data.SearchRequest
                {
                    SearchDate             = DateTime.Now,
                    Position               = location,
                    UniqueDeviceIdentifier = request.uniqueDeviceId,
                };

                var user = db.Users.SingleOrDefault(a => a.CurrentToken == request.authToken && a.UserId == request.accountId);
                if (user != null)
                {
                    searchRequest.UserId = user.UserId;
                }


                db.SearchRequests.Add(searchRequest);
                db.SaveChanges();

                var searchText = "DeviceId: "
                                 + request.uniqueDeviceId
                                 + Environment.NewLine;
                if (searchRequest.User != null)
                {
                    searchText = searchText
                                 + "User: "
                                 + searchRequest.User.Email;
                }

                SlackHelper.sendSearchChannel(searchText, request.latitude.ToString(), request.longitude.ToString());


                var gyms = db.Gyms.Where(w => w.Position != null).OrderBy(o => o.Position.Distance(location));
                rv = GymSearchListHelper.createGymSearchListResponse(gyms, location.Latitude, location.Longitude);
                if (user != null)
                {
                    rv.credits = user.Credits;
                }
                else
                {
                    rv.credits = 0;
                }
            }
            return(rv);
        }
Exemplo n.º 2
0
        public static GymSearchListResponse searchGymsByCityImplementation(SearchGymRequest request)
        {
            GymSearchListResponse rv;

            using (var db = new UniversalGymEntities())
            {
                var searchRequest = new Data.SearchRequest
                {
                    SearchDate             = DateTime.Now,
                    UniqueDeviceIdentifier = request.uniqueDeviceId,
                };

                if (request.longitude != 0 && request.latitude != 0)
                {
                    searchRequest.Position = System.Data.Entity.Spatial.DbGeography.FromText(String.Format("POINT({0} {1})", request.longitude, request.latitude));
                }



                if (!request.city.Equals("NULL"))
                {
                    searchRequest.Request = request.city;
                }

                if (!request.state.Equals("NULL"))
                {
                    searchRequest.Request = searchRequest.Request + " " + request.state ?? "NO STATE";
                }

                var user = db.Users.SingleOrDefault(a => a.CurrentToken == request.authToken && a.UserId == request.accountId);
                if (user != null)
                {
                    searchRequest.UserId = user.UserId;
                }


                db.SearchRequests.Add(searchRequest);
                db.SaveChanges();



                // convert city, state to lat, long
                var target   = searchRequest.Request;
                var geocoded = Geocoder.GeoCodeAddress(target);
                if (geocoded != null)
                {
                    var cityLocation = System.Data.Entity.Spatial.DbGeography.FromText(
                        String.Format("POINT({0} {1})",
                                      geocoded.Longitude,
                                      geocoded.Latitude));


                    var gyms = db.Gyms.Where(w => w.Position != null).OrderBy(o => o.Position.Distance(cityLocation));
                    rv = GymSearchListHelper.createGymSearchListResponse(gyms, cityLocation.Latitude, cityLocation.Longitude);
                    if (user != null)
                    {
                        rv.credits = user.Credits;
                    }
                    else
                    {
                        rv.credits = 0;
                    }

                    var searchText = "DeviceId: "
                                     + request.uniqueDeviceId
                                     + Environment.NewLine;
                    if (searchRequest.User != null)
                    {
                        searchText = searchText
                                     + "User: "******"Request: "
                                 + searchRequest.Request;

                    SlackHelper.sendSearchChannel(searchText, request.latitude.ToString(), request.longitude.ToString());
                }
                else
                {
                    var searchText = "";
                    if (searchRequest.User != null)
                    {
                        searchText = searchText
                                     + "User: "******"Request: "
                                 + searchRequest.Request;

                    SlackHelper.sendSearchChannelNotFound(searchText);

                    rv = new GymSearchListResponse {
                        success = false,
                        status  = 400,
                        message = "Could not find location"
                    };
                    if (user != null)
                    {
                        rv.credits = user.Credits;
                    }
                    else
                    {
                        rv.credits = 0;
                    }
                }
            }
            return(rv);
        }