コード例 #1
0
        internal static async Task<string> UpdateCartItem(
            int customerId, int id,  ShoppingCartRecord item)
        {
            // Change Cart Item(Quantity): http://localhost:33816/api/shoppingcart/{customerId}/{id} HTTPPut
            //   Note: Id, CustomerId, ProductId, TimeStamp, DateCreated, and Quantity in the body
            //{"Id":1,"CustomerId":1,"ProductId":33,"Quantity":2, "TimeStamp":"AAAAAAAA86s=","DateCreated":"1/20/2016"}
            //http://localhost:33816/api/shoppingcart/1/ts
            var json = JsonConvert.SerializeObject(item);
            try
            {
                using (var client = new HttpClient())
                {
                    var requestMessage = new HttpRequestMessage(
                        HttpMethod.Put,
                        $"{ServiceAddress}api/shoppingcart/{customerId}/{item.Id}");
                    requestMessage.Content = new StringContent(json,Encoding.UTF8,"application/json");
                    //var response = await client.SendAsync(requestMessage);
                    var response = await client.PutAsync($"{ServiceAddress}api/shoppingcart/{customerId}/{item.Id}", new StringContent(json, Encoding.UTF8, "application/json"));

                    return !response.IsSuccessStatusCode ? null : response.Headers.Location.AbsoluteUri;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                throw;
            }
        }
コード例 #2
0
 public void ShouldDeleteOnAddIfQuanityLessThanZero()
 {
     var item = new ShoppingCartRecord();
     item.ProductId = 34;
     item.Quantity = -10;
     item.DateCreated = DateTime.Now;
     item.CustomerId = 2;
     _repo.Add(item);
     var records = _repo.GetAll();
     var shoppingCartRecords = records.ToList();
     Assert.Equal(0,shoppingCartRecords.Count);
 }
コード例 #3
0
 public void ShouldUpdateQuanityOnAddIfAlreadyInCart()
 {
     var item = new ShoppingCartRecord();
     item.ProductId = 34;
     item.Quantity = 1;
     item.DateCreated = DateTime.Now;
     item.CustomerId = 2;
     _repo.Add(item);
     var records = _repo.GetAll();
     var shoppingCartRecords = records.ToList();
     Assert.Equal(1,shoppingCartRecords.Count);
     Assert.Equal(2,shoppingCartRecords[0].Quantity);
 }
コード例 #4
0
 public void ShouldAddAnItemToTheCart()
 {
     var item = new ShoppingCartRecord();
     item.ProductId = 2;
     item.Quantity = 5;
     item.DateCreated = DateTime.Now;
     item.CustomerId = 2;
     _repo.Add(item);
     var records = _repo.GetAll();
     var shoppingCartRecords = records.ToList();
     Assert.Equal(2,shoppingCartRecords.Count);
     Assert.Equal(2,shoppingCartRecords[1].ProductId);
     Assert.Equal(5,shoppingCartRecords[1].Quantity);
 }
コード例 #5
0
 public async Task<IActionResult> Delete(
     int customerId, int id, ShoppingCartRecord item)
 {
     if (id != item.Id)
     {
         return new BadRequestResult();
     }
     await WebAPICalls.RemoveCartItem(customerId, id, item.TimeStamp);
     return RedirectToAction(nameof(CartController.Index),
         new {customerId});
 }