public async Task <ActionResult> PostCustomOrder(int numPizzas)
        {
            var sessionOrder = UtilsSession.GetCurrentOrder(HttpContext.Session);


            sessionOrder.OrderDetails.Last().NumberOfPizzas = numPizzas;
            if (numPizzas < 1)
            {
                TempData["error"] = "your order must have at least 1 pizza";
                return(RedirectToAction("ChoosePizza", sessionOrder.OrderDetails.Last().PizzaId));
            }
            if (sessionOrder.OrderDetails.First().getPrice() > 250)
            {
                TempData["error"] = "your order must have less than 250";
                return(RedirectToAction("ChoosePizza", sessionOrder.OrderDetails.Last().PizzaId));
            }


            UtilsSession.SaveOrder(HttpContext.Session, sessionOrder);

            var json = JsonConvert.SerializeObject(sessionOrder);
            var data = new StringContent(json, Encoding.UTF8, "application/json");

            using var client = new HttpClient();
            var response = await client.PostAsync(postUrl, data);

            var result = response.Content.ReadAsStringAsync().Result;

            UtilsSession.Clear(HttpContext.Session);

            return(Redirect("Index"));
        }
        public ActionResult SelectCustomPizza(int id)
        {
            Customer      customer = null;
            IList <Pizza> pizzas   = null;

            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri(CustomerUrl + id);
                var response = client.GetAsync("");
                response.Wait();
                var result = response.Result;
                if (result.IsSuccessStatusCode)
                {
                    var readTask = result.Content.ReadAsAsync <Customer>();
                    readTask.Wait();

                    customer = readTask.Result;
                }
                else
                {
                    customer = null;
                    ModelState.AddModelError(string.Empty, "Server error, Get Stores is empty");
                }
            }
            var sessionOrder = UtilsSession.GetCurrentOrder(HttpContext.Session);

            sessionOrder.Customer = customer;
            if (sessionOrder.CustomerId == 0)
            {
                sessionOrder.CustomerId = id;
            }

            UtilsSession.SaveOrder(HttpContext.Session, sessionOrder);

            ViewBag.ErrorMessage = TempData["ErrorMessage"];



            Pizza pizza = UtilsSession.GetCustomPizza(HttpContext.Session);

            pizza.PizzaName = "custom";

            UtilsSession.SavePizza(HttpContext.Session, pizza);


            if (pizza.Sauce != null)
            {
                ViewBag.SauceName  = pizza.Sauce.Name;
                ViewBag.SaucePrice = pizza.Sauce.Price;
            }

            pizzas = new List <Pizza>();
            pizzas.Add(pizza);

            return(View(pizzas));
        }
        public ActionResult Order(int id)
        {
            Store store = null;
            IEnumerable <Customer> customers = null;

            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri(StoreUrl + id);
                var response = client.GetAsync("");
                response.Wait();
                var result = response.Result;
                if (result.IsSuccessStatusCode)
                {
                    var readTask = result.Content.ReadAsAsync <Store>();
                    readTask.Wait();

                    store = readTask.Result;
                }
                else
                {
                    store = null;
                    ModelState.AddModelError(string.Empty, "Server error, Get Stores is empty");
                }
            }

            var sessionOrder = UtilsSession.GetCurrentOrder(HttpContext.Session);

            sessionOrder.Store   = store;
            sessionOrder.StoreId = store.Id;
            sessionOrder.Date    = DateTime.Now;
            UtilsSession.SaveOrder(HttpContext.Session, sessionOrder);

            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri(CustomerUrl);
                var response = client.GetAsync("");
                response.Wait();
                var result = response.Result;
                if (result.IsSuccessStatusCode)
                {
                    var readTask = result.Content.ReadAsAsync <IList <Customer> >();
                    readTask.Wait();

                    customers = readTask.Result;
                }
                else
                {
                    customers = Enumerable.Empty <Customer>();
                    ModelState.AddModelError(string.Empty, "Server error, Get Customers is empty");
                }
            }
            return(View(customers));
        }
        public ActionResult SelectPizza(int id)
        {
            Customer            customer = null;
            IEnumerable <Pizza> pizzas   = null;

            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri(CustomerUrl + id);
                var response = client.GetAsync("");
                response.Wait();
                var result = response.Result;
                if (result.IsSuccessStatusCode)
                {
                    var readTask = result.Content.ReadAsAsync <Customer>();
                    readTask.Wait();

                    customer = readTask.Result;
                }
                else
                {
                    customer = null;
                    ModelState.AddModelError(string.Empty, "Server error, Get Stores is empty");
                }
            }
            var sessionOrder = UtilsSession.GetCurrentOrder(HttpContext.Session);

            sessionOrder.Customer   = customer;
            sessionOrder.CustomerId = customer.Id;
            UtilsSession.SaveOrder(HttpContext.Session, sessionOrder);

            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri(PizzaUrl);
                var response = client.GetAsync("");
                response.Wait();
                var result = response.Result;
                if (result.IsSuccessStatusCode)
                {
                    var readTask = result.Content.ReadAsAsync <IList <Pizza> >();
                    readTask.Wait();

                    pizzas = readTask.Result;
                }
                else
                {
                    pizzas = Enumerable.Empty <Pizza>();
                    ModelState.AddModelError(string.Empty, "Server error, Get Customers is empty");
                }
            }
            return(View(pizzas));
        }
        public ActionResult ChoosePizza(int id)
        {
            Pizza pizza = null;

            ViewBag.PresetTotalError = TempData["error"];
            if (ViewBag.PresetTotalError == null)
            {
                using (var client = new HttpClient())
                {
                    client.BaseAddress = new Uri(PizzaUrl + id);
                    var response = client.GetAsync("");
                    response.Wait();
                    var result = response.Result;
                    if (result.IsSuccessStatusCode)
                    {
                        var readTask = result.Content.ReadAsAsync <Pizza>();
                        readTask.Wait();

                        pizza = readTask.Result;
                        System.Console.WriteLine("Pizza Retrieved!");
                    }
                    else
                    {
                        pizza = null;
                        System.Console.WriteLine("Pizza Not Retrieved!");
                        ModelState.AddModelError(string.Empty, "Server error, Get pizza is empty");
                    }
                }
                var         sessionOrder = UtilsSession.GetCurrentOrder(HttpContext.Session);
                OrderDetail detail       = new OrderDetail();
                detail.Pizza   = pizza;
                detail.PizzaId = id;

                detail.OrderNumberNavigation = sessionOrder;

                if (sessionOrder.OrderDetails == null)
                {
                    sessionOrder.OrderDetails = new List <OrderDetail>();
                }

                sessionOrder.OrderDetails.Add(detail);
                UtilsSession.SaveOrder(HttpContext.Session, sessionOrder);
            }


            return(View());
        }