private async Task <List <BasketItemViewModel> > GetBasketItems(IReadOnlyCollection <BasketItem> basketItems)
        {
            var items = new List <BasketItemViewModel>();

            foreach (var item in basketItems)
            {
                var itemModel = new BasketItemViewModel
                {
                    Id            = item.Id,
                    UnitPrice     = item.UnitPrice,
                    Quantity      = item.Quantity,
                    CatalogItemId = item.CatalogItemId,
                };
                var catalogItem = await _itemRepository.GetByIdAsync(item.CatalogItemId);

                if (catalogItem != null)
                {
                    itemModel.PictureUrl  = _uriComposer.ComposePicUri(catalogItem.PictureUri);
                    itemModel.ProductName = catalogItem.Name;
                    itemModel.Description = catalogItem.Description;
                }

                items.Add(itemModel);
            }

            return(items);
        }
        private BasketViewModel BasketToViewModel(Basket basket)
        {
            var model = new BasketViewModel
            {
                BuyerId = basket.BuyerId,
                Id      = basket.Id,
                Items   = basket.Items.Select((item, index) =>
                {
                    var itemModel = new BasketItemViewModel
                    {
                        Id            = index,
                        CatalogItemId = item.CatalogItemId,
                        Quantity      = item.Quantity,
                        UnitPrice     = item.UnitPrice
                    };

                    var catalogItemTask = _session.LoadAsync <CatalogItem>("Catalog/" + item.CatalogItemId);
                    if (catalogItemTask.Status != TaskStatus.RanToCompletion)
                    {
                        throw new InvalidOperationException("Should never happen: " + catalogItemTask.Status, catalogItemTask.Exception);
                    }

                    var catalogItem        = catalogItemTask.Result;
                    itemModel.OldUnitPrice = catalogItem.Price;
                    itemModel.ProductName  = catalogItem.Name;
                    itemModel.PictureUrl   = _uriComposer.ComposePicUri(catalogItem.PictureUri);

                    return(itemModel);
                }).ToList()
            };

            return(model);
        }
Beispiel #3
0
        private BasketViewModel CreateViewModelFromBasket(Basket basket)
        {
            var pricesAndQuantities = basket.Items.Select(i => new System.Tuple <decimal, int>(i.UnitPrice, i.Quantity));
            var totals = OrderCalculator.CalculateTotals(pricesAndQuantities);

            return(new BasketViewModel
            {
                Id = basket.Id,
                BuyerId = basket.BuyerId,
                SubTotal = totals.SubTotal,
                Tax = totals.Tax,
                Total = totals.Total,
                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()
            });
        }
        public async Task <IActionResult> AddItem([FromQuery] int ProductId)
        {
            ProductDto product = null;

            try
            {
                product = await _productFacade.GetProduct(ProductId);
            }
            catch (HttpRequestException)
            {
                _logger.LogWarning("Exception Occured using Basket Facade");
                product = null;
            }

            BasketItemViewModel viewModel = new BasketItemViewModel
            {
                ProductId   = product.ProductId,
                Name        = product.Name,
                Description = product.Description,
                BrandId     = product.BrandId,
                Brand       = product.Brand,
                CategoryId  = product.CategoryId,
                Category    = product.Category,
                Price       = product.Price
            };

            return(View(viewModel));
        }
 void AndGiven_the_DotNet_Black_And_White_Mug_IsSelected()
 {
     //Arrange
     _expectedDotNetBlackAndWhiteMug = new BasketItemViewModel {
         Id = 2, ProductName = ".NET Black & White Mug"
     };
 }
Beispiel #6
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);
        }
Beispiel #7
0
        public void Given_basket_item_list_and_quantity_of_items_should_be_sum_if_order_food_is_same_as_previus(
            FoodViewModel food)
        {
            // given
            var publisher = new Subject <IBasketItemViewModel>();


            var viewModel       = ClassUnderTest;
            var firstBasketItem = new BasketItemViewModel(food)
            {
                Quantity = 2
            };
            var secondBasketItem = new BasketItemViewModel(food)
            {
                Quantity = 3
            };

            // when
            publisher.OnNext(firstBasketItem);
            publisher.OnNext(secondBasketItem);

            // then
            Assert.That(firstBasketItem.TotalPrice, Is.EqualTo(5 * food.Price));
            Assert.That(viewModel.Items.Count, Is.EqualTo(1));

            Assert.That(viewModel.Items, Is.Not.Null);
            // ReSharper disable once PossibleNullReferenceException
            Assert.That(viewModel.Items.FirstOrDefault().Quantity, Is.EqualTo(5));
        }
Beispiel #8
0
        public async Task <List <BasketItemViewModel> > GetBasketItems(IReadOnlyCollection <BasketItem> basketItems)
        {
            var catalogItemsSpecification = new CatalogItemsSpecification(basketItems.Select(b => b.CatalogItemId).ToArray());
            var catalogItems = await _itemRepository.ListAsync(catalogItemsSpecification);

            var items = basketItems.Select(basketItem =>
            {
                var catalogItem = catalogItems.FirstOrDefault(c => c.id == basketItem.CatalogItemId);

                //if basketItem.CatalogItemId == catalogItem.id make a basketViewModel out of both and return it

                var basketItemViewModel = new BasketItemViewModel
                {
                    Id            = basketItem.id,
                    CatalogItemId = basketItem.CatalogItemId,
                    ProductName   = catalogItem.Name,
                    Quantity      = basketItem.Quantity,
                    UnitPrice     = basketItem.UnitPrice,
                    PictureUri    = _uriComposer.ComposePictureUri(catalogItem.PictureUri)
                };
                return(basketItemViewModel);
            }).ToList();

            return(items);
        }
        private async Task <List <BasketItemViewModel> > GetBasketItems(IReadOnlyCollection <BasketItem> basketItems)
        {
            var items = new List <BasketItemViewModel>();

            foreach (var item in basketItems)
            {
                var itemModel = new BasketItemViewModel
                {
                    //Id = item.Id.ToString(),
                    StoreId     = item.StoreId,
                    SecondaryId = item.SecondaryId,
                    ProductName = item.Name,
                    UnitPrice   = item.Price,
                    Quantity    = item.Amount,
                    ProductId   = item.ProductId,
                    OptionId    = item.OptionId,
                    PictureUrl  = _uriComposer.ComposePicUri(item.PictureUri)
                };
                //var catalogItem = _itemRepository.find(item.CatalogItemId);

                //itemModel.ProductName = catalogItem.Name;
                items.Add(itemModel);
            }

            return(items);
        }
Beispiel #10
0
        public async Task <IActionResult> AddItemToBasket(BasketItemCreateViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest("Invalid model"));
            }

            BasketViewModel basket = await _basketViewModelService.GetOrCreateBasketForUserAsync(User.Identity.Name);

            var priceResult = Pounds.Create(model.PriceInPounds);

            if (priceResult.IsFailure)
            {
                return(BadRequest(priceResult.Error));
            }

            var result = await _basketService.AddItemToBasketAsync(basket.Id, model.ProductId, model.Quantity, priceResult.Value);

            if (result.IsFailure)
            {
                return(NotFound(result.Error));
            }

            BasketItemViewModel item = await _basketViewModelService.GetBasketItemForUserAsync(User.Identity.Name, result.Value);

            return(CreatedAtAction("GetItem", new { id = item.Id }, item));
        }
 //partial view for showing basket content in Menu, MyOrders, Checkout pages
 public PartialViewResult BasketItemTable(BasketItemViewModel model)
 {
     if (model == null || model.BasketItemDetail == null || model.BasketItemDetail.Count() == 0)
     {
         model = basketService.GetBasketItems(this.HttpContext);
     }
     return(PartialView(model));
 }
Beispiel #12
0
        private string Display(BasketItemViewModel item)

        {
            return("\t" + JsonConvert.SerializeObject(item,
                                                      new JsonSerializerSettings {
                NullValueHandling = NullValueHandling.Ignore
            }));
        }
Beispiel #13
0
        public void Add_orders_should_change_orders_count_as_string(FoodViewModel food)
        {
            var basketItemViewModel = new BasketItemViewModel(food);
            var publisher           = new Subject <IBasketItemViewModel>();


            var viewModel = ClassUnderTest;

            publisher.OnNext(basketItemViewModel);
        }
Beispiel #14
0
        public async Task <ActionResult <BasketItemViewModel> > GetItem(int id)
        {
            BasketItemViewModel item = await _basketViewModelService.GetBasketItemForUserAsync(User.Identity.Name, id);

            if (item == null)
            {
                return(NotFound(string.Format(ErrorMessage.BasketWithItemIdDoesntExists, id)));
            }
            return(item);
        }
Beispiel #15
0
        public async Task <IActionResult> Index()
        {
            var basket      = _basketService.GetByUser(_userManager.GetUserId(User));
            var basketItems = _basketItemService.GetByBasket(Convert.ToInt32(basket.BasketId));
            BasketItemViewModel basketItemViewModel = new BasketItemViewModel()
            {
                BasketItems = basketItems
            };

            return(View(basketItemViewModel));
        }
Beispiel #16
0
        public IActionResult PartialBaskett()
        {
            var basket      = _basketService.GetByUser(_userManager.GetUserId(User));
            var basketItems = _basketItemService.GetByBasket(Convert.ToInt32(basket.BasketId));
            BasketItemViewModel basketItemViewModel = new BasketItemViewModel()
            {
                BasketItems = basketItems
            };

            return(PartialView("PartialBasketView", basketItemViewModel));
        }
        public async Task <IActionResult> AddBasketItem(string courseId)
        {
            var course = await _catalogService.GetByCourseId(courseId);

            var basketItem = new BasketItemViewModel {
                CourseId = course.Id, CourseName = course.Name, Price = course.Price
            };

            await _basketService.AddBasketItem(basketItem);

            return(RedirectToAction(nameof(Index)));
        }
        public IActionResult Delete(BasketItemViewModel item, long userId = 1)
        {
            HttpClient client = new HttpClient();

            var result = client.PutAsJsonAsync("https://localhost:44309/api/basket/" + userId, item).Result;

            if (result.IsSuccessStatusCode)
            {
                Ok();
            }
            return(RedirectToAction("Index"));
        }
Beispiel #19
0
        public AndConstraint <BasketPageAssertions> OnlyHave(BasketItemViewModel basketItem)
        {
            var actualbasketItems = Subject.Items;

            Execute
            .Assertion
            .ForCondition(actualbasketItems.Single(a => AreEqual(a, basketItem)) != null)
            .FailWith($"\r\nExpected basket with : " +
                      $"\r\n{Display(basketItem)}, \r\n but was {Display(actualbasketItems)}");

            return(new AndConstraint <BasketPageAssertions>(this));
        }
Beispiel #20
0
        public async Task <IActionResult> UpdateBasket(int id, BasketItemViewModel basketItem)
        {
            try
            {
                await _basketService.UpdateBasket(basketItem, id);

                return(Ok());
            }
            catch (Exception)
            {
                return(BadRequest($"parameter 'id' Incorrect or Object is null"));
            }
        }
        public static OrderItemViewModel ConvertD(BasketItemViewModel items)
        {
            if (items == null)
            {
                throw new ArgumentNullException(nameof(items));
            }

            return(new OrderItemViewModel
            {
                Product = items.product,
                Quantity = items.Quantity
            });
        }
Beispiel #22
0
        public async Task <IHttpActionResult> RemoveBasketItemAsync(BasketItemViewModel productItemViewModel)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest("Invalid data"));
            }

            var response = await _basketCartService.RemoveItemAsync(productItemViewModel.CustomerId, productItemViewModel.ProductId);

            if (response.status == 500)
            {
                return(new HttpActionResult(HttpStatusCode.InternalServerError, JsonConvert.SerializeObject(response)));
            }

            return(new HttpActionResult(HttpStatusCode.OK, JsonConvert.SerializeObject(response)));
        }
Beispiel #23
0
        public async Task <IHttpActionResult> ClearCustomerBasketAsync(BasketItemViewModel productItemViewModel)
        {
            if (productItemViewModel.CustomerId <= 0)
            {
                return(BadRequest("Not a valid customer id"));
            }

            var response = await _basketCartService.ClearCustomerBasketAsync(productItemViewModel.CustomerId);

            if (response.status == 500)
            {
                return(new HttpActionResult(HttpStatusCode.InternalServerError, JsonConvert.SerializeObject(response)));
            }

            return(new HttpActionResult(HttpStatusCode.OK, JsonConvert.SerializeObject(response)));
        }
        public IActionResult AddItem(CreateBasketItemViewModel item, long userId = 1)
        {
            if (item.Quantity < 1)
            {
                return(BadRequest($"{nameof(item.Quantity)} <= 0"));
            }
            BasketItemViewModel bItem  = new BasketItemViewModel();
            HttpClient          client = new HttpClient();
            var result = client.PostAsJsonAsync("https://localhost:44309/api/basket/" + userId, item).Result;

            if (result.IsSuccessStatusCode)
            {
                return(RedirectToAction("GetProductById", "Product", new { id = item.ProductId }));
            }

            return(RedirectToAction("Add", new { productId = item.ProductId }));
        }
        public async Task AddBasketItem(BasketItemViewModel basketItemViewModel)
        {
            var basket = await Get();

            if (basket != null)
            {
                if (!basket.BasketItems.Any(x => x.CourseId == basketItemViewModel.CourseId))
                {
                    basket.BasketItems.Add(basketItemViewModel);
                }
            }
            else
            {
                basket = new BasketViewModel();
                basket.BasketItems.Add(basketItemViewModel);
            }

            await SaveOrUpdate(basket);
        }
Beispiel #26
0
        public async Task AddItemToBasket(BasketItemViewModel basketItem)
        {
            var basket = await _basketRepository.GetAsync(basketItem.UserId);

            if (basket == null)
            {
                throw new ArgumentNullException();
            }

            basket.AddItemToBasket(
                basketItem.FoodId,
                basketItem.FoodName,
                basketItem.UnitPrice,
                basketItem.OldUnitPrice,
                basketItem.PictureUrl,
                basketItem.Quantity);

            await _basketRepository.UnitOfWork.SaveChangesAsync();
        }
Beispiel #27
0
        public async Task UpdateBasket(BasketItemViewModel basketItem, int id)
        {
            var userId = ExtensionMethod.GetUserId(_httpContextAccessor.HttpContext);

            if (userId == null)
            {
                throw new ArgumentNullException("userId invalied");
            }
            ;

            var basket = await _basketRepository.GetAsync(userId);

            if (basket.Id != id)
            {
                throw new ArgumentNullException();
            }
            basket.UpdateBasket(basketItem.FoodId, basketItem.Quantity);
            await _basketRepository.UnitOfWork.SaveChangesAsync();
        }
        private BasketItemViewModel GetBasketItems(Basket basket)
        {
            BasketItemViewModel basketItemViewModel = new BasketItemViewModel();

            if (basket != null)
            {
                var results = (from b in basket.BasketItems
                               join p in productContext.Include(p => p.Images) on b.ProductId equals p.Id
                               select new BasketItemDetail()
                {
                    Id = b.Id,
                    Quantity = b.Quantity,
                    ProdctName = p.Name,
                    ProdctId = p.Id,
                    Image = p.Images.FirstOrDefault().Path,
                    Price = (p.Discount == 0)? p.Price : p.Price - ((p.Price * p.Discount) / 100)
                }
                               ).ToList();

                basketItemViewModel.BasketItemDetail = results;

                if (basket.CouponName != null)
                {
                    basketItemViewModel.DiscountItem            = new DiscountItem();
                    basketItemViewModel.DiscountItem.CouponName = basket.CouponName;
                    basketItemViewModel.DiscountItem.Price      = basket.CouponName == "FREE100" ? 100 : 200;
                }

                basketItemViewModel.DeliveryMethod          = new DeliveryMethod();
                basketItemViewModel.DeliveryMethod.Delivery = basket.Delivery;

                if (basket.Delivery == "Home")
                {
                    basketItemViewModel.DeliveryMethod.Price += 30;
                }
                else if (basket.Delivery == "Express")
                {
                    basketItemViewModel.DeliveryMethod.Price += 50;
                }
            }
            return(basketItemViewModel);
        }
Beispiel #29
0
        private async Task <List <BasketItemViewModel> > GetBasketItems(IReadOnlyCollection <BasketItem> basketItems)
        {
            var items = new List <BasketItemViewModel>();

            foreach (var item in basketItems)
            {
                var itemModel = new BasketItemViewModel
                {
                    Id            = item.Id,
                    Quantity      = item.Quantity,
                    ProductItemId = item.ProductItemId
                };
                var productItem = await _itemRepository.GetByIdAsync(item.ProductItemId);

                itemModel.ProductName = productItem.Name;
                items.Add(itemModel);
            }

            return(items);
        }
Beispiel #30
0
        public void Can_add_selected_item_basket()
        {
            //Arrange
            var homePage = Browser.NavigateToInitial <HomePage>();
            var expectedDotNetBlackAndWhiteMug = new BasketItemViewModel {
                Id = 2, ProductName = ".NET Black & White Mug"
            };

            //Act
            var actualPage = homePage.AddToBasketByProductId(2);

            //Assert
            actualPage
            .Should()
            .Be <BasketPage>(withExpectedTitle: "Basket - Microsoft.eShopOnWeb")
            .And
            .HaveUrlEndingWith("/Basket")
            .And
            .OnlyHave(expectedDotNetBlackAndWhiteMug);
        }