コード例 #1
0
        public async Task <Joke> GetJokeById(int id)
        {
            // the framework will concatenate the base address with path provided
            HttpResponseMessage response = await _client.GetAsync($"joke/{id}");

            if (!response.IsSuccessStatusCode)
            {
                return(null);
            }

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

            Joke result = JsonConvert.DeserializeObject <Joke>(content);

            return(result);
        }
コード例 #2
0
        public async Task <Joke> UpdateJoke(Joke joke)
        {
            string              data     = JsonConvert.SerializeObject(joke);
            StringContent       content  = new StringContent(data, Encoding.Unicode, Format.Json);
            HttpResponseMessage response = await _client.PutAsync("joke", content);

            if (!response.IsSuccessStatusCode)
            {
                return(null);
            }

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

            Joke result = JsonConvert.DeserializeObject <Joke>(created);

            return(result);
        }
コード例 #3
0
        private static void ShowJoke()
        {
            Console.WriteLine("Number?");
            string index = Console.ReadLine();
            int    jokeId;
            bool   isNumber = int.TryParse(index, out jokeId);

            if (isNumber)
            {
                Joke joke = _service.GetJokeById(jokeId).Result;
                if (joke != null)
                {
                    Console.WriteLine($"({joke.Id}) - {joke.Title}");
                    Console.WriteLine($"{joke.Question}");
                    Console.WriteLine($"{joke.Answer}");
                    Console.WriteLine("-----------------------");
                    Console.WriteLine("Please enter a command: ");
                    Console.WriteLine("1) Edit");
                    Console.WriteLine("2) Delete");
                    Console.WriteLine("Any key to return to menu");
                    string command = Console.ReadLine();
                    switch (command)
                    {
                    case "1":
                        EnterJoke(joke);
                        break;

                    case "2":
                        DeleteJoke(joke.Id);
                        break;
                    }
                }
                else
                {
                    Console.WriteLine("Joke not found");
                }
            }
            else
            {
                Console.WriteLine("Joke not found");
            }
        }