public ShoppingCartModule(IShoppingCartStore shoppingCartStore, IProductCatalogueClient productCatalogue, IEventStore eventStore) : base("/shoppingcart")
        {
            Get("/{userid:int}", parameters =>
            {
                var userId = (int)parameters.userid;
                return(shoppingCartStore.Get(userId));
            });

            Post("/{userid:int}/items", async(parameters, _) =>
            {
                var productCatalogueIds = this.Bind <int[]>();
                var userId = (int)parameters.userid;

                var shoppingCart      = shoppingCartStore.Get(userId);
                var shoppingCartItems = await productCatalogue.GetShoppingCartItems(productCatalogueIds).ConfigureAwait(false);
                shoppingCart.AddItems(shoppingCartItems, eventStore);
                shoppingCartStore.Save(shoppingCart);

                return(shoppingCart);
            });

            Delete("/{userid:int}/items", parameters =>
            {
                var productCatalogueIds = this.Bind <int[]>();
                var userId = (int)parameters.userid;

                var shoppingCart = shoppingCartStore.Get(userId);
                shoppingCart.RemoveItems(productCatalogueIds, eventStore);
                shoppingCartStore.Save(shoppingCart);

                return(shoppingCart);
            });
        }
예제 #2
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);
            });
        }
예제 #3
0
        public async Task <IActionResult> Post(int userId, [FromBody] int[] productCatalogIds)
        {
            var shoppingCart      = _shoppingCartStore.Get(userId);
            var shoppingCartItems = await _productCatalog.GetShoppingCartItems(productCatalogIds).ConfigureAwait(false);

            shoppingCart.AddItems(shoppingCartItems, _eventStore);
            _shoppingCartStore.Save(shoppingCart);

            return(Ok(shoppingCart));
        }
        public ShoppingCartModule(
            IShoppingCartStore shoppingCartStore,
            IProductCatalogueClient productCatalogue,
            IEventStore eventStore)
            : base("/shoppingcart")
        {
            Get("/{userid:int}", parameters =>
            {
                var userId = (int)parameters.userid;
                return(shoppingCartStore.Get(userId));
            });

            Post("/{userid:int}/items", async parameters =>
            {
                var productCatalogueIds = this.Bind <int[]>();
                var userId = (int)parameters.userid;

                var shoppingCart      = await shoppingCartStore.Get(userId).ConfigureAwait(false);
                var shoppingCartItems =
                    await productCatalogue.GetShoppingCartItems(productCatalogueIds).ConfigureAwait(false);

//                var shoppingCartId = 1;
//                var cartItemsList = shoppingCart.Items.ToList();
//                if (cartItemsList.Any())
//                {
//                    shoppingCartId = cartItemsList[0].ShoppingCartId;
//                }
//
//                shoppingCartItems = shoppingCartItems.Select(p => new ShoppingCartItem(
//                    shoppingCartId,
//                    p.ProductCatalogId,
//                    p.ProductName,
//                    p.ProductDescription,
//                    p.Amount,
//                    p.Currency
//                ));

                shoppingCart.AddItems(shoppingCartItems, eventStore);
                await shoppingCartStore.Save(shoppingCart);

                return(shoppingCart);
            });

            Delete("/{userid:int}/items", async parameters =>
            {
                var productCatalogueIds = this.Bind <int[]>();
                var userId = (int)parameters.userid;

                var shoppingCart = await shoppingCartStore.Get(userId).ConfigureAwait(false);
                shoppingCart.RemoveItems(productCatalogueIds, eventStore);
                await shoppingCartStore.Save(shoppingCart);

                return(shoppingCart);
            });
        }
예제 #5
0
        public ShoppingCartModule(IShoppingCartStore shoppingCartStore,
                                  IProductCatalogueClient productCatalog,
                                  IEventStore eventStore)
            : base("/shoppingcart")
        {
            // route declaration
            Get("/{userid:int}", parameters =>
            {
                // route handler
                var userId = (int)parameters.userid;
                return(shoppingCartStore.Get(userId));
            });

            // route declaration
            Post("/{userid:int}/items",
                 // use async to handle call to Product Catalog microservice
                 async(parameters, _) =>
            {
                // read and de-serialise array of product id's in http request body
                var productCatalogIds = this.Bind <int[]>();
                var userId            = (int)parameters.userid;

                // fetch product information from Product Catalog microservice
                var shoppingCart = shoppingCartStore.Get(userId);
                // async call to ProductCatalog microservice
                var shoppingCartItems = await
                                        productCatalog
                                        .GetShoppingCartItems(productCatalogIds)
                                        .ConfigureAwait(false);
                // code resumes here after async call
                // add items to cart
                shoppingCart.AddItems(shoppingCartItems, eventStore);
                shoppingCartStore.Save(shoppingCart);
                return(shoppingCart);
            });

            // route declaration
            Delete("/{userid:int}/items", parameters =>
            {
                // read and de-serialise array of product id's in http request body
                var productCatalogIds = this.Bind <int[]>();
                var userId            = (int)parameters.userid;
                var shoppingCart      = shoppingCartStore.Get(userId);
                shoppingCart.RemoveItems(productCatalogIds, eventStore);
                shoppingCartStore.Save(shoppingCart);
                return(shoppingCart);
            });
        }
예제 #6
0
        public ShoppingCartModule(
            IShoppingCartStore shoppingCartStore,
            IProductCatalogClient productCatalogClient,
            IEventStore eventStore)
        {
            async Task <IShoppingCart> addingItemsToShoppingCart(dynamic parameters, CancellationToken _)
            {
                var productCatalogIds = this.Bind <int[]>();
                var userId            = (int)parameters.userid;

                var userShoppingCart  = shoppingCartStore.CartForUserOf(userId);
                var shoppingCartItems =
                    await productCatalogClient
                    .ShoppingCartItemsOf(productCatalogIds)
                    .ConfigureAwait(false);

                userShoppingCart.AddItems(shoppingCartItems, eventStore);
                shoppingCartStore.Save(userShoppingCart);

                return(userShoppingCart);
            }

            Get("/{userid:int}", parameters =>
            {
                var userId = (int)parameters.userid;
                return(shoppingCartStore.CartForUserOf(userId));
            });

            Post("/{userid:int}/items", addingItemsToShoppingCart);
        }
        public ShoppingCartModule(IShoppingCartStore shoppingCartStore, IProductCatalogueClient productCatalog, IEventStore eventStore)
            : base("/shoppingcart")
        {
            Get("/{userid:int}", async(parameters) =>
            {
                var userId = (int)parameters.userid;
                try
                {
                    await eventStore.Raise("ShoppingCartQueried", new { UserId = userId });
                }
                catch (Exception exc)
                {
                    Debug.WriteLine(exc.Message);
                }

                return(await shoppingCartStore.Get(userId));
            });

            Post("/{userid:int}/items", async(parameters, _) =>
            {
                var productCatalogIds = this.Bind <int[]>();    // binds from the request body
                var userId            = (int)parameters.userid; // binds from URL

                var shoppingCart      = await shoppingCartStore.Get(userId);
                var shoppingCartItems = await productCatalog
                                        .GetShoppingCartItems(productCatalogIds)
                                        .ConfigureAwait(false); // The ConfigureAwait(false) call tells the Task not to save the current thread context (we are not interested in it)

                shoppingCart.AddItems(shoppingCartItems, eventStore);
                await shoppingCartStore.Save(shoppingCart);

                return(shoppingCart);
            });

            Delete("/{userid:int}/items", async(parameters, _) =>
            {
                var productCatalogIds = this.Bind <int[]>();
                var userId            = (int)parameters.userid;

                var shoppingCart = await shoppingCartStore.Get(userId);

                shoppingCart.RemoveItems(productCatalogIds, eventStore);
                await shoppingCartStore.Save(shoppingCart);

                return(System.Threading.Tasks.Task.FromResult(shoppingCart));
            });
        }
예제 #8
0
        public async Task <ShoppingCart> Post(int userId, [FromBody] int[] productIds)
        {
            var shoppingCart  =  shoppingCartStore.Get(userId);
            var shoppingCartItems = await this.productCatalog.GetShoppingCartItems(productIds);

            shoppingCart.AddItems(shoppingCartItems,  eventStore);
            shoppingCartStore.Save(shoppingCart);

            return(shoppingCart);
        }
        public async Task <ActionResult> Post(int userId, [FromBody] int[] productCatalogIds)
        {
            if (productCatalogIds == null)
            {
                // await Task.FromException(new NullReferenceException(nameof(productCatalogIds)));
                // throw new HttpResponseException(msg);
                return(BadRequest());
            }

            var shoppingCart = await _shoppingCartStore.Get(userId);

            var shoppingCartItems = await _productCatalog.GetShoppingCartItems(productCatalogIds)
                                    .ConfigureAwait(false);

            shoppingCart.AddItems(shoppingCartItems, _eventStore);
            await _shoppingCartStore.Save(shoppingCart);

            return(Ok());
        }
        public ShoppingCartModule(
            IShoppingCartStore shoppingCartStore,
            IProductCatalogClient productCatalog,
            IEventStore eventStore)
            : base("/shoppingcart")
        {
            Get("/{userid:int}", parameters => {
                var userId = (int)parameters.userid;
                return(shoppingCartStore.Get(userId));
            });

            Get("/prices", _ => {
                var client     = new MongoClient("mongodb://localhost:32769");
                var db         = client.GetDatabase("test");
                var collection = db.GetCollection <Price>("prices");
                var filter     = new BsonDocument();
                var prices     = collection.AsQueryable().Select(c => new { c.Currency, c.Amount }).ToList();
                //var prices = await collection.Find(filter).ToListAsync();
                return(prices);
            });

            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);
            });
        }
예제 #11
0
        public ShoppingCartModule(IShoppingCartStore shoppingCartStore,
                                  IProductCatalogClient productCatalog,
                                  IEventStore eventStore)
            : base("/shoppingcart")
        {
            // get a specific user's shopping basket
            // route declaration, path followed by route handler
            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);
                // get up to date information about the items chosen from the product catalog (including price)
                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);
            });
        }
예제 #12
0
        public ShoppingCartModule(
          IShoppingCartStore shoppingCartStore,
          ProductCatalogueClient productCatalogue,
          IEventStore eventStore)
          : base("/shoppingcart")
        {
            Get["/{userid:int}"] = parameters =>
            {
                var userId = (int)parameters.userid;
                return shoppingCartStore.Get(userId);
            };

            Post["/{userid:int}/items", runAsync: true] = async (parameters, _) =>
            {
                var productCatalogueIds = this.Bind<int[]>();
                var userId = (int)parameters.userid;

                var shoppingCart = shoppingCartStore.Get(userId);
                var shoppingCartItems = await productCatalogue.GetShoppingCartItems(productCatalogueIds);
                shoppingCart.AddItems(shoppingCartItems, eventStore);
                shoppingCartStore.Save(shoppingCart);

                return shoppingCart;
            };

            Delete["/{userid:int}/items"] = parameters =>
            {
                var productCatalogueIds = this.Bind<int[]>();
                var userId = (int)parameters.userid;

                var shoppingCart = shoppingCartStore.Get(userId);
                shoppingCart.RemoveItems(productCatalogueIds, eventStore);
                shoppingCartStore.Save(shoppingCart);

                return shoppingCart;
            };
        }
예제 #13
0
        public ShoppingCartModule(
            IShoppingCartStore shoppingCartStore,
            IProductCatalogueClient productCatalogue,
            IEventStore eventStore)
            : base("/shoppingcart")
        {
            …

            Delete("/{userid:int}/items", parameters =>
            {
                var productCatalogueIds = this.Bind <int[]>();
                var userId = (int)parameters.userid;

                var shoppingCart = shoppingCartStore.Get(userId);
                shoppingCart.RemoveItems(productCatalogueIds, eventStore);
                shoppingCartStore.Save(shoppingCart);

                return(shoppingCart);
            });
        }