PageRenderTime 48ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/collegeCompanionApp/collegeCompanionApp/Controllers/HomeController.cs

https://bitbucket.org/daniel_tapia/collegecompanion
C# | 813 lines | 433 code | 103 blank | 277 comment | 36 complexity | 682cc2761c97743b25c1ca0095445ca0 MD5 | raw file
  1. using collegeCompanionApp.Models;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Net;
  7. using System.Web;
  8. using System.Web.Mvc;
  9. using System.Web.Script.Serialization;
  10. using collegeCompanionApp.Models.ViewModel;
  11. using System.Diagnostics;
  12. using Newtonsoft.Json.Linq;
  13. using System.Xml;
  14. using System.Data.Linq;
  15. using System.Text;
  16. using collegeCompanionApp.Repository;
  17. using Ninject;
  18. using Geocoding;
  19. using System.Web.UI.WebControls;
  20. using Microsoft.AspNet.Identity;
  21. using System.Web.Http;
  22. using System.Web.Http.Description;
  23. using System.Windows.Forms;
  24. using System.Web.UI;
  25. namespace collegeCompanionApp.Controllers
  26. {
  27. public class HomeController : Controller
  28. {
  29. //Global Parameters
  30. string schoolName = "";
  31. string state = "";
  32. string city = "";
  33. string accreditor = "";
  34. string ownership = "";
  35. string finLimit = "";
  36. string acceptRate = "";
  37. string degree = "";
  38. string degreeType = "";
  39. string isSaved = "";
  40. int collegeCount = 0;
  41. //private Page page;
  42. //Adding in the repository pattern connection
  43. private readonly ICollegeRepository _repository;
  44. public HomeController(ICollegeRepository repo)
  45. {
  46. _repository = repo;
  47. }
  48. //An empty controller so the site will work when not passing in the repository.
  49. public HomeController(){}
  50. /// <summary>
  51. /// Home page of the website. Enables search via javascript: FormSearch.js.
  52. /// Also connects to DegreeQuiz via quizForm.js
  53. /// </summary>
  54. /// <returns>
  55. /// A view.
  56. /// </returns>
  57. public ActionResult Index()
  58. {
  59. return View();
  60. }
  61. /// <summary>
  62. /// DegreeQuiz page of the website. Functionality via quizForm.js
  63. /// </summary>
  64. /// <returns>
  65. /// A view.
  66. /// </returns>
  67. public ActionResult DegreeQuiz()
  68. {
  69. return View();
  70. }
  71. /// <summary>
  72. /// Error page of the website.
  73. /// </summary>
  74. /// <returns>
  75. /// A view.
  76. /// </returns>
  77. public ActionResult Error()
  78. {
  79. return View();
  80. }
  81. /// <summary>
  82. /// Travel page of the website. Enables WalkScore via TravelSearch.js.
  83. /// Also connects to map via googleSearch.js.
  84. /// </summary>
  85. /// <returns>
  86. /// A view.
  87. /// </returns>
  88. public ActionResult Travel()
  89. {
  90. var userName = User.Identity.Name;
  91. var userId = User.Identity.GetUserId();
  92. if (userId == null)
  93. {
  94. return RedirectToAction("Login", "Account");
  95. }
  96. else
  97. {
  98. return View(_repository.GetSavedColleges(userName));
  99. }
  100. }
  101. /// <summary>
  102. /// SearchesMenu page of the website. Enables an accordion via accordion.js.
  103. /// Connects to other pages: Travel, Demographic & Yelp.
  104. /// </summary>
  105. /// <returns>
  106. /// A view.
  107. /// </returns>
  108. public ActionResult SearchesMenu()
  109. {
  110. return View();
  111. }
  112. /// <summary>
  113. /// About page of the website.
  114. /// </summary>
  115. /// <returns>
  116. /// A view.
  117. /// </returns>
  118. public ActionResult About()
  119. {
  120. return View();
  121. }
  122. /// <summary>
  123. /// Contact page of the website.
  124. /// </summary>
  125. /// <returns>
  126. /// A view.
  127. /// </returns>
  128. public ActionResult Contact()
  129. {
  130. return View();
  131. }
  132. /// <summary>
  133. /// DELETE: api/SearchResults/5
  134. /// </summary>
  135. /// <returns>
  136. /// A view.
  137. /// </returns>
  138. [ResponseType(typeof(SearchResult))]
  139. public ActionResult Delete(int id)
  140. {
  141. if (User.Identity.IsAuthenticated)
  142. {
  143. SearchResult searchResult = _repository.FindCollege(id);
  144. if (searchResult == null)
  145. {
  146. Debug.WriteLine("Error for Delete() method.");
  147. return RedirectToAction("SaveDataList", "Home");
  148. }
  149. _repository.DeleteCollege(searchResult);
  150. _repository.SaveCollege(searchResult);
  151. return RedirectToAction("SaveDataList", "Home");
  152. } else
  153. {
  154. return RedirectToAction("Login", "Account");
  155. }
  156. }
  157. /// <summary>
  158. /// SaveDataList page of the website, it presents the list of saved colleges
  159. /// for a given user.
  160. /// </summary>
  161. /// <returns>
  162. /// A view.
  163. /// </returns>
  164. public ActionResult SaveDataList()
  165. {
  166. if (User.Identity.IsAuthenticated)
  167. {
  168. Debug.WriteLine("SaveDataList() Method!");
  169. if (ModelState.IsValid)
  170. {
  171. return View(_repository.GetSavedColleges(User.Identity.Name));
  172. }
  173. else
  174. {
  175. Debug.WriteLine("Error for SaveDataList() method.");
  176. return View();
  177. }
  178. }
  179. else
  180. {
  181. return RedirectToAction("Login", "Account");
  182. }
  183. }
  184. //****************************************************WalkScore*******************************************************//
  185. /// <summary>
  186. /// Searches for ratings of Walking, Transit & Bike for a given city using Walk Score API.
  187. /// </summary>
  188. /// <returns>
  189. /// A "Content" result of JSON String with ratings within the requested city.
  190. /// </returns>
  191. public ActionResult WalkScoreSearch()
  192. {
  193. //Get user input data from Javascript file "TravelSearch.js"
  194. var street = Request.QueryString["addressInput"];
  195. var city = Request.QueryString["cityInput"];
  196. var state = Request.QueryString["stateInput"];
  197. var zipcode = Request.QueryString["zipInput"];
  198. var lat = Request.QueryString["latitude"];
  199. var lon = Request.QueryString["longitude"];
  200. var location = street + city + state + zipcode;
  201. location = location.Replace(" ", "%20");
  202. //User Stringbuilder to make a URL for the request.
  203. StringBuilder sb = new StringBuilder();
  204. sb.Append("http://api.walkscore.com/score?format=json&");
  205. sb.Append("&address=" + location);
  206. sb.Append("&lon=" + lon);
  207. sb.Append("&lat=" + lat);
  208. sb.Append("&transit=1&bike=1");
  209. sb.Append("&wsapikey=");
  210. sb.Append(System.Web.Configuration.WebConfigurationManager.AppSettings["WalkScoreAPIKey"]);
  211. Debug.WriteLine(sb.ToString());
  212. // Make a web request to get the list of classes.
  213. WebRequest request = HttpWebRequest.Create(sb.ToString()); //Takes an arguement of the URL of the webserver being targeted.
  214. // Using a web response process the file returned from the API.
  215. WebResponse response = request.GetResponse();
  216. string resultString;
  217. using (var reader = new StreamReader(response.GetResponseStream()))
  218. {
  219. // Get the data from the stream reader to parse data into usable format
  220. resultString = reader.ReadToEnd();
  221. }
  222. //Note: This is completed without a JSON parse action so don't treat it like the other methods! lol
  223. return Content(resultString, "application/json");
  224. }
  225. //****************************************************College Search*******************************************************//
  226. /// <summary>
  227. /// SearchForm page of the website. This page presents multiple input/selector fields the user
  228. /// can alter to get back a college search result on the SearchResults.cshtml page.
  229. /// </summary>
  230. /// <returns>
  231. /// A view.
  232. /// </returns>
  233. public ActionResult SearchForm()
  234. {
  235. return View(_repository.GetFormData());
  236. }
  237. /// <summary>
  238. /// SearchResults page of the website. This page takes the results of the API call and presents
  239. /// them in a pleasing format via the SearchResults.js file.
  240. /// </summary>
  241. /// <returns>
  242. /// A view.
  243. /// </returns>
  244. public ActionResult SearchResults()
  245. {
  246. return View(_repository.GetAllUsers());
  247. }
  248. /// <summary>
  249. /// Searches for Colleges via an API call.
  250. /// </summary>
  251. /// <returns>
  252. /// A JSON result of colleges within the requested confines.
  253. /// </returns>
  254. public JsonResult Search()
  255. {
  256. Debug.WriteLine("SearchForm() Method!");
  257. //Get Data from Current URL
  258. schoolName = Request.QueryString["school.name"];
  259. state = Request.QueryString["school.state"];
  260. city = Request.QueryString["school.city"];
  261. accreditor = Request.QueryString["school.accreditor"];
  262. ownership = Request.QueryString["school.ownership"];
  263. finLimit = Request.QueryString["school.tuition"];
  264. acceptRate = Request.QueryString["school.admission_rate"];
  265. degree = Request.QueryString["school.degree"];
  266. degreeType = Request.QueryString["school.degreeType"];
  267. // build a WebRequest
  268. WebRequest request = WebRequest.Create(CreateURL(schoolName, state, city, accreditor, ownership, finLimit, acceptRate, degree, degreeType));
  269. WebResponse response = request.GetResponse();
  270. Stream dataStream = response.GetResponseStream();
  271. StreamReader reader = new StreamReader(response.GetResponseStream());
  272. // Read the content.
  273. string responseFromServer = reader.ReadToEnd();
  274. // Clean up the streams and the response.
  275. reader.Close();
  276. response.Close();
  277. // Create a JObject, using Newtonsoft NuGet package
  278. JObject json = JObject.Parse(responseFromServer);
  279. // Create a serializer to deserialize the string response (string in JSON format)
  280. JavaScriptSerializer serializer = new JavaScriptSerializer();
  281. // Store JSON results in results to be passed back to client (javascript)
  282. var data = serializer.DeserializeObject(responseFromServer);
  283. //saveData(schoolName, state, city, accreditor, ownership, finLimit);
  284. //return CollegeSearch(college);
  285. return Json(data, JsonRequestBehavior.AllowGet);
  286. }
  287. /// <summary>
  288. /// Saves selected data into the College Database
  289. /// If the college already exists it throws a pop up error to the user.
  290. /// </summary>
  291. public ActionResult SaveData()
  292. {
  293. int userID = Int32.Parse(Request.QueryString["UserID"]);
  294. string name = Request.QueryString["Name"];
  295. string stateName = Request.QueryString["StateName"];
  296. string city = Request.QueryString["City"];
  297. int zipCode = Int32.Parse(Request.QueryString["ZipCode"]);
  298. string accreditor = Request.QueryString["Accreditor"];
  299. string degree = Request.QueryString["Degree"];
  300. string degreeType = Request.QueryString["DegreeType"];
  301. int ownership = Int32.Parse(Request.QueryString["Ownership"]);
  302. int cost;
  303. int.TryParse(Request.QueryString["Cost"], out cost);
  304. if (_repository.GetCollege(name, userID) == 0) {
  305. SearchResult college = new SearchResult {
  306. CompanionID = userID,
  307. Name = name,
  308. StateName = stateName,
  309. City = city,
  310. ZipCode = zipCode,
  311. Accreditor = accreditor,
  312. Degree = degree,
  313. DegreeType = degreeType,
  314. Ownership = ownership,
  315. Cost = cost
  316. };
  317. if (User.Identity.IsAuthenticated)
  318. {
  319. Debug.WriteLine("saveData() Method!");
  320. if (ModelState.IsValid)
  321. {
  322. collegeCount = _repository.GetSavedColleges(User.Identity.Name).Count();
  323. _repository.AddCollege(college);
  324. _repository.SaveCollege(college);
  325. isSaved = "Not Saved";
  326. return RedirectToAction("SaveDataList", "Home");
  327. }
  328. else
  329. {
  330. Debug.WriteLine("Error for SaveData() method.");
  331. return View();
  332. }
  333. }
  334. else
  335. {
  336. return RedirectToAction("Login","Account");
  337. }
  338. }
  339. else
  340. {
  341. isSaved = "Saved";
  342. return RedirectToAction("SaveDataList", "Home");
  343. }
  344. }
  345. /// <summary>
  346. /// Creats a URL for the API search based on User Input.
  347. /// </summary>
  348. /// <param name="acceptRate">The acceptance rate of the college.</param>
  349. /// <param name="cityName">The city the college is located in.</param>
  350. /// <param name="degree">The degree selected by the user.</param>
  351. /// <param name="accreditor">The accreditory for the school.</param>
  352. /// <param name="degreeType">The type of degree selected by the user.</param>
  353. /// <param name="finLimit">The finanicial limit selected by the user.</param>
  354. /// <param name="ownership">The ownership status of the school: Public, Private For Profit, Private NonProfit.</param>
  355. /// <param name="schoolName">The school's name.</param>
  356. /// <param name="stateName">The school's state of location.</param>
  357. /// <returns>
  358. /// A URL string.
  359. /// </returns>
  360. public string CreateURL(string schoolName, string stateName, string cityName, string accreditor, string ownership, string finLimit, string acceptRate, string degree, string degreeType)
  361. {
  362. Debug.WriteLine("createURL() Method!");
  363. var source = "https://api.data.gov/ed/collegescorecard/v1/schools?"; // Source, Endpoint
  364. var APIKey = "&api_key=" + System.Web.Configuration.WebConfigurationManager.AppSettings["CollegeScoreCardAPIKey"]; // CollegeScoreCard API Key
  365. var fields = SetCollegeFields(); // Set Fields
  366. var values = SetCollegeValues(ownership, acceptRate, finLimit); // Set Values, Parameters
  367. // Checks for Empty Values
  368. // If not empty, add to parameters
  369. if (schoolName != "")
  370. {
  371. values = values + "&school.name=" + schoolName;
  372. }
  373. if (stateName != "")
  374. {
  375. values = values + "&school.state=" + stateName;
  376. }
  377. if (cityName != "")
  378. {
  379. values = values + "&school.city=" + cityName;
  380. }
  381. if (cityName != "")
  382. {
  383. values = values + "&school.city=" + cityName;
  384. }
  385. if (accreditor != "")
  386. {
  387. values = values + "&school.accreditor=" + accreditor;
  388. }
  389. if (degree != "" && degree != "Any")
  390. {
  391. string theDegree = SetDegree(degreeType, degree); // Set up Degree value
  392. values = values + AddDegreeValue(theDegree); // Add Degree to Parameters
  393. fields = fields + AddDegreeField(theDegree); // Add Degree to Fields
  394. }
  395. //Set up GET URL to College Scorecard
  396. string url = source + values + APIKey + fields;
  397. //Replace spaces with %20
  398. url = url.Trim();
  399. url = url.Replace(" ", "%20");
  400. Debug.WriteLine("URL: " + url);
  401. return url;
  402. }
  403. /// <summary>
  404. /// A method to ensure colelge fields are set up correctly in the URL string.
  405. /// </summary>
  406. /// <returns>
  407. /// A string of fields for the URL call.
  408. /// </returns>
  409. public string SetCollegeFields()
  410. {
  411. // Default Fields to get
  412. return "&_fields=school.name,school.state,school.city,school.accreditor,school.ownership,school.tuition_revenue_per_fte,2015.admissions.admission_rate.overall,school.school_url,school.zip&_per_page=100";
  413. }
  414. /// <summary>
  415. /// A method to set selected fields to the entered values of the user.
  416. /// </summary>
  417. /// <param name="acceptRate">The selected acceptance rate.</param>
  418. /// <param name="finLimit">The selected financial limit.</param>
  419. /// <param name="ownership">The selected ownership.</param>
  420. /// <returns>
  421. /// A string for the URL call.
  422. /// </returns>
  423. public string SetCollegeValues(string ownership, string acceptRate, string finLimit)
  424. {
  425. string collegeValues = "school.ownership=" + ownership + "&2015.admissions.admission_rate.overall__range=" + acceptRate
  426. + "&school.tuition_revenue_per_fte__range=" + finLimit + "&_sort=school.tuition_revenue_per_fte:desc"; // Default Parameters
  427. return collegeValues;
  428. }
  429. /// <summary>
  430. /// A method to append the degree type to the degree category in the format the API desires.
  431. /// </summary>
  432. /// <param name="theDegree">The degree category.</param>
  433. /// <returns>
  434. /// A string for the URL call.
  435. /// </returns>
  436. public string AddDegreeField(string theDegree)
  437. {
  438. string field = "," + theDegree; // Add Degree to fields
  439. return field;
  440. }
  441. /// <summary>
  442. /// A method to add a range to the degree is necessary.
  443. /// </summary>
  444. /// <param name="theDegree">Gets the selected type to append the data around.</param>
  445. /// <returns>
  446. /// A string for the URL call.
  447. /// </returns>
  448. public string AddDegreeValue(string theDegree)
  449. {
  450. string val = "&" + theDegree + "__range=1.."; // Add Degree to parameters
  451. return val;
  452. }
  453. /// <summary>
  454. /// A method to set up the degree string to set it.
  455. /// </summary>
  456. /// <param name="degree">The degree field.</param>
  457. /// <param name="degreeType">The degree type.</param>
  458. /// <returns>
  459. /// A string for the URL call.
  460. /// </returns>
  461. public string SetDegree(string degreeType, string degree)
  462. {
  463. string aDegree = "2015.academics.program." + degreeType + "." + degree; // Degree to Search
  464. return aDegree;
  465. }
  466. //****************************************************Yelp*******************************************************//
  467. /// <summary>
  468. /// Yelp page for the website.
  469. /// </summary>
  470. /// <returns>
  471. /// A view.
  472. /// </returns>
  473. public ActionResult Yelp()
  474. {
  475. var userName = User.Identity.Name;
  476. var userId = User.Identity.GetUserId();
  477. if (userId == null)
  478. {
  479. return RedirectToAction("Login", "Account");
  480. }
  481. else
  482. {
  483. return View(_repository.GetSavedColleges(userName));
  484. }
  485. }
  486. /// <summary>
  487. /// A search method for the Yelp API call. This runs via the YelpSearch.js.
  488. /// </summary>
  489. /// <returns>
  490. /// A JSON result for the API call.
  491. /// </returns>
  492. public JsonResult YelpSearch()
  493. {
  494. Debug.WriteLine("YelpSearch() Method!");
  495. //Get Yelp API Key
  496. string YelpAPIKey = System.Web.Configuration.WebConfigurationManager.AppSettings["YelpAPIKey"];
  497. YelpAPIKey = IsAPIKey(YelpAPIKey);
  498. //Get Location
  499. string location = GetLocation(Request.QueryString["location"]);
  500. //Get Term
  501. string term = GetTerm(Request.QueryString["term"]);
  502. //Get IsOpen
  503. string isOpen = Request.QueryString["isOpen"];
  504. //Set Parameters
  505. string param = SetParam(location, term, isOpen);
  506. //URL Endpoint
  507. var url = SetURL(param);
  508. //URL GET Request
  509. Debug.WriteLine("URL: " + url);
  510. // build a WebRequest
  511. WebRequest request = WebRequest.Create(url);
  512. request.Headers.Add("Authorization", "Bearer " + YelpAPIKey);
  513. WebResponse response = request.GetResponse();
  514. Stream dataStream = response.GetResponseStream();
  515. StreamReader reader = new StreamReader(response.GetResponseStream());
  516. // Read the content.
  517. string responseFromServer = reader.ReadToEnd();
  518. // Clean up the streams and the response.
  519. reader.Close();
  520. response.Close();
  521. // Create a JObject, using Newtonsoft NuGet package
  522. JObject json = JObject.Parse(responseFromServer);
  523. // Create a serializer to deserialize the string response (string in JSON format)
  524. JavaScriptSerializer serializer = new JavaScriptSerializer();
  525. // Store JSON results in results to be passed back to client (javascript)
  526. var data = serializer.DeserializeObject(responseFromServer);
  527. return Json(data, JsonRequestBehavior.AllowGet);
  528. }
  529. /// <summary>
  530. /// A simple method to test if something is an API key or not.
  531. /// </summary>
  532. /// <param name="key">The API key being tested.</param>
  533. /// <returns>
  534. /// The API key if successful, or "NoKey" if not.
  535. /// </returns>
  536. public string IsAPIKey(string key)
  537. {
  538. if (key.Length <= 5)
  539. {
  540. key = "NoKey";
  541. }
  542. return key;
  543. }
  544. /// <summary>
  545. /// A simple method to test if a location if being populated or not.
  546. /// </summary>
  547. /// <param name="location">The location being tested.</param>
  548. /// <returns>
  549. /// The location if it is not null, otherwise it throws a debug statement that it is null.
  550. /// </returns>
  551. public string GetLocation(string location)
  552. {
  553. if (location == null)
  554. {
  555. Debug.WriteLine("No Location");
  556. }
  557. return location;
  558. }
  559. /// <summary>
  560. /// A method to set null term strings to empty strings to avoid errors with the API.
  561. /// </summary>
  562. /// <param name="term">The term being tested/converted.</param>
  563. /// <returns>
  564. /// An empty string if the term was null.
  565. /// </returns>
  566. public string GetTerm(string term)
  567. {
  568. if (term == null)
  569. {
  570. term = "";
  571. }
  572. return term;
  573. }
  574. /// <summary>
  575. /// A method to create a parameter string for the geolocation.
  576. /// </summary>
  577. /// <param name="location">The location field.</param>
  578. /// <param name="term">The term field.</param>
  579. /// <returns>
  580. /// The resulting string for the URL.
  581. /// </returns>
  582. public string SetParam(string location, string term, string isOpen)
  583. {
  584. var param = "term=" + term + "&location=" + location + "&limit=12&sort_by=distance&open_now=";
  585. if(isOpen == "Open")
  586. {
  587. param = param + "true";
  588. }
  589. else
  590. {
  591. param = param + "false";
  592. }
  593. return param;
  594. }
  595. /// <summary>
  596. /// A simple method to set the URL for the yelp search.
  597. /// </summary>
  598. /// <param name="param">A parameter upon which the search is run.</param>
  599. /// <returns>
  600. /// The resulting parameter URL string.
  601. /// </returns>
  602. public string SetURL(string param)
  603. {
  604. return "https://api.yelp.com/v3/businesses/search?" + param;
  605. }
  606. //****************************************************Demographic*******************************************************//
  607. /// <summary>
  608. /// The demographic search page. Runs with the DemographicSearch.js.
  609. /// </summary>
  610. /// <returns>
  611. /// A view.
  612. /// </returns>
  613. public ActionResult Demographic()
  614. {
  615. var userName = User.Identity.Name;
  616. var userId = User.Identity.GetUserId();
  617. if (userId == null)
  618. {
  619. return RedirectToAction("Login", "Account");
  620. }
  621. else
  622. {
  623. FormdataDB fd = new FormdataDB();
  624. LifeStyle lifeStyle = _repository.GetLifeStyleVM();
  625. lifeStyle.SearchResults = _repository.GetSavedColleges(userName);
  626. lifeStyle.DemoAges = _repository.GetFormData().DemoAges;
  627. lifeStyle.DemoRaces = _repository.GetFormData().DemoRaces;
  628. return View(lifeStyle);
  629. }
  630. }
  631. /// <summary>
  632. /// The DemographicSearch method to run the API call to Demographics Inquiry.
  633. /// </summary>
  634. /// <returns>
  635. /// A JSON result for the API call.
  636. /// </returns>
  637. public JsonResult DemographicSearch()
  638. {
  639. Debug.WriteLine("DemographicSearch() Method!");
  640. //Get Demographic API Key
  641. string DemoAPIKey = System.Web.Configuration.WebConfigurationManager.AppSettings["DemographicAPIKey"];
  642. //Gets the latitude & longitude
  643. string coordinates = GetCoordinates(Request.QueryString["latitude"], Request.QueryString["longitude"]);
  644. //Set parameters with coordinates & variables
  645. string param = SetDemoParams(coordinates, Request.QueryString["variables"]);// + "/" + variables);
  646. //Endpoint Description Link: https://market.mashape.com/mapfruition/demographicinquiry#inquire-by-point
  647. //Set up url endpoint with parameters
  648. var url = SetDemoURL(param);
  649. //URL GET Request
  650. Debug.WriteLine("JSon URL Call: " + url);
  651. // build a WebRequest
  652. WebRequest request = WebRequest.Create(url);
  653. //Add Header with API Key
  654. request.Headers.Add("X-Mashape-Key", DemoAPIKey);
  655. WebResponse response = request.GetResponse();
  656. Stream dataStream = response.GetResponseStream();
  657. StreamReader reader = new StreamReader(response.GetResponseStream());
  658. // Read the content.
  659. string responseFromServer = reader.ReadToEnd();
  660. // Clean up the streams and the response.
  661. reader.Close();
  662. response.Close();
  663. // Create a JObject, using Newtonsoft NuGet package
  664. JObject json = JObject.Parse(responseFromServer);
  665. // Create a serializer to deserialize the string response (string in JSON format)
  666. JavaScriptSerializer serializer = new JavaScriptSerializer();
  667. // Store JSON results in results to be passed back to client (javascript)
  668. var data = serializer.DeserializeObject(responseFromServer);
  669. return Json(data, JsonRequestBehavior.AllowGet);
  670. }
  671. /// <summary>
  672. /// A method to set up the URL starting parameters being serarched on.
  673. /// </summary>
  674. /// <param name="param">The parameter being appended by the user.</param>
  675. /// <returns>
  676. /// A string for the URL.
  677. /// </returns>
  678. public string SetDemoURL(string param)
  679. {
  680. string url = "https://mapfruition-demoinquiry.p.mashape.com/inquirebypoint/" + param;
  681. return url;
  682. }
  683. /// <summary>
  684. /// A method to put a slash between the coordinates and the variables.
  685. /// </summary>
  686. /// <param name="coordinates">The coordinates provided.</param>
  687. /// <param name="variables">The variables selected.</param>
  688. /// <returns>
  689. /// A string for the URL call.
  690. /// </returns>
  691. public string SetDemoParams(string coordinates, string variables)
  692. {
  693. string param = coordinates + "/" + variables;
  694. return param;
  695. }
  696. /// <summary>
  697. /// A method to get coordinates based on a latitude and longitude.
  698. /// </summary>
  699. /// <param name="lat">The Latitutde</param>
  700. /// <param name="lon">The Longitude</param>
  701. /// <returns>
  702. /// A string for the URL.
  703. /// </returns>
  704. public string GetCoordinates(string lat, string lon)
  705. {
  706. string cord = lat + "," + lon;
  707. return cord;
  708. }
  709. }
  710. }