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); }); }
public ShoppingCartController( CartContext cartContext , IEventStore eventStore , IProductCatalogueClient productCatalogueClient ) { _cartContext = cartContext; _eventStore = eventStore; _productCatalogueClient = productCatalogueClient; }
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); }); }
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); }); }
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)); }); }
public ShoppingCartModule(IShoppingCartStore shoppingCartStore, IProductCatalogueClient productCatalogue) : base("/shoppingcart") { // See specified user's shopping cart object Get("/{userid:int}", parameters => { var userId = (int)parameters.userid; return(shoppingCartStore.Get(userId)); }); // Add (or assign) specified items // available in products store to the shopping cart 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); }); // Remove items from the shopping cart 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); }); }
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); }); }