Esempio n. 1
0
        public ActionResult GetAds( AdRequest adRequest )
        {
            string connectionString = ConfigurationManager.AppSettings["ConnectionString"];
            NpgsqlConnection conn = new NpgsqlConnection(connectionString);
            // DataReader to read database record.
            NpgsqlDataReader reader = null;

            string pointToGeography = string.Format(@"ST_GeomFromText('POINT({0} {1})', 4326)::geography", adRequest.Lon, adRequest.Lat);

            NpgsqlCommand command = new NpgsqlCommand(string.Format(@"SELECT * FROM ads WHERE ST_DWithin( the_geog, {0}, radius)", pointToGeography));

            command.Connection = conn;

            try
            {
                conn.Open();

                reader = command.ExecuteReader();

                List<AdRequestInfo> adRequestInfoList = new List<AdRequestInfo>();
                while (reader.Read())
                {
                    adRequestInfoList.Add(new AdRequestInfo()
                    {
                        Name = reader.GetString(reader.GetOrdinal("name")),
                        Description = reader.GetString(reader.GetOrdinal("description"))
                    });
                }

                reader.Close();

                if (adRequestInfoList.Count > 0)
                {
                    var jsonSerializer = new JavaScriptSerializer();
                    var json = jsonSerializer.Serialize(adRequestInfoList);

                    return new WebResult(json);
                }

            }
            catch (NpgsqlException e)
            {
                return new WebResult(new ResultInfo() { GreatSuccess = false, Message = e.Message });
            }
            finally
            {
                conn.Close();
            }

            return new WebResult(new ResultInfo() { GreatSuccess = false });
        }
        public override object BindForModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            var request = controllerContext.HttpContext.Request;

            StreamReader r = new StreamReader(request.InputStream);
            string s = r.ReadToEnd();

            var queryParams = HttpUtility.ParseQueryString(s);

            AdRequest a = new AdRequest();
            a.Lat = float.Parse(queryParams["lat"]);
            a.Lon = float.Parse(queryParams["lon"]);

            return a;
        }