Ejemplo n.º 1
0
        public async Task <int> CreateOrderAsync(int basketId, string firstName, string lastName, Address address)
        {
            // get the basket with items
            var specBasket = new BasketWithItemsSpecification(basketId);
            var basket     = await _basketRepository.FirstOrDefaultAsync(specBasket);

            // get the list of products in basket
            var specProducts = new ProductsSpecification(basket.Items.Select(x => x.ProductId).ToArray());
            var products     = await _productRepository.ListAsync(specProducts);

            // create order
            var order = new Order()
            {
                BuyerId       = basket.BuyerId,
                FirstName     = firstName,
                LastName      = lastName,
                ShipToAddress = address,
                OrderItems    = basket.Items.Select(x => {
                    var product = products.First(p => p.Id == x.ProductId);
                    return(new OrderItem()
                    {
                        ProductId = x.ProductId,
                        ProductName = product.Name,
                        PictureUri = product.PictureUri,
                        UnitPrice = x.UnitPrice,
                        Units = x.Quantity
                    });
                }).ToList()
            };

            // save order
            order = await _orderRepository.AddAsync(order);

            return(order.Id);
        }
        public async Task HandleAsync(CreatePurchaseOrderCommand command)
        {
            //Issue: Crossing the boundary context to cut shot the time.
            //This should call the Basket Api and get the details. Not the repository directly.
            var basketSpecification = new BasketWithItemsSpecification(command.BasketId);
            var basket = await _basketAsyncRepository.GetSingleAsync(basketSpecification);

            var items = new List <PurchaseOrderItem>();

            foreach (var basketItem in basket.BasketItems)
            {
                var catalogItem = await _catalogItemAsyncRepository.GetByIdAsync(basketItem.CatalogItemId);

                var itemOrdered = new CatalogItemOrdered(catalogItem.Id,
                                                         (CatalogItemType)catalogItem.CatalogType, catalogItem.Name, catalogItem.PictureUri);
                var orderItem = new PurchaseOrderItem(itemOrdered, basketItem.UnitPrice, basketItem.Quantity);
                items.Add(orderItem);
            }

            var purchaseOrderId = Guid.NewGuid();
            var purchaseOrderIdNumberMapping = new PurchaseOrderIdNumberMapping {
                PurchaseOrderId = purchaseOrderId
            };

            _purchaseOrderIdNumberMappingAsyncRepository.Add(purchaseOrderIdNumberMapping);
            await _unitOfWork.SaveChangesAsync();

            var purchaseOrder = new Domain.PurchaseOrder(purchaseOrderId, purchaseOrderIdNumberMapping.Id, basket.BuyerId, command.Address, items);
            await _shopAggregateRepositoryService.SaveAsync(purchaseOrder);
        }
Ejemplo n.º 3
0
        public async Task <BasketViewModel> GetBasket(int basketId)
        {
            var basketSpec = new BasketWithItemsSpecification(basketId);
            var basket     = _basketRepository.List(basketSpec).FirstOrDefault();

            if (basket == null)
            {
                return(await CreateBasket());
            }

            var viewModel = new BasketViewModel();

            viewModel.Id      = basket.Id;
            viewModel.BuyerId = basket.BuyerId;
            viewModel.Items   = basket.Items.Select(i =>
            {
                var itemModel = new BasketItemViewModel()
                {
                    Id            = i.Id,
                    UnitPrice     = i.UnitPrice,
                    Quantity      = i.Quantity,
                    CatalogItemId = i.CatalogItemId
                };
                var item              = _itemRepository.GetById(i.CatalogItemId);
                itemModel.PictureUrl  = _uriComposer.ComposePicUri(item.PictureUri);
                itemModel.ProductName = item.Name;
                return(itemModel);
            })
                                .ToList();
            return(viewModel);
        }
Ejemplo n.º 4
0
        public async Task <int> AddItemToBasketAsync(int basketId, int productId, decimal price, int quantity = 1)
        {
            // get basket with items
            var    spec   = new BasketWithItemsSpecification(basketId);
            Basket basket = await _basketRepository.FirstOrDefaultAsync(spec);

            // get basket item if exists
            BasketItem basketItem = basket.Items.FirstOrDefault(x => x.ProductId == productId);

            // if exists increase quantity
            if (basketItem != null)
            {
                basketItem.Quantity += quantity;
            }
            // if not exists add new basket item
            else
            {
                basketItem = new BasketItem()
                {
                    BasketId  = basketId,
                    ProductId = productId,
                    Quantity  = quantity,
                    UnitPrice = price
                };
                basket.Items.Add(basketItem);
            }

            // update basket
            await _basketRepository.UpdateAsync(basket);

            return(basket.Items.Count);
        }
        public async Task <BasketViewModel> GetBasketViewModel()
        {
            int basketId = await GetOrCreateBasketIdAsync();

            var specBasket = new BasketWithItemsSpecification(basketId);//sepeti öğeleriyle getirdik
            var basket     = await _basketRepository.FirstOrDefaultAsync(specBasket);

            var productsIds  = basket.Items.Select(x => x.ProductId).ToArray();//sepetteki öğelerin ilişkili olduğu urunleri getirdik
            var specProducts = new ProductsSpecification(productsIds);
            var products     = await _productRepository.ListAsync(specProducts);

            var basketItems = new List <BasketItemViewModel>();//sepet ögelerini ürünbilgileriyle birlikte BasketItemViewModel nesnelerini oluşturduk ve listeye ekledik

            foreach (var item in basket.Items.OrderBy(x => x.Id))
            {
                var product = products.First(x => x.Id == item.ProductId);
                basketItems.Add(new BasketItemViewModel()
                {
                    Id          = item.Id,
                    ProductId   = item.ProductId,
                    Quantity    = item.Quantity,
                    ProductName = product.Name,
                    Price       = product.Price,
                    PictureUri  = product.PictureUri
                });
            }
            return(new BasketViewModel()//sepet ögelerii ile basketViewModeli oluşturduk. Sepet sayfasında göstereceğimiz şeyleri.
            {
                Id = basketId,
                BuyerId = basket.BuyerId,
                Items = basketItems
            });
        }
Ejemplo n.º 6
0
        public async Task <DeliveryTimeDTO> CalculateDeliveryTime(int basketId) //TODO: Create unit test
        {
            var basketSpec = new BasketWithItemsSpecification(basketId);
            var basket     = (await _basketRepository.ListAsync(basketSpec)).SingleOrDefault();

            if (basket.Items.Any(x => x.CatalogTypeId.HasValue))
            {
                return(null);
            }
            DeliveryTimeDTO deliveryTime = new DeliveryTimeDTO
            {
                Min  = 2,
                Max  = 3,
                Unit = DeliveryTimeUnitType.Days
            };

            foreach (var item in basket.Items)
            {
                var spec    = new CatalogTypeFilterSpecification(item.CatalogItemId);
                var product = await _itemRepository.GetBySpecAsync(spec);

                if (product.CatalogType.DeliveryTimeUnit > deliveryTime.Unit)
                {
                    deliveryTime.Min  = product.CatalogType.DeliveryTimeMin;
                    deliveryTime.Max  = product.CatalogType.DeliveryTimeMax;
                    deliveryTime.Unit = product.CatalogType.DeliveryTimeUnit;
                }
                else if (product.CatalogType.DeliveryTimeUnit == deliveryTime.Unit)
                {
                    deliveryTime.Min = product.CatalogType.DeliveryTimeMin > deliveryTime.Min ? product.CatalogType.DeliveryTimeMin : deliveryTime.Min;
                    deliveryTime.Max = product.CatalogType.DeliveryTimeMax > deliveryTime.Max ? product.CatalogType.DeliveryTimeMax : deliveryTime.Max;
                }
            }
            return(deliveryTime);
        }
Ejemplo n.º 7
0
        public async Task TransferBasketAsync(string anonymousId, string userName, bool isGuest)
        {
            Guard.Against.NullOrEmpty(anonymousId, nameof(anonymousId));
            Guard.Against.NullOrEmpty(userName, nameof(userName));

            var basketSpec = new BasketWithItemsSpecification(anonymousId);
            var basket     = (await _basketRepository.ListAsync(basketSpec)).LastOrDefault();

            if (basket == null)
            {
                return;
            }

            if (!isGuest && basket.Items?.Count > 0)
            {
                //Delete previous baskets
                var basketSpecUser = new BasketWithItemsSpecification(userName);
                var listBasket     = await _basketRepository.ListAsync(basketSpecUser);

                foreach (var item in listBasket)
                {
                    await _basketRepository.DeleteAsync(item);
                }
            }
            basket.BuyerId     = userName;
            basket.UpdatedDate = DateTime.Now;
            await _basketRepository.UpdateAsync(basket);
        }
Ejemplo n.º 8
0
        public async Task CreateOrderAsync(int basketId, Address shippingAddress)
        {
            var spec   = new BasketWithItemsSpecification(basketId);
            var basket = await _basketRepository.FirstOrDefaultAsync(spec);

            var specProduct = new ProductSpecification(basket.Items.Select(x => x.ProductId).ToArray());
            var products    = await _productRepository.ListAsync(specProduct);

            Order order = new Order()
            {
                OrderDate     = DateTimeOffset.Now,
                ShipToAddress = shippingAddress,
                BuyerId       = basket.BuyerId,
                OrderItems    = basket.Items.Select(x =>
                {
                    var product = products.First(p => p.Id == x.ProductId);
                    return(new OrderItem()
                    {
                        ProductId = x.ProductId,
                        Quantity = x.Quantity,
                        PictureUri = product.PictureUri,
                        Price = product.Price,
                        ProductName = product.Name
                    });
                }).ToList()
            };
            await _orderRepository.AddAsync(order);
        }
Ejemplo n.º 9
0
        public async Task TransferBasketAsync(string anonymousId, string userName)
        {
            Guard.Against.NullOrEmpty(anonymousId, nameof(anonymousId));
            Guard.Against.NullOrEmpty(userName, nameof(userName));
            var anonymousBasketSpec = new BasketWithItemsSpecification(anonymousId);
            var anonymousBasket     = await _basketRepository.FirstOrDefaultAsync(anonymousBasketSpec);

            if (anonymousBasket == null)
            {
                return;
            }
            var userBasketSpec = new BasketWithItemsSpecification(userName);
            var userBasket     = await _basketRepository.FirstOrDefaultAsync(userBasketSpec);

            if (userBasket == null)
            {
                userBasket = new Basket(userName);
                await _basketRepository.AddAsync(userBasket);
            }
            foreach (var item in anonymousBasket.Items)
            {
                userBasket.AddItem(item.CatalogItemId, item.UnitPrice, item.Quantity);
            }
            await _basketRepository.UpdateAsync(userBasket);

            await _basketRepository.DeleteAsync(anonymousBasket);
        }
Ejemplo n.º 10
0
        public async Task <BasketViewModel> GetBasketViewModel()
        {
            int basketId = await GetOrCreateBasketIdAsync();

            var specBasket = new BasketWithItemsSpecification(basketId);
            var basket     = await _basketRepository.FirstOrDefaultAsync(specBasket);

            var productIds   = basket.Items.Select(x => x.ProductId).ToArray();
            var specProducts = new ProductsSpecification(productIds);
            var products     = await _productRepository.ListAsync(specProducts);

            var basketItems = new List <BasketItemViewModel>();

            foreach (var item in basket.Items.OrderBy(x => x.Id))
            {
                var product = products.First(x => x.Id == item.ProductId);
                basketItems.Add(new BasketItemViewModel()
                {
                    Id          = item.Id,
                    ProductId   = item.ProductId,
                    Quantity    = item.Quantity,
                    ProductName = product.Name,
                    Price       = product.Price,
                    PictureUri  = product.PictureUri
                });
            }

            return(new BasketViewModel()
            {
                Id = basketId,
                BuyerId = basket.BuyerId,
                Items = basketItems,
            });
        }
Ejemplo n.º 11
0
        public void MatchesNoBasketsIfBasketIdNotPresent()
        {
            int badBasketId = -1;
            var spec        = new BasketWithItemsSpecification(badBasketId);
            var result      = _evaluator.GetQuery(GetTestBasketCollection().AsQueryable(), spec).Any();

            Assert.False(result);
        }
        public void BasketWithItemsSpecification_Bad_BasketId_Test()
        {
            var spec = new BasketWithItemsSpecification(10000);

            var result = _evaluator.GetQuery(GetBaskets().AsQueryable(), spec).Any();

            Assert.False(result);
        }
Ejemplo n.º 13
0
        public void MatchesNoBasketifBuyerIdNotPresent()
        {
            string badBuyerId = "Bad Id";
            var    spec       = new BasketWithItemsSpecification(badBuyerId);
            var    result     = _evaluator.GetQuery(GetTestBasketCollection().AsQueryable(), spec).Any();

            Assert.False(result);
        }
Ejemplo n.º 14
0
        public void MatchesBasketWithGivenBuyerId()
        {
            var spec   = new BasketWithItemsSpecification(_testBasketId);
            var result = _evaluator.GetQuery(GetTestBasketCollection().AsQueryable(), spec).FirstOrDefault();

            Assert.NotNull(result);
            Assert.Equal(_testBasketId, result.Id);
        }
        public void MatchesNoBasketsIfIdNotPresent()
        {
            int badId = -1;
            var spec  = new BasketWithItemsSpecification(badId);

            Assert.False(GetTestBasketCollection()
                         .AsQueryable()
                         .Any(spec.WhereExpressions.First().Compile()));
        }
        public void BasketWithItemsSpecification_BasketId_Test()
        {
            var spec = new BasketWithItemsSpecification(_basketId);

            var result = _evaluator.GetQuery(GetBaskets().AsQueryable(), spec).FirstOrDefault();

            Assert.NotNull(result);
            Assert.Equal(_basketId, result.Id);
        }
Ejemplo n.º 17
0
        public void MatchesNoBasketsIfBuyerIdNotPresent()
        {
            string badBuyerId = "badBuyerId";
            var    spec       = new BasketWithItemsSpecification(badBuyerId);

            Assert.False(GetTestBasketCollection()
                         .AsQueryable()
                         .Any(spec.Criterias.FirstOrDefault()));
        }
        public void MatchesNoBasketsIfIdNotPresent()
        {
            int badId = -1;
            var spec  = new BasketWithItemsSpecification(badId);

            Assert.False(GetTestBasketCollection()
                         .AsQueryable()
                         .Any(spec.Criteria));
        }
        public void MatchesNoBasketsIfBuyerIdNotPresent()
        {
            string badBuyerId = "badBuyerId";
            var    spec       = new BasketWithItemsSpecification(badBuyerId);

            var result = spec.Evaluate(GetTestBasketCollection()).Any();

            Assert.False(result);
        }
        public void MatchesBasketWithGivenBuyerId()
        {
            var spec = new BasketWithItemsSpecification(_buyerId);

            var result = spec.Evaluate(GetTestBasketCollection()).FirstOrDefault();

            Assert.NotNull(result);
            Assert.Equal(_buyerId, result.BuyerId);
        }
Ejemplo n.º 21
0
        public async Task <BasketViewModel> GetOrCreateBasketForUser(string userName)
        {
            var basketSpec = new BasketWithItemsSpecification(userName);
            var basket     = (await _basketRepository.ListAsync(basketSpec)).FirstOrDefault();

            if (basket == null)
            {
                return(await CreateBasketForUser(userName));
            }
            return(CreateViewModelFromBasket(basket));
        }
Ejemplo n.º 22
0
        public async Task AddItemToShoppingCart(int basketId, int productItemId, decimal price, int quantity = 1)
        {
            var shoppingCartSpecification = new BasketWithItemsSpecification(basketId);
            var shoppingCart = await _shoppingCartRepository.FirstOrDefaultAsync(shoppingCartSpecification);

            Guard.Against.Null(shoppingCart, nameof(shoppingCart));

            shoppingCart.AddItem(productItemId, price, quantity);

            await _shoppingCartRepository.UpdateAsync(shoppingCart);
        }
Ejemplo n.º 23
0
        public async Task AddItemToBasket(int basketId, int catalogItemId, decimal price, int quantity = 1)
        {
            var basketSpec = new BasketWithItemsSpecification(basketId);
            var basket     = await _basketRepository.FirstOrDefaultAsync(basketSpec);

            Guard.Against.NullBasket(basketId, basket);

            basket.AddItem(catalogItemId, price, quantity);

            await _basketRepository.UpdateAsync(basket);
        }
        public void MatchesBasketWithGivenId()
        {
            var spec = new BasketWithItemsSpecification(_testBasketId);

            var result = GetTestBasketCollection()
                         .AsQueryable()
                         .FirstOrDefault(spec.Criteria);

            Assert.NotNull(result);
            Assert.Equal(_testBasketId, result.Id);
        }
        public async Task <IActionResult> RemoveItem(int itemId)
        {
            string userId     = User.Identity.Name;
            var    basketSpec = new BasketWithItemsSpecification(userId);
            var    basket     = await _basketRepository.FirstOrDefaultAsync(basketSpec);

            basket.RemoveItem(itemId);
            await _basketRepository.UpdateAsync(basket);

            return(RedirectToAction("Index", "Basket"));
        }
Ejemplo n.º 26
0
        public async Task TransferBasketAsync(string anonymousId, string userName)
        {
            var basketSpec = new BasketWithItemsSpecification(anonymousId);
            var basket     = (await _basketRepository.ListAsync(basketSpec)).FirstOrDefault();

            if (basket == null)
            {
                return;
            }
            basket.BuyerId = userName;
            await _basketRepository.UpdateAsync(basket);
        }
Ejemplo n.º 27
0
        public Task TransferBasket(string anonymousId, string userName)
        {
            var basketSpec = new BasketWithItemsSpecification(anonymousId);
            var basket     = _basketRepository.List(basketSpec).FirstOrDefault();

            if (basket == null)
            {
                return(Task.CompletedTask);
            }
            basket.BuyerId = userName;
            _basketRepository.Update(basket);
            return(Task.CompletedTask);
        }
Ejemplo n.º 28
0
    public async Task <BasketViewModel> GetOrCreateBasketForUser(string userName)
    {
        var basketSpec = new BasketWithItemsSpecification(userName);
        var basket     = (await _basketRepository.GetBySpecAsync(basketSpec));

        if (basket == null)
        {
            return(await CreateBasketForUser(userName));
        }
        var viewModel = await Map(basket);

        return(viewModel);
    }
Ejemplo n.º 29
0
        public async Task <int> GetBasketItemCountAsync(string userName)
        {
            Guard.Against.NullOrEmpty(userName, nameof(userName));
            var basketSpec = new BasketWithItemsSpecification(userName);
            var basket     = await _basketRepository.FirstOrDefaultAsync(basketSpec);

            if (basket == null)
            {
                _logger.LogInformation($"No basket found for ${userName}");
            }
            int count = basket.Items.Sum(i => i.Quantity); // get the sum of all basketItems(via Quantity attribute)

            return(count);
        }
Ejemplo n.º 30
0
        public async Task TransferBasketAsync(string anonymousId, string userName)
        {
            Guard.Against.NullOrEmpty(anonymousId, nameof(anonymousId));
            Guard.Against.NullOrEmpty(userName, nameof(userName));
            var basketSpec = new BasketWithItemsSpecification(anonymousId);
            var basket     = (await _basketRepository.GetAsync(basketSpec)).FirstOrDefault();

            if (basket == null)
            {
                return;
            }
            basket.BuyerId = userName;
            await _basketRepository.UpdateAsync(basket);
        }