Skip to content

Instantly share code, notes, and snippets.

@karthikeyanVK
Last active February 14, 2020 19:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save karthikeyanVK/4e132fa4a5f4320c1dfe2951b8dd1509 to your computer and use it in GitHub Desktop.
Save karthikeyanVK/4e132fa4a5f4320c1dfe2951b8dd1509 to your computer and use it in GitHub Desktop.
using System;
using PetShop.DBAccess.CosmosDB;
using PetShop.Model;
using PetShop.DBAccess.CosmosDB;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using Newtonsoft.Json;
using System.Text;
namespace PetShop.Business.Orders
{
public class OrderService
{
static HttpClient client = new HttpClient();
public static Guid DefaultCustomerID = Guid.Parse("17EDCACA-C902-431A-B935-A1FCF05CA52A");
public bool SaveData(Model.Order order)
{
var orderData = new Order
{
OrderId = Guid.NewGuid(),
ProductId = order.ProductId,
ProductName = order.ProductName,
CustomerId = DefaultCustomerID,
Quantity = order.Quantity
};
if (UpdateProductQuantity(orderData))
{
ICosmosDbService cosmosDbService = new CosmosDbService();
cosmosDbService.AddItemAsync<Order>(orderData, orderData.OrderId.ToString()).GetAwaiter().GetResult();
}
return true;
}
private bool UpdateProductQuantity(Model.Order order)
{
OrderedProduct orderedProduct = new OrderedProduct
{
OrderedProductQuantity = order.Quantity,
ProductId = order.ProductId
};
var productUrl = Environment.GetEnvironmentVariable("ProductBaseUrl");
var orderedProductContent = JsonConvert.SerializeObject(orderedProduct);
client.BaseAddress = new Uri(productUrl);
var response = client.PostAsync(
productUrl, new StringContent(orderedProductContent, Encoding.UTF8, "application/json"));
var result = response.GetAwaiter().GetResult();
return true;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment