PizzaGenerator

mail@pastecode.io avatar
unknown
csharp
a year ago
2.3 kB
6
Indexable
Never
using Newtonsoft.Json;
using System;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace Spindox.Vodafone.Monet.ConsoleTest
{
    public class Pizza
    {
        public string Base { get; set; } = "Margherita";
        public string[] Ingredients { get; set; }

        public Pizza(string[] ingredients)
        {
            Ingredients = ingredients;
        }

    }

    class Program
    {
        static async Task Main(string[] args)
        {
            string[] pizzaIngredients;

            bool roastedPotatoes = await CheckRoastedPotatoes();

            pizzaIngredients = roastedPotatoes
                ? new string[] { "Wurstel", "Roasted Potatoes" }
                : new string[] { "Wurstel", "Black Olives" };

            var alePizza = new Pizza(pizzaIngredients);
            await SendToMatte(alePizza);
        }

        static async Task<bool> CheckRoastedPotatoes()
        {
            using var client = new HttpClient();
            string url = "PizzeriaWebService";
            var response = await client.GetAsync(url);

            if (response.IsSuccessStatusCode)
            {
                var responseContent = await response.Content.ReadAsStringAsync();
                return responseContent.ToLower() == "true";
            }
            else
            {
                Console.WriteLine("Error calling the web service.");
                return false;
            }
        }
        static async Task SendToMatte(Pizza pizza)
        {
            using (HttpClient client = new HttpClient())
            {
                string orderServiceUrl = "MatteWebService";

                var orderContent = new StringContent($"{{\"type\":\"{pizza.Base}\",\"ingredients\":{JsonConvert.SerializeObject(pizza.Ingredients)}}}",
                    Encoding.UTF8, "application/json");

                HttpResponseMessage response = await client.PostAsync(orderServiceUrl, orderContent);

                if (response.IsSuccessStatusCode)
                    Console.WriteLine("Order placed successfully!");
                else
                    Console.WriteLine("Error placing the order.");
            }
        }
    }
}