コード例 #1
0
 public async Task<IActionResult> Buy(int customerId, Customer customer)
 {
     if (customerId != customer.Id)
     {
         return new BadRequestResult();
     }
     if (!ModelState.IsValid)
     {
         //TODO: Handle this better
         throw new Exception("Oops");
         //return RedirectToAction();
     }
     int orderId = await WebAPICalls.PurchaseCart(customerId, customer);
     return RedirectToAction(nameof(OrderController.Details),"Order",new { customerId,orderId });
 }
コード例 #2
0
 internal static async Task<int> PurchaseCart(int customerId, Customer customer)
 {
     //Purchase: http://localhost:33816/api/shoppingcart/{customerId}/buy HTTPPost
     //Note: Customer in the body
     //{ "Id":1,"FullName":"Super Spy","EmailAddress":"*****@*****.**"}
     //  http://localhost:33816/api/shoppingcart/1/buy 
     var json = JsonConvert.SerializeObject(customer);
     try
     {
         //response.Headers.Location.AbsoluteUri
         using (var client = new HttpClient())
         {
             var uri = $"{ServiceAddress}api/shoppingcart/{customerId}/buy";
             var response = await client.PostAsync(uri,
                 new StringContent(json, Encoding.UTF8, "application/json"));
                                 
             return !response.IsSuccessStatusCode ? 0 : int.Parse(await response.Content.ReadAsStringAsync());
         }
     }
     catch (Exception ex)
     {
         Console.WriteLine(ex);
         throw;
     }
 }