コード例 #1
0
        private List <IProduct> GetCatalog(string search, bool getAll = false)
        {
            string select = "SELECT id,name,description,unitPrice,stockCount";
            string from   = "FROM Products";

            string where = "WHERE";
            string and     = "AND ";
            string or      = "OR ";
            string orderby = "ORDER BY";

            if (titleCheckBox.IsChecked == true && getAll == false)
            {
                if (descriptionCheckBox.IsChecked == false)
                {
                    where = $"{where} name LIKE @title";
                }
                if (descriptionCheckBox.IsChecked == true)
                {
                    where = $"{where} ( name LIKE @title";
                }
            }

            if (descriptionCheckBox.IsChecked == true && getAll == false)
            {
                if (where != "WHERE")
                {
                    where = $"{where} {or}";
                }

                if (where != "WHERE")
                {
                    where = $"{where} description LIKE @description)";
                }
                else
                {
                    where = $"{where} description LIKE @description";
                }
            }

            if (priceRangeCheckBox.IsChecked == true)
            {
                if (minimumPriceDUD.Value != null && minimumPriceDUD.Value > 0)
                {
                    if (where != "WHERE")
                    {
                        where = $"{where} {and}";
                    }

                    where = $"{where} unitPrice >= @minPrice";
                }

                if (maximumPriceDUD.Value != null)
                {
                    if (where != "WHERE")
                    {
                        where = $"{where} {and}";
                    }
                    where = $"{where} unitPrice <= @maxPrice";
                }
            }

            var    selectedItem = (ComboBoxItem)sortByComboBox.SelectedItem;
            string tag          = selectedItem.Tag.ToString();

            if (tag == "noSort")
            {
                orderby = string.Empty;
            }
            else
            {
                orderby = $"{orderby} {tag}";
            }

            if (where == "WHERE")
            {
                where = string.Empty;
            }

            string query = $"{select} {from} {where} {orderby}";

            var parameterKVPs = new Dictionary <string, object>();

            if (titleCheckBox.IsChecked == true)
            {
                parameterKVPs.Add("@title", $"%{search}%");
            }

            if (descriptionCheckBox.IsChecked == true)
            {
                parameterKVPs.Add("@description", $"%{search}%");
            }

            if (priceRangeCheckBox.IsChecked == true)
            {
                if (minimumPriceDUD.Value != null && minimumPriceDUD.Value > 0)
                {
                    parameterKVPs.Add("@minPrice", (decimal)minimumPriceDUD.Value);
                }

                if (maximumPriceDUD.Value != null)
                {
                    parameterKVPs.Add("@maxPrice", (decimal)maximumPriceDUD.Value);
                }
            }

            List <IProduct> products = DBAccessHelper.GetProducts(query, parameterKVPs);

            return(products);
        }