public async Task Index_GivenAValidProductId_ShouldReturnProperView() { //Arrange const int productId = 5; var productServiceMock = new Mock <IProductsService>(); productServiceMock.Setup(x => x.GetProductDetailsAsync(productId)) .ReturnsAsync(this.GetProductDetails()); var controller = new ProductDetailsController(productServiceMock.Object); //Act IActionResult result = await controller.Index(productId); //Assert Assert.IsType <ViewResult>(result); }
protected void btnCheckOut_Click(object sender, EventArgs e) { if (Request.Cookies["cartid"] != null) { if (!string.IsNullOrEmpty(HttpContext.Current.User.Identity.Name)) { var value = Request.Cookies["cartid"].Value; ProductDetailsController prd = new ProductDetailsController(); prd.AddOrder(Convert.ToInt16(value), HttpContext.Current.User.Identity.Name); var httpCookie = Response.Cookies["cartid"]; if (httpCookie != null) { httpCookie.Expires = DateTime.Now.AddDays(-1); } } } }
public void Details_Returns_with_Correct_View() { #region Arrange const int expected_product_id = 1; const decimal expected_price = 10m; var expected_name = $"Product id {expected_product_id}"; var expected_brand_name = $"Brand of product {expected_product_id}"; var product_data_mock = new Mock <IProductData>(); product_data_mock.Setup(p => p.GetProductById(It.IsAny <int>())) .Returns <int>(id => new ProductDTO( id, $"Product id {id}", 1, expected_price, $"img{id}.png", new BrandDTO(1, $"Brand of product {id}", 1, 1), new SectionDTO(1, $"Section of product {id}", 1, null, 1) )); var controller = new ProductDetailsController(product_data_mock.Object); #endregion #region Act var result = controller.ProductDetailsIndex(expected_product_id); #endregion #region Assert var view_result = Assert.IsType <ViewResult>(result); var model = Assert.IsAssignableFrom <ProductViewModel>(view_result.Model); Assert.Equal(expected_product_id, model.Id); Assert.Equal(expected_name, model.Name); Assert.Equal(expected_price, model.Price); Assert.Equal(expected_brand_name, model.Brand); #endregion }
public ProductsDetails() { Controller = new ProductDetailsController(); DataContext = Controller; InitializeComponent(); }
/// <summary> /// Get the Details for a particular product. /// </summary> /// <param name="productId">The product id you need the details for.</param> /// <returns>The View for that product.</returns> public async Task <ActionResult> Index(string productId = "") { EcommerceContext ecommerceContext = ServiceUtilities.GetEcommerceContext(this.HttpContext); OrgUnitOperationsHandler orgUnitOperationsHandler = new OrgUnitOperationsHandler(ecommerceContext); PagedResult <Category> categories = await orgUnitOperationsHandler.GetNavigationalHierarchyCategories(Utilities.DefaultQuerySettings); IEnumerable <long> rawCategoryIds = categories.Select(c => c.RecordId); ObservableCollection <long> productIds = null; Product prod = null; Collection <CustomLink> breadcrumbNavLinks = new Collection <CustomLink>(); long productIdentifier; if (string.IsNullOrEmpty(productId) || !long.TryParse(productId, out productIdentifier)) { RetailLogger.Log.OnlineStoreInvalidProductIdProvided(productId); return(this.RedirectToAction(HomeController.DefaultActionName, HomeController.ControllerName)); } else { // add productId to an ObservableCollection productIds = new ObservableCollection <long>(); productIds.Add(productIdentifier); ProductSearchCriteria searchCriteria = new ProductSearchCriteria { DataLevelValue = 4, Ids = productIds }; // try and get product information ProductOperationsHandler productOperationsHandler = new ProductOperationsHandler(ecommerceContext); PagedResult <ProductCatalog> productCatalogs = await productOperationsHandler.GetProductCatalogs(Utilities.DefaultQuerySettings); IEnumerable <long> activeCatalogIds = productCatalogs.Results.Select(pc => pc.RecordId); PagedResult <Product> products = await productOperationsHandler.SearchProducts(searchCriteria, activeCatalogIds, Utilities.DefaultQuerySettings); if (!products.Results.Any()) { var message = string.Format("ProductIds: {0}.", string.Join(",", productIds)); RetailLogger.Log.OnlineStoreNoProductsFound(message); return(this.RedirectToAction(HomeController.DefaultActionName, HomeController.ControllerName)); } prod = products.Results.First <Product>(); // Breadcrumb Navigation Links // add current item breadcrumbNavLinks.Add(new CustomLink("/ProductDetails?productId=" + prod.RecordId, prod.ProductName)); Category currentCategory = this.GetCategoryById(prod.CategoryIds.First(), categories); while (currentCategory.ParentCategory != 0) { breadcrumbNavLinks.Add(new CustomLink("/ProductGallery?categoryId=" + currentCategory.RecordId, currentCategory.Name)); currentCategory = this.GetCategoryById(currentCategory.ParentCategory, categories); } breadcrumbNavLinks.Add(new CustomLink("/", "Home")); } prod = (await ProductDetailsController.PopulateViewSpecificProductInfo(new Product[] { prod }, ecommerceContext)).FirstOrDefault(); return(this.View(ProductDetailsController.ProductDetailsViewName, new ProductDetailsModel(prod, breadcrumbNavLinks))); }
/// <summary> /// Return View with optional search criteria added. /// </summary> /// <param name="categoryId">Required: Category id to show products for.</param> /// <param name="filterBrands">List of brands to show (comma separated).</param> /// <param name="filterCategories">List of categories to show (comma separated).</param> /// <returns>View of Products.</returns> public async Task <ActionResult> Index(string categoryId = "", string[] filterBrands = null, string[] filterCategories = null) { EcommerceContext ecommerceContext = ServiceUtilities.GetEcommerceContext(this.HttpContext); OrgUnitOperationsHandler orgUnitOperationsHandler = new OrgUnitOperationsHandler(ecommerceContext); PagedResult <Category> categories = await orgUnitOperationsHandler.GetNavigationalHierarchyCategories(Utilities.DefaultQuerySettings); IEnumerable <long> rawCategoryIds = categories.Select(c => c.RecordId); // determine what category to load products for, if null, load all products ObservableCollection <long> categoryIds; if (string.IsNullOrEmpty(categoryId)) { categoryIds = new ObservableCollection <long>(rawCategoryIds); } else { categoryIds = new ObservableCollection <long>(); categoryIds.Add(long.Parse(categoryId)); } // Category Id to Name Mapping Dictionary <long, string> mapping = new Dictionary <long, string>(); foreach (Category category in categories) { mapping.Add(category.RecordId, category.Name); } // Retrieving Products - make sure we include products from descendant categories too ProductSearchCriteria searchCriteria = new ProductSearchCriteria { DataLevelValue = 4, CategoryIds = categoryIds, IncludeProductsFromDescendantCategories = true }; // try and get product information ProductOperationsHandler productOperationsHandler = new ProductOperationsHandler(ecommerceContext); PagedResult <ProductCatalog> productCatalogs = await productOperationsHandler.GetProductCatalogs(Utilities.DefaultQuerySettings); IEnumerable <long> activeCatalogIds = productCatalogs.Results.Select(pc => pc.RecordId); PagedResult <Product> products = await productOperationsHandler.SearchProducts(searchCriteria, activeCatalogIds, Utilities.DefaultQuerySettings); // Breadcrumb Navigation Links Collection <CustomLink> breadcrumbNavLinks = new Collection <CustomLink>(); Category currentCategory = this.GetCategoryById(long.Parse(categoryId), categories); while (!currentCategory.ParentCategory.Equals((long?)0)) { breadcrumbNavLinks.Add(new CustomLink("/ProductGallery?categoryId=" + currentCategory.RecordId, currentCategory.Name)); currentCategory = this.GetCategoryById(currentCategory.ParentCategory, categories); } breadcrumbNavLinks.Add(new CustomLink("/", "Home")); // Filter Mapping Dictionary <string, string[]> filters = new Dictionary <string, string[]>(); filters.Add("brand", filterBrands); filters.Add("categories", filterCategories); IEnumerable <Product> productList = await ProductDetailsController.PopulateViewSpecificProductInfo(products.Results, ecommerceContext); // create a new product gallery model for the view ProductGalleryModel productGalleryModel = new ProductGalleryModel(long.Parse(categoryId), productList, breadcrumbNavLinks, mapping, filters); return(this.View(ProductGalleryController.ProductGalleryViewName, productGalleryModel)); }