Exemplo n.º 1
0
        public ShoppingCartModule(IShoppingCartStore shoppingCartStore,
                                  IProductCatalogClient productCatalogClient,
                                  IEventStore eventStore)
            : base("/shoppingcart")
        {
            Get("/{userid:int}", async parameters => await GetShoppingCart(shoppingCartStore, parameters));

            Post("/{userid:int}", async(parameters, _) =>
            {
                var productIds = this.Bind <int[]>();
                ShoppingCartModel shoppingCart = await GetShoppingCart(shoppingCartStore, parameters);

                var products = await productCatalogClient.GetShoppingCartItems(productIds).ConfigureAwait(false);
                shoppingCart.AddProducts(products, eventStore);

                shoppingCartStore.Save(shoppingCart);
                return(shoppingCart);
            });

            Delete("/{userid:int}", async parameters =>
            {
                var productIds = this.Bind <int[]>(new BindingConfig()
                {
                    BodyOnly = true
                });
                var shoppingCart = await GetShoppingCart(shoppingCartStore, parameters);

                shoppingCart.RemoveProducts(productIds, eventStore);

                shoppingCartStore.Save(shoppingCart);
                return(shoppingCart);
            });
        }