public CustomersController(FilterDbContext context)
        {
            context.Database.EnsureCreated();
            if (!context.Customers.Any())
            {
                Customer customer = new Customer
                {
                    Name           = "First",
                    CustomMetadata = "{\"CustomPropA\": \"CustomValA\" }"
                };
                context.Customers.Add(customer);

                customer = new Customer
                {
                    Name           = "Second",
                    CustomMetadata = "{\"CustomPropB\": \"CustomValB\" }"
                };
                context.Customers.Add(customer);

                customer = new Customer
                {
                    Name           = "Third",
                    CustomMetadata = "{\"CustomPropA\": \"CustomValA\", \"CustomPropC\": \"CustomValC\" }"
                };
                context.Customers.Add(customer);

                context.SaveChanges();
            }

            _context = context;
        }
Beispiel #2
0
        public void Should_be_able_to_handle_nullable_source()
        {
            using (var context = new FilterDbContext(fixture.ConnectionString))
                using (var transaction = context.Database.BeginTransaction())
                {
                    context.People.AddRange(People);
                    context.SaveChanges();

                    var @return = context.People.Filter(new
                    {
                        Rating = (Range <decimal>) "[4.5,5.0]"
                    });

                    Assert.Equal(1, @return.Count());

                    transaction.Rollback();
                }
        }
Beispiel #3
0
        public void Can_filter_datetimeoffset_via_entity_framework()
        {
            using (var context = new FilterDbContext(fixture.ConnectionString))
                using (var transaction = context.Database.BeginTransaction())
                {
                    context.People.AddRange(People);
                    context.SaveChanges();

                    var @return = context.People.Filter(new
                    {
                        FavoriteDateTimeOffset = (Range <DateTimeOffset>) "[2010-01-01,2010-01-02)"
                    });

                    Assert.Equal(1, @return.Count());

                    transaction.Rollback();
                }
        }
Beispiel #4
0
        public void Can_filter_nullable_models_via_entity_framework()
        {
            Database.SetInitializer(new DropCreateDatabaseAlways <FilterDbContext>());

            using (var context = new FilterDbContext(fixture.ConnectionString))
                using (var transaction = context.Database.BeginTransaction())
                {
                    context.People.AddRange(People);
                    context.SaveChanges();

                    var @return = context.People.Filter(new
                    {
                        Rating = new decimal?(4.5m)
                    });

                    Assert.Equal(1, @return.Count());

                    transaction.Rollback();
                }
        }
Beispiel #5
0
        public void Should_optimize_arrays_containing_a_single_value()
        {
            var singleParameter = new
            {
                FirstName = "Tim"
            };

            var collectionParameter = new
            {
                FirstName = new[] { "Tim" }
            };

            using (var context = new FilterDbContext(fixture.ConnectionString))
            {
                IQueryable <Person> query = context.People.AsNoTracking();

                var expectedQuery = query.Where(x => x.FirstName == singleParameter.FirstName);
                var actualQuery   = query.Filter(collectionParameter);

                Assert.Equal(expectedQuery.ToString(), actualQuery.ToString(), StringComparer.OrdinalIgnoreCase);
            }
        }
Beispiel #6
0
        public void Should_not_optimize_collections_containing_multiple_values()
        {
            var singleParameter = new
            {
                FirstName = "Tim"
            };

            var collectionParameter = new
            {
                FirstName = new List <string> {
                    "Tim", "John"
                }
            };

            using (var context = new FilterDbContext(fixture.ConnectionString))
            {
                IQueryable <Person> query = context.People.AsNoTracking();

                var expectedQuery = query.Filter(singleParameter);
                var actualQuery   = query.Filter(collectionParameter);

                Assert.NotEqual(expectedQuery.ToString(), actualQuery.ToString(), StringComparer.OrdinalIgnoreCase);
            }
        }
Beispiel #7
0
        //public IEnumerable<laptop> getAll()
        //{
        //    var searchQuery = db.laptops;
        //    return searchQuery;
        //}
        //public IHttpActionResult GetProduct(string features_hash)
        //{
        //    SearchParameters seacrhParameters = new SearchParameters();
        //    string[] ListQuery;
        //    ListQuery = features_hash.Split('_');
        //    foreach (var item in ListQuery)
        //    {
        //        string typeQuery = item.Split('-')[0];
        //        string QueryData = item.Split('-')[1];
        //        switch (typeQuery)
        //        {
        //            case "1":
        //                seacrhParameters.SearchTerm = QueryData;
        //                break;
        //            case "2":
        //                seacrhParameters.Company.Add(QueryData);
        //                break;
        //            case "3":
        //                seacrhParameters.ScreenResolution.Add(QueryData);
        //                break;
        //            case "4":
        //                seacrhParameters.TypeName.Add(QueryData);
        //                break;
        //            case "5":
        //                seacrhParameters.Inches.Add(float.Parse(QueryData));
        //                break;
        //            case "6":
        //                seacrhParameters.CPU.Add(QueryData);
        //                break;
        //            case "7":
        //                seacrhParameters.Speed.Add(float.Parse(QueryData));
        //                break;
        //            case "8":
        //                seacrhParameters.Memory.Add(QueryData);
        //                break;
        //            case "9":
        //                seacrhParameters.Ram.Add(Int32.Parse(QueryData));
        //                break;
        //            case "10":
        //                seacrhParameters.GPU.Add(QueryData);
        //                break;
        //            case "11":
        //                seacrhParameters.OS.Add(QueryData);
        //                break;
        //            case "12":
        //                seacrhParameters.PriceLow = double.Parse(QueryData);
        //                break;
        //            case "13":
        //                seacrhParameters.PriceHigh = double.Parse(QueryData);
        //                break;
        //            case "14":
        //                seacrhParameters.Weight.Add(float.Parse(QueryData));
        //                break;
        //            case "15":
        //                switch (QueryData)
        //                {
        //                    case "1":
        //                        seacrhParameters.SortBy = SortCriteria.PriceHighToLow;
        //                        break;
        //                    case "2":
        //                        seacrhParameters.SortBy = SortCriteria.PriceLowToHigh;
        //                        break;
        //                    default:
        //                        seacrhParameters.SortBy = SortCriteria.Relevance;
        //                        break;
        //                }
        //                break;
        //            default:
        //                break;
        //        }
        //    }
        //    var searchQuery = new SearchBuilder().
        //    SetSearchTerm(seacrhParameters.SearchTerm).//1
        //    SetCompany(seacrhParameters.Company).//2
        //    SetScreenResolution(seacrhParameters.ScreenResolution).//3
        //    SetTypeName(seacrhParameters.TypeName).//4
        //    SetInch(seacrhParameters.Inches).//5
        //    SetCPU(seacrhParameters.CPU).//6
        //    SetSpeed(seacrhParameters.Speed).//7
        //    SetMemory(seacrhParameters.Memory).//8
        //    SetRam(seacrhParameters.Ram).//9
        //    SetGPU(seacrhParameters.GPU).//10
        //    SetOS(seacrhParameters.OS).//11
        //    SetPriceLow(seacrhParameters.PriceLow).//12
        //    SetPriceHigh(seacrhParameters.PriceHigh).//13
        //    SetWeight(seacrhParameters.Weight)//14
        //    .SetSortBy(SortCriteria.PriceHighToLow)//15
        //    .Build(db);
        //    return Ok(searchQuery);
        //}
        public IHttpActionResult Search(SearchParameters searchParameters)
        {
            FilterDbContext db = new FilterDbContext();

            var searchQuery = new SearchBuilder().
                              SetSearchTerm(searchParameters.SearchTerm).
                              SetCompany(searchParameters.Company).
                              SetScreenResolution(searchParameters.ScreenResolution).
                              SetTypeName(searchParameters.TypeName).
                              SetInch(searchParameters.Inches).
                              SetCPU(searchParameters.CPU).
                              SetSpeed(searchParameters.Speed).
                              SetMemory(searchParameters.Memory).
                              SetRam(searchParameters.Ram).
                              SetGPU(searchParameters.GPU).
                              SetOS(searchParameters.OS).
                              SetPriceLow(searchParameters.PriceLow).
                              SetPriceHigh(searchParameters.PriceHigh).
                              SetWeight(searchParameters.Weight)
                              .SetSortBy(SortCriteria.PriceHighToLow)
                              .Build(db);

            return(Ok(searchQuery));
        }
Beispiel #8
0
        //-----------

        public IEnumerable <laptop> Build(FilterDbContext Entities)
        {
            var predicate = PredicateExtensions.PredicateExtensions.Begin <laptop>();

            //search term
            if (!String.IsNullOrEmpty(_searchParameters.SearchTerm))
            {
                predicate = predicate.And(e => e.Product.Contains(_searchParameters.SearchTerm));
            }
            //price

            if (_searchParameters.PriceLow > 0 && _searchParameters.PriceHigh > 0)
            {
                predicate = predicate.And(e => e.Price_euros >= _searchParameters.PriceLow && e.Price_euros <= _searchParameters.PriceHigh);
            }
            //company
            if (_searchParameters.Company.Count > 0)
            {
                var temppredicate = PredicateExtensions.PredicateExtensions.Begin <laptop>();

                foreach (var item in _searchParameters.Company)
                {
                    temppredicate = temppredicate.Or(e => e.Company == item);
                }
                predicate = predicate.And(temppredicate);
            }
            //ScreenResolution
            if (_searchParameters.ScreenResolution.Count > 0)
            {
                var temppredicate = PredicateExtensions.PredicateExtensions.Begin <laptop>();
                foreach (var item in _searchParameters.ScreenResolution)
                {
                    temppredicate = temppredicate.Or(e => e.ScreenResolution == item);
                }
                predicate = predicate.And(temppredicate);
            }
            if (_searchParameters.TypeName.Count > 0)
            {
                var temppredicate = PredicateExtensions.PredicateExtensions.Begin <laptop>();
                foreach (var item in _searchParameters.TypeName)
                {
                    temppredicate = temppredicate.Or(e => e.TypeName == item);
                }
                predicate = predicate.And(temppredicate);
            }
            //Inches
            if (_searchParameters.Inches.Count > 0)
            {
                var temppredicate = PredicateExtensions.PredicateExtensions.Begin <laptop>();
                foreach (var item in _searchParameters.Inches)
                {
                    temppredicate = temppredicate.Or(e => e.Inches == item);
                }
                predicate = predicate.And(temppredicate);
            }
            //CPU
            if (_searchParameters.CPU.Count > 0)
            {
                var temppredicate = PredicateExtensions.PredicateExtensions.Begin <laptop>();
                foreach (var item in _searchParameters.CPU)
                {
                    temppredicate = temppredicate.Or(e => e.Cpu == item);
                }
                predicate = predicate.And(temppredicate);
            }
            //Speed
            if (_searchParameters.Speed.Count > 0)
            {
                var temppredicate = PredicateExtensions.PredicateExtensions.Begin <laptop>();
                foreach (var item in _searchParameters.Speed)
                {
                    temppredicate = temppredicate.Or(e => e.Speed == item);
                }
                predicate = predicate.And(temppredicate);
            }
            //Memory
            if (_searchParameters.Memory.Count > 0)
            {
                var temppredicate = PredicateExtensions.PredicateExtensions.Begin <laptop>();
                foreach (var item in _searchParameters.Memory)
                {
                    temppredicate = temppredicate.Or(e => e.Memory == item);
                }
                predicate = predicate.And(temppredicate);
            }
            //Ram
            if (_searchParameters.Ram.Count > 0)
            {
                var temppredicate = PredicateExtensions.PredicateExtensions.Begin <laptop>();
                foreach (var item in _searchParameters.Ram)
                {
                    temppredicate = temppredicate.Or(e => e.Ram == item);
                }
                predicate = predicate.And(temppredicate);
            }
            //GPU
            if (_searchParameters.GPU.Count > 0)
            {
                var temppredicate = PredicateExtensions.PredicateExtensions.Begin <laptop>();
                foreach (var item in _searchParameters.GPU)
                {
                    temppredicate = temppredicate.Or(e => e.Gpu == item);
                }
                predicate = predicate.And(temppredicate);
            }
            if (_searchParameters.OS.Count > 0)
            {
                var temppredicate = PredicateExtensions.PredicateExtensions.Begin <laptop>();
                foreach (var item in _searchParameters.OS)
                {
                    temppredicate = temppredicate.Or(e => e.OpSys == item);
                }
                predicate = predicate.And(temppredicate);
            }
            //Weight
            if (_searchParameters.Weight.Count > 0)
            {
                var temppredicate = PredicateExtensions.PredicateExtensions.Begin <laptop>();
                foreach (var item in _searchParameters.Weight)
                {
                    temppredicate = temppredicate.Or(e => e.Weight == item);
                }
                predicate = predicate.And(temppredicate);
            }
            var records = Entities.laptops.Where(predicate);

            switch (_searchParameters.SortBy)
            {
            case SortCriteria.Relevance:
                break;

            case SortCriteria.PriceLowToHigh:
                records = records.OrderBy(e => e.Price_euros);
                break;

            case SortCriteria.PriceHighToLow:
                records = records.OrderByDescending(e => e.Price_euros);
                break;

            default:
                break;
            }
            return(records);
        }