Esempio n. 1
0
        public IActionResult Create()
        {
            BasketViewModel basket = new BasketViewModel();
            HttpClient      client = new HttpClient();
            var             result = client.GetAsync("https://localhost:44309/api/basket/1").Result;

            if (result.IsSuccessStatusCode)
            {
                basket = result.Content.ReadAsAsync <BasketViewModel>().Result;
            }
            basket.items = BasketToViewModel.Convert(basket.BasketItems);

            foreach (var item in basket.items)
            {
                result = client.GetAsync("https://localhost:44309/api/product/" + item.ProductId).Result;
                if (result.IsSuccessStatusCode)
                {
                    item.product = result.Content.ReadAsAsync <ProductViewModel>().Result;
                }
            }
            CreateOrderViewModel order = new CreateOrderViewModel()
            {
                User       = basket.User,
                items      = BasketToCreateOrder.Convert(basket.items),
                TotalPrice = basket.items.Select(p => p.product.Price).Sum(),
            };

            if (order.OrderItems == null)
            {
                return(RedirectToAction("Index", "Basket", new { userId = order.User.Id }));
            }

            return(View(order));
        }
Esempio n. 2
0
        public IActionResult AddO(CreateOrderViewModel vm)
        {
            BasketViewModel basket = new BasketViewModel();
            HttpClient      client = new HttpClient();
            var             result = client.GetAsync("https://localhost:44309/api/basket/1").Result;

            if (result.IsSuccessStatusCode)
            {
                basket = result.Content.ReadAsAsync <BasketViewModel>().Result;
            }
            basket.items = BasketToViewModel.Convert(basket.BasketItems);

            foreach (var item in basket.items)
            {
                result = client.GetAsync("https://localhost:44309/api/product/" + item.ProductId).Result;
                if (result.IsSuccessStatusCode)
                {
                    item.product = result.Content.ReadAsAsync <ProductViewModel>().Result;
                }
            }
            CreateOrderViewModel order = new CreateOrderViewModel()
            {
                User       = basket.User,
                items      = BasketToCreateOrder.Convert(basket.items),
                TotalPrice = basket.items.Select(p => p.product.Price).Sum(),
                Comment    = vm.Comment
            };

            order.OrderItems = BasketToCreateOrder.Convert(order.items);
            OrderViewModel model = new OrderViewModel();

            result = client.PostAsJsonAsync("https://localhost:44309/api/order", order).Result;
            if (result.IsSuccessStatusCode)
            {
                model = result.Content.ReadAsAsync <OrderViewModel>().Result;
            }
            return(RedirectToAction("GetOrderById", new { id = model.Id }));
        }