public async Task <List <Product> > QueryMiceAsync(ProductFiltersUrlModel productFilters, decimal minPrice, decimal maxPrice) { var productCategory = await this.context.Categories .FirstAsync(x => x.Name.ToLower() == "mice"); var result = productCategory .Products .Where(p => productFilters.Gaming .Any(g => p.FullCharacteristics .Any(DetermineIfAll <FullCharacteristic>(g, "Gaming"))) && p.Price >= minPrice && p.Price <= maxPrice && p.IsDeleted == false && p.Make != null && productFilters.Make.ToList().Any(x => p.Make.ToLower() == x.ToLower() || x == "All") && productFilters.Connectivity .Any(c => p.BasicCharacteristics .Any(DetermineIfAll <BasicCharacteristic>(c, "Connectivity"))) && productFilters.Interface .Any(i => p.BasicCharacteristics .Any(DetermineIfAll <BasicCharacteristic>(i, "Interface")))) .ToList(); return(result); }
public async Task <List <Product> > QueryComputersAsync(ProductFiltersUrlModel productFilters, decimal minPrice, decimal maxPrice) { var productCategory = await this.context.Categories .FirstAsync(x => x.Name.ToLower() == "computers"); var result = productCategory .Products .Where(p => productFilters.Processor .Any(pr => p.BasicCharacteristics .Any(DetermineIfAll <BasicCharacteristic>(pr, "Processor"))) && p.Price >= minPrice && p.Price <= maxPrice && p.IsDeleted == false && productFilters.VideoCard .Any(vc => p.BasicCharacteristics .Any(DetermineIfAll <BasicCharacteristic>(vc, "VideoCard"))) && productFilters.OS .Any(os => p.FullCharacteristics .Any(DetermineIfAll <FullCharacteristic>(os, "OS"))) && productFilters.RAM .Any(ram => p.BasicCharacteristics .Any(DetermineIfAll <BasicCharacteristic>(ram, "Ram")))) .ToList(); return(result); }
public async Task <IActionResult> Products([FromQuery] ProductFiltersUrlModel productFilters, string category) { var categoryExists = await this.service.CategoryExistsAsync(category); if (!categoryExists) { return(this.RedirectToAction("Error", "Home")); } var productViewModel = new ProductsViewModel(); var products = await this.service.QueryProductsAsync(productFilters, category); var filters = await this.service.GetFiltersAsync(category); productViewModel.FilterCategory = mapper.Map <List <FilterCategoryViewModel> >(filters); productViewModel.Products = mapper.Map <List <ProductViewModel> >(products); await this.service.OrderByAsync(ref productViewModel, productFilters.OrderBy); productViewModel.Pager = new Pager(productViewModel.Products.Count, productFilters.Page, 20); productViewModel.Products = productViewModel.Products.Skip((productFilters.Page - 1) * 20).Take(20).ToList(); productViewModel.Category = category; await this.service.ApplyFiltersFromUrlAsync(productViewModel.FilterCategory, productFilters); return(this.View(productViewModel)); }
public async Task <List <Product> > QueryKeyboardsAsync(ProductFiltersUrlModel productFilters, decimal minPrice, decimal maxPrice) { var productCategory = await this.context.Categories .FirstAsync(x => x.Name.ToLower() == "keyboards"); var result = productCategory .Products .Where(p => productFilters.Type .Any(t => p.BasicCharacteristics .Any(DetermineIfAll <BasicCharacteristic>(t, "Type"))) && p.Price >= minPrice && p.Price <= maxPrice && p.IsDeleted == false && p.Make != null && productFilters.Make.Any(x => p.Make.ToLower() == x.ToLower() || x == "All") && productFilters.Interface .Any(i => p.BasicCharacteristics .Any(DetermineIfAll <BasicCharacteristic>(i, "Interface"))) && productFilters.Mechanical .Any(m => p.BasicCharacteristics .Any(DetermineIfAll <BasicCharacteristic>(m, "Mechanical")))) .ToList(); return(result); }
public async Task <List <Product> > QueryMonitorsAsync(ProductFiltersUrlModel productFilters, decimal minPrice, decimal maxPrice) { var productCategory = await this.context.Categories .FirstAsync(x => x.Name.ToLower() == "monitors"); var result = productCategory .Products .Where(p => productFilters.Resolution .Any(pr => p.BasicCharacteristics .Any(DetermineIfAll <BasicCharacteristic>(pr, "Resolution"))) && p.Price >= minPrice && p.Price <= maxPrice && p.IsDeleted == false && productFilters.Make.ToList().Any(x => p.Make.ToLower() == x.ToLower() || x == "All") && productFilters.FPS .Any(fps => p.BasicCharacteristics .Any(DetermineIfAll <BasicCharacteristic>(fps, "FPS"))) && productFilters.ReactionTime .Any(rt => p.BasicCharacteristics .Any(DetermineIfAll <BasicCharacteristic>(rt, "Reaction Time"))) && productFilters.MatrixType .Any(mt => p.BasicCharacteristics .Any(DetermineIfAll <BasicCharacteristic>(mt, "Matrix Type"))) && productFilters.DisplaySize .Any(ds => p.BasicCharacteristics .Any(DetermineIfAll <BasicCharacteristic>(ds, "Display Size"))) ) .ToList(); return(result); }
public Task ApplyFiltersFromUrlAsync(ICollection <FilterCategoryViewModel> filterCategory, ProductFiltersUrlModel urlData) { foreach (var category in filterCategory) { foreach (var filter in category.Filters) { var properties = urlData.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public); foreach (var prop in properties) { if (prop.Name == filter.Name && prop.Name != "MinPrice" && prop.Name != "MaxPrice" && prop.Name != "Page" && prop.Name != "OrderBy") { var val = this.GetPropertyValue(urlData, prop.Name) as string[]; if (val.Any(x => x == filter.Value)) { filter.IsChecked = true; break; } } } } } if (decimal.TryParse(urlData.MaxPrice, out decimal maxPrice) && decimal.TryParse(urlData.MinPrice, out decimal minPrice)) { string maxPriceString = maxPrice.ToString(); string minPriceString = minPrice.ToString(); var priceFilters = filterCategory.FirstOrDefault(x => x.ViewSubCategoryName == "Price"); priceFilters.Filters.FirstOrDefault(x => x.Name == "MinPrice").Value = minPriceString; priceFilters.Filters.FirstOrDefault(x => x.Name == "MaxPrice").Value = maxPriceString; } var orderByCategory = filterCategory.FirstOrDefault(x => x.ViewSubCategoryName == "Order By"); if (orderByCategory != null) { var orderByFilters = orderByCategory.Filters; var orderBy = orderByFilters.FirstOrDefault(x => x.Value == urlData.OrderBy); orderBy.IsChecked = true; } return(Task.CompletedTask); }
private object GetPropertyValue(ProductFiltersUrlModel urlData, string propName) { var prop = urlData.GetType().GetProperty(propName); return(prop.GetValue(urlData, null)); }
public async Task <IEnumerable <Product> > QueryProductsAsync(ProductFiltersUrlModel productFilters, string category) { decimal minPrice; decimal maxPrice; if (!decimal.TryParse(productFilters.MaxPrice, out maxPrice)) { maxPrice = 30000; } if (!decimal.TryParse(productFilters.MinPrice, out minPrice)) { minPrice = 0; } if (productFilters.Make == null) { productFilters.Make = new string[] { "All" }; } if (productFilters.Model == null) { productFilters.Model = new string[] { "All" }; } if (productFilters.OrderBy == null) { productFilters.OrderBy = "Default"; } if (productFilters.Processor == null) { productFilters.Processor = new string[] { "All" }; } if (productFilters.VideoCard == null) { productFilters.VideoCard = new string[] { "All" }; } if (productFilters.OS == null) { productFilters.OS = new string[] { "All" }; } if (productFilters.RAM == null) { productFilters.RAM = new string[] { "All" }; } if (productFilters.Resolution == null) { productFilters.Resolution = new string[] { "All" }; } if (productFilters.FPS == null) { productFilters.FPS = new string[] { "All" }; } if (productFilters.ReactionTime == null) { productFilters.ReactionTime = new string[] { "All" }; } if (productFilters.MatrixType == null) { productFilters.MatrixType = new string[] { "All" }; } if (productFilters.DisplaySize == null) { productFilters.DisplaySize = new string[] { "All" }; } if (productFilters.Gaming == null) { productFilters.Gaming = new string[] { "All" }; } if (productFilters.Interface == null) { productFilters.Interface = new string[] { "All" }; } if (productFilters.Connectivity == null) { productFilters.Connectivity = new string[] { "All" }; } if (productFilters.Type == null) { productFilters.Type = new string[] { "All" }; } if (productFilters.Mechanical == null) { productFilters.Mechanical = new string[] { "All" }; } var result = new List <Product>(); if (category.ToLower() == "laptops") { result = await QueryLaptopsAsync(productFilters, minPrice, maxPrice); } else if (category.ToLower() == "keyboards") { result = await QueryKeyboardsAsync(productFilters, minPrice, maxPrice); } else if (category.ToLower() == "mice") { result = await QueryMiceAsync(productFilters, minPrice, maxPrice); } else if (category.ToLower() == "monitors") { result = await QueryMonitorsAsync(productFilters, minPrice, maxPrice); } else if (category.ToLower() == "computers") { result = await QueryComputersAsync(productFilters, minPrice, maxPrice); } return(result); }
public async Task <IActionResult> Search([Required][MinLength(1)] string searchInput, [FromQuery] ProductFiltersUrlModel productFilters) { if (productFilters.OrderBy == null) { productFilters.OrderBy = "Default"; } if (productFilters.MinPrice == null) { productFilters.MinPrice = "0"; } if (productFilters.MaxPrice == null) { productFilters.MaxPrice = "9999"; } var model = new ProductsViewModel(); var products = await this.service.SearchForResultsAsync(searchInput, productFilters.MinPrice, productFilters.MaxPrice); var filters = await this.service.GetFiltersAsync("Search"); model.FilterCategory = mapper.Map <List <FilterCategoryViewModel> >(filters); model.Products = mapper.Map <List <ProductViewModel> >(products); model.Pager = new Pager(model.Products.Count, productFilters.Page, 20); model.Products = model.Products.Skip((productFilters.Page - 1) * 20).Take(20).ToList(); await this.service.OrderByAsync(ref model, productFilters.OrderBy); model.Category = "Search"; model.Search = true; model.SearchInput = searchInput; await this.service.ApplyFiltersFromUrlAsync(model.FilterCategory, productFilters); return(this.View("Products", model)); }
public async Task TestIfQueryComputersReturnsCorrectResults(string category) { var context = PCHUBDbContextInMemoryInitializer.InitializeContext(); var productService = new ProductServices(context); Account cloudinaryCredentials = new Account( CloudinaryAccountTests.CloudName, CloudinaryAccountTests.ApiKey, CloudinaryAccountTests.ApiSecret); var cloudinaryUtility = new Cloudinary(cloudinaryCredentials); var cloudinary = new CloudinaryServices(cloudinaryUtility); var adminProductServices = new Areas.Administration.Services.AdminProductsServices(context, cloudinary); var model = new InsertJsonProductViewModel(); var computersJson = await File.ReadAllTextAsync( @"C:\Users\velis\source\repos\PCHUBStore\PCHUBStore.Tests\AdminServicesTests\JSONComputers\computers.json"); var computers = JsonConvert.DeserializeObject <List <JsonProductModel> >(computersJson); await adminProductServices.CreateCategoryAsync(category); foreach (var computer in computers) { var jsonBasic = JsonConvert.SerializeObject(computer.BasicChars); var jsonAdvanced = JsonConvert.SerializeObject(computer.AdvancedChars); model.Category = category; model.BasicCharacteristics = jsonBasic; model.FullCharacteristics = jsonAdvanced; await adminProductServices.CreateComputerFromJSONAsync(model); } var filters = new ProductFiltersUrlModel(); if (filters.Make == null) { filters.Make = new string[] { "All" }; } if (filters.Model == null) { filters.Model = new string[] { "All" }; } if (filters.OrderBy == null) { filters.OrderBy = "Default"; } if (filters.Processor == null) { filters.Processor = new string[] { "All" }; } if (filters.VideoCard == null) { filters.VideoCard = new string[] { "All" }; } if (filters.OS == null) { filters.OS = new string[] { "All" }; } if (filters.RAM == null) { filters.RAM = new string[] { "All" }; } if (filters.Resolution == null) { filters.Resolution = new string[] { "All" }; } if (filters.FPS == null) { filters.FPS = new string[] { "All" }; } if (filters.ReactionTime == null) { filters.ReactionTime = new string[] { "All" }; } if (filters.MatrixType == null) { filters.MatrixType = new string[] { "All" }; } if (filters.DisplaySize == null) { filters.DisplaySize = new string[] { "All" }; } if (filters.Gaming == null) { filters.Gaming = new string[] { "All" }; } if (filters.Interface == null) { filters.Interface = new string[] { "All" }; } if (filters.Connectivity == null) { filters.Connectivity = new string[] { "All" }; } if (filters.Type == null) { filters.Type = new string[] { "All" }; } if (filters.Mechanical == null) { filters.Mechanical = new string[] { "All" }; } var result = await productService.QueryComputersAsync(filters, 1000, 2000); Assert.Equal(4, result.Count()); Assert.Contains(result, x => x.Title.Contains("UPGRADED Компютър Fury (Ryzen 2700, 8GB, 1TB, 120 GB SSD, GTX 1660ti 6GB GDDR6, Win10)")); filters.Processor = new string[] { "AMD", }; var secondResultTest = await productService.QueryComputersAsync(filters, 1800, 1820); Assert.Equal(3, secondResultTest.Count()); Assert.Contains(secondResultTest, x => x.ArticleNumber.Contains("101279_101305")); filters.Make = new string[] { "Dell", }; filters.MinPrice = "800"; filters.MaxPrice = "900"; var thirdResultTest = await productService.QueryComputersAsync(filters, 800, 900); Assert.Empty(thirdResultTest); }
public async Task TestIfQueryProductsReturnsCorrectResults(string category) { var context = PCHUBDbContextInMemoryInitializer.InitializeContext(); var productService = new ProductServices(context); Account cloudinaryCredentials = new Account( CloudinaryAccountTests.CloudName, CloudinaryAccountTests.ApiKey, CloudinaryAccountTests.ApiSecret); var cloudinaryUtility = new Cloudinary(cloudinaryCredentials); var cloudinary = new CloudinaryServices(cloudinaryUtility); var adminProductServices = new Areas.Administration.Services.AdminProductsServices(context, cloudinary); var model = new InsertJsonProductViewModel(); var laptopsJson = await File.ReadAllTextAsync( @"C:\Users\velis\source\repos\PCHUBStore\PCHUBStore.Tests\AdminServicesTests\JSONLaptops\laptops.json"); var laptops = JsonConvert.DeserializeObject <List <JsonProductModel> >(laptopsJson); await adminProductServices.CreateCategoryAsync(category); foreach (var laptop in laptops) { var jsonBasic = JsonConvert.SerializeObject(laptop.BasicChars); var jsonAdvanced = JsonConvert.SerializeObject(laptop.AdvancedChars); model.Category = category; model.BasicCharacteristics = jsonBasic; model.FullCharacteristics = jsonAdvanced; await adminProductServices.CreateLaptopFromJSONAsync(model); } var filters = new ProductFiltersUrlModel(); var result = await productService.QueryProductsAsync(filters, category); Assert.Equal(2, result.Count()); Assert.Contains(result, x => x.Title.Contains("Acer")); filters.Make = new string[] { "Acer", }; filters.MinPrice = "700"; filters.MaxPrice = "750"; var secondResultTest = await productService.QueryProductsAsync(filters, category); Assert.Equal(1, secondResultTest.Count()); Assert.Contains(secondResultTest, x => x.ArticleNumber.Contains("83366")); filters.Make = new string[] { "Dell", }; filters.MinPrice = "800"; filters.MaxPrice = "900"; var thirdResultTest = await productService.QueryProductsAsync(filters, category); Assert.Empty(thirdResultTest); }
public async Task TestIfApplyFiltersFromUrlAppliesFilters() { var context = PCHUBDbContextInMemoryInitializer.InitializeContext(); var productService = new ProductServices(context); var filterCategory = new List <FilterCategoryViewModel>(); var filtersPrice = new List <FilterViewModel>(); filtersPrice.Add(new FilterViewModel { Name = "MinPrice", }); filtersPrice.Add(new FilterViewModel { Name = "MaxPrice", }); filterCategory.Add(new FilterCategoryViewModel { CategoryName = "Laptops", ViewSubCategoryName = "Price", Filters = filtersPrice }); var filtersMake = new List <FilterViewModel>(); filtersMake.Add(new FilterViewModel { Name = "Make", Value = "Acer" }); filterCategory.Add(new FilterCategoryViewModel { CategoryName = "Laptops", ViewSubCategoryName = "Make", Filters = filtersMake }); var filtersOrderBy = new List <FilterViewModel>(); filtersOrderBy.Add(new FilterViewModel { Name = "Order By", Value = "Default" }); filterCategory.Add(new FilterCategoryViewModel { CategoryName = "Laptops", ViewSubCategoryName = "OrderBy", Filters = filtersOrderBy }); var urlFilters = new ProductFiltersUrlModel(); urlFilters.Page = 1; urlFilters.MinPrice = "300"; urlFilters.MaxPrice = "600"; urlFilters.Make = new string[] { "Acer", }; urlFilters.OrderBy = "Descending"; await productService.ApplyFiltersFromUrlAsync(filterCategory, urlFilters); var result = filterCategory.FirstOrDefault(x => x.CategoryName == "Laptops" && x.ViewSubCategoryName == "Price"); Assert.True(result.Filters.Any(x => x.Name == "MinPrice" && x.Value == "300")); Assert.True(result.Filters.Any(x => x.Name == "MaxPrice" && x.Value == "600")); var makeResult = filterCategory.FirstOrDefault(x => x.CategoryName == "Laptops" && x.ViewSubCategoryName == "Make"); Assert.NotNull(makeResult); Assert.True(makeResult.Filters.Any(x => x.IsChecked == true)); }
public async Task TestIfQueryMiceReturnsCorrectResults(string category) { var context = PCHUBDbContextInMemoryInitializer.InitializeContext(); var productService = new ProductServices(context); Account cloudinaryCredentials = new Account( CloudinaryAccountTests.CloudName, CloudinaryAccountTests.ApiKey, CloudinaryAccountTests.ApiSecret); var cloudinaryUtility = new Cloudinary(cloudinaryCredentials); var cloudinary = new CloudinaryServices(cloudinaryUtility); var adminProductServices = new Areas.Administration.Services.AdminProductsServices(context, cloudinary); var model = new InsertJsonProductViewModel(); var miceJson = await File.ReadAllTextAsync( @"C:\Users\velis\source\repos\PCHUBStore\PCHUBStore.Tests\AdminServicesTests\JSONMice\mice.json"); var mice = JsonConvert.DeserializeObject <List <JsonProductModel> >(miceJson); await adminProductServices.CreateCategoryAsync(category); foreach (var mouse in mice) { var jsonBasic = JsonConvert.SerializeObject(mouse.BasicChars); var jsonAdvanced = JsonConvert.SerializeObject(mouse.AdvancedChars); model.Category = category; model.BasicCharacteristics = jsonBasic; model.FullCharacteristics = jsonAdvanced; await adminProductServices.CreateMouseFromJSONAsync(model); } var filters = new ProductFiltersUrlModel(); if (filters.Make == null) { filters.Make = new string[] { "All" }; } if (filters.Model == null) { filters.Model = new string[] { "All" }; } if (filters.OrderBy == null) { filters.OrderBy = "Default"; } if (filters.Processor == null) { filters.Processor = new string[] { "All" }; } if (filters.VideoCard == null) { filters.VideoCard = new string[] { "All" }; } if (filters.OS == null) { filters.OS = new string[] { "All" }; } if (filters.RAM == null) { filters.RAM = new string[] { "All" }; } if (filters.Resolution == null) { filters.Resolution = new string[] { "All" }; } if (filters.FPS == null) { filters.FPS = new string[] { "All" }; } if (filters.ReactionTime == null) { filters.ReactionTime = new string[] { "All" }; } if (filters.MatrixType == null) { filters.MatrixType = new string[] { "All" }; } if (filters.DisplaySize == null) { filters.DisplaySize = new string[] { "All" }; } if (filters.Gaming == null) { filters.Gaming = new string[] { "All" }; } if (filters.Interface == null) { filters.Interface = new string[] { "All" }; } if (filters.Connectivity == null) { filters.Connectivity = new string[] { "All" }; } if (filters.Type == null) { filters.Type = new string[] { "All" }; } if (filters.Mechanical == null) { filters.Mechanical = new string[] { "All" }; } var result = await productService.QueryMiceAsync(filters, 50, 100); Assert.Equal(2, result.Count()); Assert.Contains(result, x => x.Title.Contains("Геймърска мишка Kingston HyperX Pulsefire Surge, RGB")); filters.Gaming = new string[] { "ДА", }; var secondResultTest = await productService.QueryMiceAsync(filters, 50, 200); Assert.Equal(3, secondResultTest.Count()); Assert.Contains(secondResultTest, x => x.ArticleNumber.Contains("110703")); filters.Make = new string[] { "Dell", }; filters.MinPrice = "800"; filters.MaxPrice = "900"; var thirdResultTest = await productService.QueryMiceAsync(filters, 800, 900); Assert.Empty(thirdResultTest); }