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

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

                var shoppingCart      = await shoppingCartStore.Get(userId).ConfigureAwait(false);
                var shoppingCartItems = await productCatalog.GetShoppingCartItems(productCatalogIds).ConfigureAwait(false);

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

                return(shoppingCart);
            });

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

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

                return(shoppingCart);
            });
        }
        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);
            });
        }
Exemple #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 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);
            });
        }
Exemple #6
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);
            });
        }