コード例 #1
0
        static void Main(string[] args)
        {
            // In order to run this application you need to start first the Web application

            string token = UserLogin().Result;

            if (token == null)
            {
                return;
            }

            var basketItemModel1 = new BasketItemCreateViewModel
            {
                ProductId     = 4,
                Quantity      = 3,
                PriceInPounds = 5
            };

            var basketItemModel2 = new BasketItemCreateViewModel
            {
                ProductId     = 8,
                Quantity      = 1,
                PriceInPounds = 2
            };

            AddItemToBasket(token, basketItemModel1).Wait();
            int basketItemId = AddItemToBasket(token, basketItemModel2).Result;

            ChangeQuantity(token, basketItemId, 6).Wait();

            GetBasket(token).Wait();

            DeleteBasketItem(token, 1).Wait();

            GetBasket(token).Wait();

            ClearAllItems(token).Wait();

            GetBasket(token).Wait();

            Console.ReadLine();
        }
コード例 #2
0
        public static async Task <int> AddItemToBasket(string token, BasketItemCreateViewModel basketItemModel)
        {
            using (var client = new HttpClient())
            {
                Console.WriteLine("Adding item to basket");

                client.BaseAddress = new Uri($"{BaseUrl}/api/basket/items");
                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
                HttpResponseMessage response = await client.PostAsJsonAsync("", basketItemModel);

                if (!response.IsSuccessStatusCode)
                {
                    Console.WriteLine("An error has occurred");
                    return(0);
                }

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

                var objResult = JObject.Parse(result);
                return(objResult["id"].Value <int>());
            }
        }