/// <summary> /// Gets facilities based around a given zipcode. /// </summary> /// <param name="client">Object for making HTTP requests.</param> /// <param name="result">Object holding the response of the HTTP request.</param> /// <param name="zip">the arguments for the zipcode based API request. Ex. time, proximity, etc...</param> /// <returns>Key-Value paris of the returned TeeTimes by Facility.</returns> private static IDictionary <string, string> GetFacilityByZip(HttpClient client, HttpResponseMessage result, TeesByZipArgs zip) { //Call the API with the args. result = client.GetAsync(ENDPOINT + $"/channel/{CHANNEL_ID}/facilities?q=postal-code&postal-code={zip.ZipCode}&proximity={zip.Proximity}&minplayercount={zip.NumOfPlayers}&fields=ID,name").Result; //Parse response into C# objects. FacilitiesResponse facilities = new FacilitiesResponse(JsonConvert.DeserializeObject <List <Facility> >(result.Content.ReadAsStringAsync().Result)); //Send facilites to be searched for tee times. Return K/V pair array of results. return(getTeeTimesByFacilities(client, result, facilities, zip.Time, zip.NumOfPlayers)); }
/// <summary> /// Gets facilities based around a given city, state(optional), and country (optional). /// </summary> /// <param name="client">Object for making HTTP requests.</param> /// <param name="result">Object holding the response of the HTTP request.</param> /// <param name="zip">the arguments for the city based API request. Ex. time, proximity, etc...</param> /// <returns>Key-Value paris of the returned TeeTimes by Facility.</returns> private static IDictionary <string, string> GetFacilityByCity(HttpClient client, HttpResponseMessage result, TeesByCityArgs city) { //All cities that could match the one entered by the user. List <City> possibleMatchs = getCityInfo(client, result, city); FacilitiesResponse facilities = new FacilitiesResponse(); //Iterate through the potentially matching cities and gather combine eligable facilites. foreach (City possibleCity in possibleMatchs) { result = client.GetAsync(ENDPOINT + $"/channel/{CHANNEL_ID}/facilities?q=country-city-state&country-code={possibleCity.CountryCode}&state-province-code={possibleCity.StateCode}&city={possibleCity.CityName}&players={city.NumOfPlayers}&fields=ID,name").Result; facilities.Facilities = facilities.Facilities.Concat(JsonConvert.DeserializeObject <List <Facility> >(result.Content.ReadAsStringAsync().Result)).ToList(); } //Send facilites to be searched for tee times. Return K/V pair array of results. return(getTeeTimesByFacilities(client, result, facilities, city.Time, city.NumOfPlayers)); }
/// <summary> /// Searches a list of given facilites for tee time options. /// </summary> /// <param name="client">Object for making HTTP requests.</param> /// <param name="result">Object holding the response of the HTTP request.</param> /// <param name="facilities">The list of facilities to be searched.</param> /// <returns>Key-Value paris of the returned TeeTimes by Facility.</returns> private static IDictionary <string, string> getTeeTimesByFacilities(HttpClient client, HttpResponseMessage result, FacilitiesResponse facilities, string time, string players) { IDictionary <string, string> rateOptions = new Dictionary <string, string>(); //If no facilities, return blank object. if (facilities.Facilities.Count == 0) { return(rateOptions); } DateTime scheduleTime = Convert.ToDateTime(time); //If the time is in the past assume they ment tomorrow. while (scheduleTime < DateTime.Now) { scheduleTime = scheduleTime.AddHours(24); } //Define a range of time to search. string localDateMin = scheduleTime.AddMinutes(-15).ToLocalTime().ToString(); string localDateMax = scheduleTime.AddHours(2).ToLocalTime().ToString(); result = client.GetAsync(ENDPOINT + Uri.EscapeUriString($"/channel/{CHANNEL_ID}/facilities/tee-times?q=multi-facilities&facilityids={string.Join("|", facilities.GetIDs())}&date-min={localDateMin}&date-max={localDateMax}&take=3")).Result; TeeTimesResponse teeTimesResponse = JsonConvert.DeserializeObject <TeeTimesResponse>(result.Content.ReadAsStringAsync().Result); //The text to be displayed to the user. string output = ""; //Keep track of which option is next. int optionNumb = 1; for (int i = 0; i < teeTimesResponse.TeeTimes.Count; i++) { //Look for the name of the facility to match the known ID. foreach (Facility facility in facilities.Facilities) { if (facility.ID == teeTimesResponse.TeeTimes[i].FacilityID) { output += facility.Name + " " + teeTimesResponse.TeeTimes[i].Time.ToLocalTime() + " : - "; } } //Display various rates. for (int x = 0; x < 4 && x < teeTimesResponse.TeeTimes[i].Rates.Count; x++) { TeeTime teeTime = teeTimesResponse.TeeTimes[i]; Rate rate = teeTime.Rates[x]; output += $"(#{optionNumb}){rate.RateName}-${rate.SinglePlayerPrice.GreensFees.Value}{rate.SinglePlayerPrice.GreensFees.CurrencyCode}"; rateOptions.Add("rateOption" + optionNumb.ToString(), JsonConvert.SerializeObject(new TeeTimeRateSelection(teeTime.FacilityID, rate.TeeTimeRateID))); if (x < 3 && x < (teeTime.Rates.Count - 1)) { output += " | "; } else { output += " - "; } optionNumb++; } } if (teeTimesResponse.TeeTimes.Count > 0) { output += "Enter the number of a suitable option."; } rateOptions.Add("players", players); rateOptions.Add("output", output); return(rateOptions); }