예제 #1
0
        private JokeResultsModel PackageJokeResults(IList <JokeModel> allJokes)
        {
            JokeResultsModel resultsModel = new JokeResultsModel();

            var shortResults = from j in allJokes
                               where j.Joke.Split(' ').Length < 10
                               select j;

            var mediumResults = from j in allJokes
                                where j.Joke.Split(' ').Length < 20 && j.Joke.Split(' ').Length >= 10
                                select j;

            var longResults = from j in allJokes
                              where j.Joke.Split(' ').Length >= 20
                              select j;

            resultsModel.ShortList  = shortResults.ToList <JokeModel>();
            resultsModel.MediumList = mediumResults.ToList <JokeModel>();
            resultsModel.LongList   = longResults.ToList <JokeModel>();

            return(resultsModel);
        }
예제 #2
0
        public async Task <JokeResultsModel> GetJokesSearch(string term)
        {
            JokeResultsModel resultsModel = new JokeResultsModel();

            using (HttpClient client = new HttpClient()){
                try
                {
                    client.DefaultRequestHeaders.Add("Accept", "application/json");

                    var response = await client.GetAsync(Path.Combine(this._url, $"search?limit=30&term={term}"));

                    string jsonString = await response.Content.ReadAsStringAsync();

                    var model = JsonConvert.DeserializeObject <JokeSearchModel>(jsonString);

                    resultsModel = PackageJokeResults(model.Results);
                }
                catch (Exception e) {
                    // log exceptions
                }
            }

            return(resultsModel);
        }