PageRenderTime 43ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/Controllers/LocationSearchController.cs

https://github.com/mhenderson442/HC_LocalHunch
C# | 51 lines | 40 code | 10 blank | 1 comment | 0 complexity | 5c3d2b967e3a80e35e7273642afe4987 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Net;
  6. using System.Net.Http;
  7. using System.Web.Configuration;
  8. using System.Web.Http;
  9. using HCHunchApp.Web.Models;
  10. using Newtonsoft.Json.Linq;
  11. namespace HCHunchApp.Web.Controllers
  12. {
  13. public class LocationSearchController : ApiController
  14. {
  15. public string Get()
  16. {
  17. return "TEST";
  18. }
  19. // GET api/locationsearch/5
  20. public LocationSearchResultItem Get(string search)
  21. {
  22. var key = WebConfigurationManager.AppSettings["mapKey"];
  23. var location_search_url = WebConfigurationManager.AppSettings["location_search_url"];
  24. var url = string.Format(location_search_url, search, key);
  25. var searchRequest = (HttpWebRequest)WebRequest.Create(url);
  26. searchRequest.ContentType = "application/json; charset=utf-8";
  27. var searchResponse = (HttpWebResponse)searchRequest.GetResponse();
  28. var responseData = new JObject();
  29. var item = new LocationSearchResultItem();
  30. using (var sr = new StreamReader(searchResponse.GetResponseStream()))
  31. {
  32. var data = sr.ReadToEnd();
  33. responseData = JObject.Parse(data);
  34. item.name = (string)responseData["resourceSets"][0]["resources"][0]["name"];
  35. item.lat = (double)responseData["resourceSets"][0]["resources"][0]["point"]["coordinates"][0];
  36. item.lng = (double)responseData["resourceSets"][0]["resources"][0]["point"]["coordinates"][1];
  37. }
  38. return item;
  39. }
  40. }
  41. }