-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShoppingCartModule.cs
111 lines (93 loc) · 3.96 KB
/
ShoppingCartModule.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
using Nancy;
using Nancy.ModelBinding;
namespace ShoppingCart
{
public class ShoppingCartModule: NancyModule
{
private static string productCatalogBaseUrl =
@"http://private-05cc8-chapter2productcatalogmicroservice.apiary-mock.com";
private static string getProductPathTemplate =
@"/products?productIds=[{0}]";
private static async Task<HttpResponseMessage>
RequestProductFromProductCatalogue(int[] productCatalogueIds)
{
var productsResource = string.Format(
getProductPathTemplate, string.Join(",", productCatalogueIds)
);
using(var httpClient = new HttpClient()){
httpClient.BaseAddress = new Uri(productCatalogBaseUrl);
return await
httpClient.GetAsync(productsResource).ConfigureAwait(false);
}
}
private static async Task<IEnumerable<ShoppingCartItem>>
ConvertToShoppingCartItems(HttpResponseMessage response){
response.EnsureSuccessStatusCode();
var products =
JsonConvert.DeserializeObject<List<ProductCatalogueProduct>>(
await response.Content.ReadAsStringAsync().ConfigureAwait(false)
);
return
products
.Select(productCatalogBaseUrl => new ShoppingCartItem(
int.Parse(p.ProductId),
p.ProductName,
p.ProductDescription,
p.Price
));
}
private class ProductCatalogueProduct {
public string ProductId { get; set; }
public string ProductName { get; set; }
public string ProductDescription { get; set; }
public Money Price { get; set; }
}
private async Task<IEnumerable<ShoppingCartItem>>
GetItemsFromCatalogueService(int[] productCatalogueIds)
{
var response = await
RequestProductFromProductCatalogue(productCatalogueIds)
.ConfigureAwait(false);
return await ConvertToShoppingCartItems(response)
.ConfigureAwait(false);
}
public ShoppingCartModule(
IShoppingCartStore shoppingCartStore,
IProductCatalogClient productCatalog,
IEventStore eventStore
): base("/shoppingCart"){
Get("/{userid:int}", parameters =>
{
var userId = (int) parameters.userid;
return shoppingCartStore.Get(userId);
}
);
Post("/{userid:int}/items",
async (parameters, _) =>
{
var productCatalogIds = this.Bind<int[]>();
var userId = (int) parameters.userid;
var shoppingCart = shoppingCartStore.Get(userid);
var shoppingCartItems = await
productCatalog
.GetShoppingCartItems(productCatalogIds)
.ConfigureAwait(false);
shoppingCart.AddItems(shoppingCartItems, eventStore);
shoppingCartStore.Save(shoppingCart);
return shoppingCart;
}
);
Delete("/{userid:int}/items",
parameters =>
{
var productCatalogIds = this.Bind<int[]>();
var userId = (int) parameters.userid;
var shoppingCart = shoppingCartStore.Get(userid);
shoppingCart.RemoveItems(productCatalogIds, eventStore);
shoppingCartStore.Save(shoppingCart);
return shoppingCart;
}
);
}
}
}