示例#1
0
        public ContentResult FilteredGet([FromBody] MultyFilter filter)
        {
            var pizzas = _context.Pizzas.ToList();
            List <PizzaModel> filteredList = new List <PizzaModel>();
            var firstFilter = filter.Filters.First();

            foreach (var item in pizzas)
            {
                var properties = item.GetType().GetProperties();
                foreach (var prop in properties)
                {
                    if (prop.Name == firstFilter.PropertyName)
                    {
                        if (firstFilter.Conditions[0] == ">" || firstFilter.Conditions[0] == ">=" || firstFilter.Conditions[0] == "<" || firstFilter.Conditions[0] == "<=")
                        {
                            if (firstFilter.Conditions.Count > 1)
                            {
                                switch (firstFilter.Conditions[0])
                                {
                                case ">":
                                    switch (firstFilter.Conditions[1])
                                    {
                                    case "<":
                                        if ((decimal)prop.GetValue(item) > decimal.Parse(firstFilter.Values[0]) && (decimal)prop.GetValue(item) < decimal.Parse(firstFilter.Values[1]))
                                        {
                                            filteredList.Add(new PizzaModel()
                                            {
                                                Name        = item.Name,
                                                Description = item.Description,
                                                Image       = item.Image,
                                                Price       = item.Price
                                            });
                                        }
                                        break;

                                    case "<=":
                                        if ((decimal)prop.GetValue(item) > decimal.Parse(firstFilter.Values[0]) && (decimal)prop.GetValue(item) <= decimal.Parse(firstFilter.Values[1]))
                                        {
                                            filteredList.Add(new PizzaModel()
                                            {
                                                Name        = item.Name,
                                                Description = item.Description,
                                                Image       = item.Image,
                                                Price       = item.Price
                                            });
                                        }
                                        break;
                                    }

                                    break;

                                case ">=":
                                    switch (firstFilter.Conditions[1])
                                    {
                                    case "<":
                                        if ((decimal)prop.GetValue(item) >= decimal.Parse(firstFilter.Values[0]) && (decimal)prop.GetValue(item) < decimal.Parse(firstFilter.Values[1]))
                                        {
                                            filteredList.Add(new PizzaModel()
                                            {
                                                Name        = item.Name,
                                                Description = item.Description,
                                                Image       = item.Image,
                                                Price       = item.Price
                                            });
                                        }
                                        break;

                                    case "<=":
                                        if ((decimal)prop.GetValue(item) >= decimal.Parse(firstFilter.Values[0]) && (decimal)prop.GetValue(item) <= decimal.Parse(firstFilter.Values[1]))
                                        {
                                            filteredList.Add(new PizzaModel()
                                            {
                                                Name        = item.Name,
                                                Description = item.Description,
                                                Image       = item.Image,
                                                Price       = item.Price
                                            });
                                        }
                                        break;
                                    }
                                    break;

                                case "<":
                                    switch (firstFilter.Conditions[1])
                                    {
                                    case ">":
                                        if ((decimal)prop.GetValue(item) < decimal.Parse(firstFilter.Values[0]) && (decimal)prop.GetValue(item) > decimal.Parse(firstFilter.Values[1]))
                                        {
                                            filteredList.Add(new PizzaModel()
                                            {
                                                Name        = item.Name,
                                                Description = item.Description,
                                                Image       = item.Image,
                                                Price       = item.Price
                                            });
                                        }
                                        break;

                                    case ">=":
                                        if ((decimal)prop.GetValue(item) < decimal.Parse(firstFilter.Values[0]) && (decimal)prop.GetValue(item) >= decimal.Parse(firstFilter.Values[1]))
                                        {
                                            filteredList.Add(new PizzaModel()
                                            {
                                                Name        = item.Name,
                                                Description = item.Description,
                                                Image       = item.Image,
                                                Price       = item.Price
                                            });
                                        }
                                        break;
                                    }
                                    break;

                                case "<=":
                                    switch (firstFilter.Conditions[1])
                                    {
                                    case ">":
                                        if ((decimal)prop.GetValue(item) <= decimal.Parse(firstFilter.Values[0]) && (decimal)prop.GetValue(item) > decimal.Parse(firstFilter.Values[1]))
                                        {
                                            filteredList.Add(new PizzaModel()
                                            {
                                                Name        = item.Name,
                                                Description = item.Description,
                                                Image       = item.Image,
                                                Price       = item.Price
                                            });
                                        }
                                        break;

                                    case ">=":
                                        if ((decimal)prop.GetValue(item) <= decimal.Parse(firstFilter.Values[0]) && (decimal)prop.GetValue(item) >= decimal.Parse(firstFilter.Values[1]))
                                        {
                                            filteredList.Add(new PizzaModel()
                                            {
                                                Name        = item.Name,
                                                Description = item.Description,
                                                Image       = item.Image,
                                                Price       = item.Price
                                            });
                                        }
                                        break;
                                    }
                                    break;
                                }
                            }
                            else
                            {
                                switch (firstFilter.Conditions[0])
                                {
                                case ">":
                                    if ((decimal)prop.GetValue(item) > decimal.Parse(firstFilter.Values[0]))
                                    {
                                        filteredList.Add(new PizzaModel()
                                        {
                                            Name        = item.Name,
                                            Description = item.Description,
                                            Image       = item.Image,
                                            Price       = item.Price
                                        });
                                    }
                                    break;

                                case ">=":
                                    if ((decimal)prop.GetValue(item) >= decimal.Parse(firstFilter.Values[0]))
                                    {
                                        filteredList.Add(new PizzaModel()
                                        {
                                            Name        = item.Name,
                                            Description = item.Description,
                                            Image       = item.Image,
                                            Price       = item.Price
                                        });
                                    }
                                    break;

                                case "<":
                                    if ((decimal)prop.GetValue(item) < decimal.Parse(firstFilter.Values[0]))
                                    {
                                        filteredList.Add(new PizzaModel()
                                        {
                                            Name        = item.Name,
                                            Description = item.Description,
                                            Image       = item.Image,
                                            Price       = item.Price
                                        });
                                    }
                                    break;

                                case "<=":
                                    if ((decimal)prop.GetValue(item) <= decimal.Parse(firstFilter.Values[0]))
                                    {
                                        filteredList.Add(new PizzaModel()
                                        {
                                            Name        = item.Name,
                                            Description = item.Description,
                                            Image       = item.Image,
                                            Price       = item.Price
                                        });
                                    }
                                    break;
                                }
                            }
                        }
                        else
                        {
                            if (firstFilter.Conditions[0] == "==")
                            {
                                if (prop.GetValue(item) is string)
                                {
                                    if (((string)prop.GetValue(item)).Contains(firstFilter.Values[0]))
                                    {
                                        filteredList.Add(new PizzaModel()
                                        {
                                            Name        = item.Name,
                                            Description = item.Description,
                                            Image       = item.Image,
                                            Price       = item.Price
                                        });
                                    }
                                }
                                else
                                {
                                    if (decimal.Parse(firstFilter.Values[0]) == (decimal)prop.GetValue(item))
                                    {
                                        filteredList.Add(new PizzaModel()
                                        {
                                            Name        = item.Name,
                                            Description = item.Description,
                                            Image       = item.Image,
                                            Price       = item.Price
                                        });
                                    }
                                }
                            }
                        }
                        break;
                    }
                }
            }
            if (filter.Filters.Count > 1)
            {
                for (int i = 1; i < filter.Filters.Count; i++)
                {
                    filteredList = FinishFilter(filter.Filters[i], filteredList);
                }
            }
            return(Content(JsonConvert.SerializeObject(filteredList)));
        }
示例#2
0
        private void SearchBtn_Click(object sender, RoutedEventArgs e)
        {
            MultyFilter filter = new MultyFilter();

            if (NameTextBox.Text != "")
            {
                List <string> conditions = new List <string>()
                {
                    "=="
                };
                List <string> values = new List <string>()
                {
                    NameTextBox.Text
                };
                filter.Filters.Add(new Filter()
                {
                    PropertyName = "Name", Conditions = conditions, Values = values
                });
            }
            if (FromTextBox.Text != "" || ToTextBox.Text != "")
            {
                List <string> conditions = new List <string>();
                List <string> values     = new List <string>();
                if (FromTextBox.Text != "")
                {
                    conditions.Add(">=");
                    values.Add(FromTextBox.Text);
                }
                if (ToTextBox.Text != "")
                {
                    conditions.Add("<=");
                    values.Add(ToTextBox.Text);
                }


                filter.Filters.Add(new Filter()
                {
                    PropertyName = "Price", Conditions = conditions, Values = values
                });
            }



            HttpWebRequest webRequest = WebRequest.CreateHttp($"https://localhost:44361/api/pizzas/filtered/get");

            webRequest.Method      = "PUT";
            webRequest.ContentType = "application/json";

            using (Stream stream = webRequest.GetRequestStream())
            {
                using (StreamWriter writer = new StreamWriter(stream))
                {
                    writer.Write(JsonConvert.SerializeObject(filter));
                }
            }
            var    webResponse = webRequest.GetResponse();
            string response    = "";

            using (Stream stream = webResponse.GetResponseStream())
            {
                using (StreamReader reader = new StreamReader(stream))
                {
                    response = reader.ReadToEnd();
                }
            }

            pizzas = (JsonConvert.DeserializeObject <List <PizzaModel> >(response));
            listViewPizza.ItemsSource = pizzas;
        }