Ejemplo n.º 1
0
        public async Task <CheckoutSummary> Checkout(string userId)
        {
            var result = new CheckoutSummary();

            //call user actor to get the basket
            IUserActor             userActor = GetUserActor(userId);
            Dictionary <Guid, int> basket    = await userActor.GetBasket();

            //get catalog client
            IProductCatalogService catalogService = GetProductCatalogService();

            //constuct CheckoutProduct items by calling to the catalog
            foreach (KeyValuePair <Guid, int> basketLine in basket)
            {
                Product product = await catalogService.GetProduct(basketLine.Key);

                var checkoutProduct = new CheckoutProduct
                {
                    Product  = product,
                    Price    = product.Price,
                    Quantity = basketLine.Value
                };
                result.Products.Add(checkoutProduct);
            }

            //generate total price
            result.TotalPrice = result.Products.Sum(p => p.Price);

            //clear user basket
            await userActor.ClearBasket();

            await AddToHistory(result);

            return(result);
        }
Ejemplo n.º 2
0
        public async Task <CheckoutSummary> Checkout(string userId)
        {
            var result = new CheckoutSummary();

            result.Date     = DateTime.UtcNow;
            result.Products = new List <CheckoutProduct>();

            IUserActor             userActor = GetUserActor(userId);
            Dictionary <Guid, int> basket    = await userActor.GetBasket();

            IProductCatalogService catalogService = GetProductCatalogService();

            foreach (KeyValuePair <Guid, int> basketLine in basket)
            {
                Product product = await catalogService.GetProduct(basketLine.Key);

                var checkoutProduct = new CheckoutProduct
                {
                    Product  = product,
                    Price    = product.Price,
                    Quantity = basketLine.Value
                };
                result.Products.Add(checkoutProduct);
            }

            result.TotalPrice = result.Products.Sum(p => p.Price);

            await userActor.ClearBasket();

            await AddToHistory(result);

            return(result);
        }
Ejemplo n.º 3
0
        public async Task <CheckoutSummary> CheckoutAsync(string userId)
        {
            var result = new CheckoutSummary();

            result.Date     = DateTime.UtcNow;
            result.Products = new List <CheckoutProduct>();

            // Get the user basket
            IUserActor userActor = GetUserActor(userId);

            BasketItem[] basket = await userActor.GetBasket();

            IProductCatalogService catalogService = GetProductCatalogService();

            foreach (var item in basket)
            {
                var product = await catalogService.GetProduct(item.ProductId);

                var checkoutProduct = new CheckoutProduct()
                {
                    Product  = product,
                    Price    = product.Price,
                    Quantity = item.Quantity
                };

                result.Products.Add(checkoutProduct);
            }

            return(result);
        }
Ejemplo n.º 4
0
        private void Subscribe(IUserActor userActor)
        {
            Log.Info($"{_user.Login}");

            if (_syncSubscription != null)
            {
                throw new InvalidOperationException("Already subscribed to changes.");
            }

            var start = new ChangeSummary();

            start.Add(userId: _user.UserId);

            // Changes streamed from the queue
            _syncSubscription = _syncManager.Changes
                                .ObserveOn(TaskPoolScheduler.Default)
                                .StartWith(start) // Run an initial sync no matter what.
                                .Select(c =>
                                        Observable.FromAsync(() => _syncContext.Sync(c))
                                        .Catch <Unit, Exception>(LogError <Unit>))
                                .Concat() // Force sequential evaluation
                                .Subscribe();

            // Polling for updates
            _pollSubscription = _PollInterval
                                .ObserveOn(TaskPoolScheduler.Default)
                                .StartWith(0)
                                .Select(_ =>
                                        Observable.FromAsync(() => userActor.Sync())
                                        .Catch <Unit, Exception>(LogError <Unit>))
                                .Concat() // Force sequential evaluation
                                .Subscribe();
        }
Ejemplo n.º 5
0
        public async Task <CheckoutSummary> CheckoutAsync(string userId)
        {
            var result = new CheckoutSummary();

            result.Date     = DateTime.UtcNow;
            result.Products = new List <CheckoutProduct>();
            IUserActor userActor = GetUserActor(userId);

            BasketItem[] basket = await userActor.GetBasket();

            var catalogService = GetProductCatalogService();

            foreach (var basketLine in basket)
            {
                var product = await catalogService.GetProduct(basketLine.ProductId);

                var checkoutProduct = new CheckoutProduct
                {
                    Product  = product,
                    Price    = product.Price,
                    Quantity = basketLine.Quantity
                };

                result.Products.Add(checkoutProduct);
            }

            result.TotalPrice = result.Products.Sum(p => p.Price);
            await userActor.ClearBasket();

            await AddToHistoryAsync(result);

            return(result);
        }
        public async Task AddAsync(
            string userId,
            [FromBody] ApiBasketAddRequest request)
        {
            IUserActor actor = GetActor(userId);

            await actor.AddToBasket(request.ProductId, request.Quantity);
        }
Ejemplo n.º 7
0
        public async Task <ApiBasket> Get(string userId)
        {
            IUserActor actor = GetActor(userId);

            Dictionary <Guid, int> products = await actor.GetBasket();

            return(new ApiBasket()
            {
                UserId = userId,
                Items = products.Select(p => new ApiBasketItem {
                    ProductId = p.Key.ToString(), Quantity = p.Value
                }).ToArray()
            });
        }
Ejemplo n.º 8
0
        public async Task <UserInfo> Get(int id)
        {
            IUserActor actor    = this.GetActor(id);
            UserInfo   userInfo = await actor.GetUserInfoAsync();

            if (userInfo == null)
            {
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }
            else
            {
                return(userInfo);
            }
        }
Ejemplo n.º 9
0
        public async Task <ApiBasket> Get(string userId)
        {
            IUserActor userActor = GetActor(userId);

            Dictionary <Guid, int> basketItems = await userActor.GetBasket();

            ApiBasket apiBasket = new ApiBasket()
            {
                UserId = userId,
                Items  = basketItems.Select(i => new ApiBasketItem {
                    ProductId = i.Key.ToString(), Quantity = i.Value
                }).ToArray()
            };

            return(apiBasket);
        }
Ejemplo n.º 10
0
        public async Task <ApiBasket> GetAsync(string userId)
        {
            IUserActor actor = GetActor(userId);

            BasketItem[] products = await actor.GetBasket();

            return(new ApiBasket()
            {
                UserId = userId,
                Items = products.Select(p => new ApiBasketItem
                {
                    ProductId = p.ProductId.ToString(),
                    Quantity = p.Quantity
                })
                        .ToArray()
            });
        }
Ejemplo n.º 11
0
        public async Task <ApiBasket> GetAsync(string userId)
        {
            IUserActor actor = GetActor(userId);

            // We should not use any complicated types over the wire (interfaces, generics, etc)
            //Dictionary<Guid, int> products = await actor.GetBasket();
            BasketItem[] products = await actor.GetBasket();

            return(new ApiBasket()
            {
                UserId = userId,
                Items = products.Select( // Convert from Product to ApiBasketItem
                    p => new ApiBasketItem
                {
                    ProductId = p.ProductId.ToString(), //Key.ToString(),
                    Quantity = p.Quantity               //Value
                }).ToArray()
            });
        }
Ejemplo n.º 12
0
 public async Task Add(string userId, [FromBody] ApiBasketAddRequest request)
 {
     try
     {
         IUserActor actor = GetActor(userId);
         if (request == null)
         {
             request = new ApiBasketAddRequest
             {
                 ProductId = Guid.NewGuid(),
                 Quantity  = 1
             };
         }
         await actor.AddToBasket(request.ProductId, request.Quantity);
     }
     catch (Exception ex)
     {
         throw new Exception(ex.ToString());
     }
 }
Ejemplo n.º 13
0
        public async Task <CheckoutSummary> Checkout(string userId)
        {
            var result = new CheckoutSummary()
            {
                Date     = DateTime.UtcNow,
                Products = new List <CheckoutProduct>()
            };

            IUserActor actor = CreateUserActor(userId);
            var        cart  = await actor.GetCart();

            IProductCatalogService productCatalogService = GetProductCatalogService();
            var products = await productCatalogService.GetAllProducts();

            var totalCost = 0.0;

            foreach (var item in cart)
            {
                var productId = item.Key;
                var quantity  = item.Value;
                var product   = products.FirstOrDefault(p => p.Id == productId);
                if (product != null)
                {
                    result.Products.Add(new CheckoutProduct()
                    {
                        Product  = product,
                        Quantity = quantity,
                        Price    = product.Price * quantity
                    });
                    totalCost += (product.Price * quantity);
                }
            }
            result.TotalPrice = totalCost;

            //Clearing the cart since user has checkout all the products and add to history
            await actor.ClearCart();

            await AddToHistory(result);

            return(result);
        }
        public async Task <CheckoutSummary> CheckoutAsync(string userId)
        {
            //holds result of the checkout
            var result = new CheckoutSummary();

            result.Date     = DateTime.UtcNow;
            result.Products = new List <CheckoutProduct>();

            //call user actor to get the basket
            IUserActor userActor = GetUserActor(userId);

            BasketItem[] basket = await userActor.GetBasket();

            //get catalog client
            IProductCatalogService catalogService = GetProductCatalogService();

            //get detailed product information for each basket item by calling product service
            foreach (BasketItem basketLine in basket)
            {
                Product product = await catalogService.GetProductAsync(basketLine.ProductId);

                var checkoutProduct = new CheckoutProduct
                {
                    Product  = product,
                    Price    = product.Price,
                    Quantity = basketLine.Quantity
                };
                result.Products.Add(checkoutProduct);
            }

            //generate total price
            result.TotalPrice = result.Products.Sum(p => p.Price);

            //clear user basket
            await userActor.ClearBasket();

            await AddToHistoryAsync(result);

            return(result);
        }
Ejemplo n.º 15
0
        public async Task <CheckoutSummary> CheckoutAsync(string userId)
        {
            var checkoutSummary = new CheckoutSummary();

            checkoutSummary.Date     = DateTime.UtcNow;
            checkoutSummary.Products = new List <CheckoutProduct>();

            IUserActor userActor       = GetUserActor(userId);
            var        userBasketItems = await userActor.GetBasketAsync();

            var basket = new Dictionary <Guid, int>();

            foreach (var userBasketItem in userBasketItems)
            {
                basket.Add(userBasketItem.ProductId, userBasketItem.Quantity);
            }

            IProductCatalogService catalogService = GetProductCatalogService();

            foreach (KeyValuePair <Guid, int> basketLine in basket)
            {
                Product product = await catalogService.GetProductAsync(basketLine.Key);

                var checkoutProduct = new CheckoutProduct
                {
                    Product  = product,
                    Quantity = basketLine.Value,
                    SubTotal = product.Price * basketLine.Value
                };

                checkoutSummary.Products.Add(checkoutProduct);

                checkoutSummary.TotalPrice += checkoutProduct.SubTotal;
            }

            await AddToHistoryAsync(checkoutSummary);

            return(checkoutSummary);
        }
Ejemplo n.º 16
0
        public async Task <CheckoutSummary> CheckoutAsync(string userId)
        {
            CheckoutSummary result = new CheckoutSummary()
            {
                Date     = DateTime.UtcNow,
                Products = new List <CheckoutProduct>()
            };

            // get user basket
            IUserActor userActor = GetUserActor(userId);
            var        basket    = await userActor.GetBasket();

            IProductCatalogService catalogService = GetProductCatalogService();

            // build the products using the catalog service data and the basket line items
            foreach (var basketItem in basket)
            {
                Product product = await catalogService.GetProductAsync(basketItem.ProductId);

                if (product != null)
                {
                    var checkoutProduct = new CheckoutProduct()
                    {
                        Product  = product,
                        Price    = product.Price,
                        Quantity = basketItem.Quantity
                    };

                    result.Products.Add(checkoutProduct);
                }
            }

            // add the current checkout summary to history (for later retrieval)
            await AddToHistoryAsync(result);

            return(result);
        }
        public async Task Add(string userId, [FromBody] CartRequest request)
        {
            IUserActor actor = GetActor(userId);

            await actor.AddToCart(request.ProductId, request.Quantity);
        }
Ejemplo n.º 18
0
 public async Task Delete(int id)
 {
     IUserActor actor = this.GetActor(id);
     await actor.DeleteUserInfoAsync();
 }
Ejemplo n.º 19
0
 public async Task Put(int id, [FromBody] UserInfo value)
 {
     IUserActor actor = this.GetActor(id);
     await actor.SetUserInfoAsync(value);
 }
Ejemplo n.º 20
0
        public async Task Add(string userId, [FromBody] ApiBasketAddRequest request)
        {
            IUserActor userActor = GetActor(userId);

            await userActor.AddToBasket(new Guid(request.ProductId), request.Quantity);
        }
Ejemplo n.º 21
0
 private void CreateActor(string userId)
 {
     _userActor = ActorProxy.Create <IUserActor>(
         actorId: new ActorId(userId),
         serviceUri: new Uri("fabric:/ECommerce.Sf/UserActorService"));
 }
        public async Task Delete(string userId)
        {
            IUserActor actor = GetActor(userId);

            await actor.ClearBasket();
        }
Ejemplo n.º 23
0
        public async Task Add(string userId, [FromBody] BasketRequestItemViewModel request)
        {
            IUserActor actor = GetActor(userId);

            await actor.AddToBasket(request.ProductId, request.Quantity);
        }