Beispiel #1
0
        public ProductCategory GetProductCategory(string code)
        {
            ProductCategory result = null;
            var             batch  = new BatchSql();

            batch.Append(CategoriesTable.Select().Where(CategoriesTable.ColumnsQualified.Code, code));
            batch.Append(ProductsTable.Select().Add("\r\nWHERE SKU in (SELECT SKU FROM Categories_Products WHERE Categories_Products.CategoryCode=@p0)"));


            var cmd = batch.BuildCommand(connectionStringName);

            using (var rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
            {
                if (rdr.Read())
                {
                    result = LoadProductCategory(rdr);

                    if (result != null)
                    {
                        result.Products = new List <Product>();
                        if (rdr.NextResult())
                        {
                            while (rdr.Read())
                            {
                                result.Products.Add(LoadProduct(rdr));
                            }
                        }
                    }
                }
            }
            return(result);
        }
        public Customer GetCustomer(string userName)
        {
            var      productRepo = new SimpleProductRepository();
            Customer result      = null;
            var      batch       = new BatchSql();


            //see if there's a customer - if not create one
            object user = CustomersTable.Select(CustomersTable.Columns.UserName)
                          .Where(CustomersTable.Columns.UserName, userName)
                          .BuildCommand()
                          .ExecuteScalar();

            if (user == null)
            {
                CustomersTable.Insert(
                    new Dictionary <string, object>()
                {
                    { CustomersTable.Columns.UserName, userName },
                    { CustomersTable.Columns.LanguageCode, System.Globalization.CultureInfo.CurrentCulture.TwoLetterISOLanguageName }
                })
                .BuildCommand()
                .ExecuteNonQuery();
            }


            //customer
            batch.Append(CustomersTable.Select()
                         .Where(CustomersTable.Columns.UserName, userName));


            //addresses
            batch.Append(AddressesTable.Select()
                         .Where(AddressesTable.Columns.UserName, userName));

            //shopping cart
            Guid orderID = GetCartID(userName);

            batch.Append(OrdersTable.Select()
                         .Where(OrdersTable.Columns.OrderID, orderID));

            //items
            //avert your eyes if this bothers you
            var itemSql = new SqlStatement(connectionStringName);

            itemSql.Add(string.Format("SELECT {0}, {1} ", OrderItemsTable.COLUMN_LIST, ProductsTable.COLUMN_LIST));
            itemSql.InnerJoin(
                OrderItemsTable.TABLE_NAME,
                OrderItemsTable.ColumnsQualified.SKU,
                ProductsTable.TABLE_NAME,
                ProductsTable.ColumnsQualified.SKU)
            .Where(OrderItemsTable.Columns.OrderID, orderID);


            batch.Append(itemSql);

            var cmd = batch.BuildCommand(connectionStringName);

            using (var rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection)) {
                //customer
                if (rdr.Read())
                {
                    result = new Customer(userName,
                                          CustomersTable.ReadEmail(rdr),
                                          CustomersTable.ReadFirst(rdr),
                                          CustomersTable.ReadLast(rdr));
                }
                else
                {
                    result = new Customer();
                }
                //address
                result.AddressBook = new List <Address>();
                if (rdr.NextResult())
                {
                    while (rdr.Read())
                    {
                        result.AddressBook.Add(LoadAddress(rdr));
                    }
                }
                //cart
                result.Cart = new ShoppingCart(userName);
                if (rdr.NextResult())
                {
                    if (rdr.Read())
                    {
                        result.Cart.ShippingAddress = result.AddressBook.SingleOrDefault(x => x.AddressID == OrdersTable.ReadShippingAddressID(rdr));
                        result.Cart.BillingAddress  = result.AddressBook.SingleOrDefault(x => x.AddressID == OrdersTable.ReadBillingAddressID(rdr));
                        result.Cart.ShippingService = OrdersTable.ReadShippingService(rdr) ?? "";
                        result.Cart.ShippingAmount  = OrdersTable.ReadShippingAmount(rdr);
                        result.Cart.TaxAmount       = OrdersTable.ReadTaxAmount(rdr);
                    }
                }

                //items
                if (rdr.NextResult())
                {
                    while (rdr.Read())
                    {
                        var product = productRepo.LoadProduct(rdr);
                        result.Cart.Items.Add(new ShoppingCartItem(product, CartItemsTable.ReadQuantity(rdr), CartItemsTable.ReadDateAdded(rdr)));
                    }
                }
            }

            return(result);
        }
Beispiel #3
0
        public Product GetProduct(string sku)
        {
            Product result = null;
            var     batch  = new BatchSql();

            batch.Append(ProductsTable.Select().Where(ProductsTable.ColumnsQualified.SKU, sku));

            //desciptors
            batch.Append(ProductDescriptorsTable.Select()
                         .Where(ProductDescriptorsTable.ColumnsQualified.SKU, sku)
                         .And(ProductDescriptorsTable.ColumnsQualified.LanguageCode, System.Globalization.CultureInfo.CurrentCulture.TwoLetterISOLanguageName));

            //images
            batch.Append(ProductImagesTable.Select().Where(ProductImagesTable.ColumnsQualified.SKU, sku));

            //cross sells
            batch.Append(ProductsTable.Select().Add("\r\nWHERE sku in (SELECT CrossSku FROM Products_CrossSell WHERE Products_CrossSell.SKU=@p0)"));
            //TODO: Fix this


            //related
            batch.Append(ProductsTable.Select().Add("\r\nWHERE sku in (SELECT RelatedSKu FROM Products_Related WHERE Products_Related.SKU=@p0)"));

            var cmd = batch.BuildCommand(connectionStringName);

            using (var rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection)){
                if (rdr.Read())
                {
                    result = LoadProduct(rdr);
                }
                if (result != null)
                {
                    //desciptors
                    result.Descriptors = new List <ProductDescriptor>();
                    if (rdr.NextResult())
                    {
                        while (rdr.Read())
                        {
                            result.Descriptors.Add(new ProductDescriptor(
                                                       ProductDescriptorsTable.ReadTitle(rdr),
                                                       ProductDescriptorsTable.ReadBody(rdr)
                                                       ));
                        }
                    }

                    //images
                    result.Images = new List <Image>();
                    if (rdr.NextResult())
                    {
                        while (rdr.Read())
                        {
                            result.Images.Add(new Image(
                                                  ProductImagesTable.ReadThumbUrl(rdr),
                                                  ProductImagesTable.ReadFullImageUrl(rdr)));
                        }
                    }

                    //cross sells
                    result.CrossSells = new List <Product>();
                    if (rdr.NextResult())
                    {
                        while (rdr.Read())
                        {
                            result.CrossSells.Add(LoadProduct(rdr));
                        }
                    }
                    //related
                    result.RelatedProducts = new List <Product>();
                    if (rdr.NextResult())
                    {
                        while (rdr.Read())
                        {
                            result.RelatedProducts.Add(LoadProduct(rdr));
                        }
                    }
                }
            }

            return(result);
        }
        public Customer GetCustomer(string userName)
        {
            var productRepo = new SimpleProductRepository();
            Customer result = null;
            var batch = new BatchSql();

            //see if there's a customer - if not create one
            object user = CustomersTable.Select(CustomersTable.Columns.UserName)
                .Where(CustomersTable.Columns.UserName, userName)
                .BuildCommand()
                .ExecuteScalar();

            if (user == null) {
                CustomersTable.Insert(
                    new Dictionary<string, object>() {
                    {CustomersTable.Columns.UserName,userName},
                    {CustomersTable.Columns.LanguageCode,System.Globalization.CultureInfo.CurrentCulture.TwoLetterISOLanguageName}
                })
                .BuildCommand()
                .ExecuteNonQuery();
            }

            //customer
            batch.Append(CustomersTable.Select()
                .Where(CustomersTable.Columns.UserName, userName));

            //addresses
            batch.Append(AddressesTable.Select()
                .Where(AddressesTable.Columns.UserName, userName));

            //shopping cart
            Guid orderID = GetCartID(userName);
            batch.Append(OrdersTable.Select()
                .Where(OrdersTable.Columns.OrderID, orderID));

            //items
            //avert your eyes if this bothers you
            var itemSql = new SqlStatement(connectionStringName);
            itemSql.Add(string.Format("SELECT {0}, {1} ", OrderItemsTable.COLUMN_LIST, ProductsTable.COLUMN_LIST));
            itemSql.InnerJoin(
                OrderItemsTable.TABLE_NAME,
                OrderItemsTable.ColumnsQualified.SKU,
                ProductsTable.TABLE_NAME,
                ProductsTable.ColumnsQualified.SKU)
                .Where(OrderItemsTable.Columns.OrderID, orderID);

            batch.Append(itemSql);

            var cmd = batch.BuildCommand(connectionStringName);

            using (var rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection)) {
                //customer
                if (rdr.Read()) {
                    result = new Customer(userName,
                        CustomersTable.ReadEmail(rdr),
                        CustomersTable.ReadFirst(rdr),
                        CustomersTable.ReadLast(rdr));
                } else {
                    result = new Customer();
                }
                //address
                result.AddressBook = new List<Address>();
                if (rdr.NextResult()) {
                    while (rdr.Read()) {
                        result.AddressBook.Add(LoadAddress(rdr));

                    }
                }
                //cart
                result.Cart = new ShoppingCart(userName);
                if (rdr.NextResult()) {
                    if (rdr.Read()) {
                        result.Cart.ShippingAddress = result.AddressBook.SingleOrDefault(x => x.AddressID == OrdersTable.ReadShippingAddressID(rdr));
                        result.Cart.BillingAddress = result.AddressBook.SingleOrDefault(x => x.AddressID == OrdersTable.ReadBillingAddressID(rdr));
                        result.Cart.ShippingService = OrdersTable.ReadShippingService(rdr) ?? "";
                        result.Cart.ShippingAmount = OrdersTable.ReadShippingAmount(rdr);
                        result.Cart.TaxAmount = OrdersTable.ReadTaxAmount(rdr);
                    }
                }

                //items
                if (rdr.NextResult()) {
                    while (rdr.Read()) {
                        var product = productRepo.LoadProduct(rdr);
                        result.Cart.Items.Add(new ShoppingCartItem(product, CartItemsTable.ReadQuantity(rdr), CartItemsTable.ReadDateAdded(rdr)));
                    }
                }
            }

            return result;
        }
        public Order GetOrder(Guid orderID)
        {
            SimpleProductRepository  productRepository  = new SimpleProductRepository();
            SimpleCustomerRepository customerRepository = new SimpleCustomerRepository();
            Order result = null;

            var batch = new BatchSql();

            batch.Append(OrdersTable.Select().Where("OrderID", orderID));

            //items

            //products for the items
            var sql = new SqlStatement(connectionStringName);

            sql.Add("SELECT ");
            sql.Add(ProductsTable.COLUMN_LIST);
            sql.Add(OrderItemsTable.COLUMN_LIST);
            sql.Add("FROM Products INNER JOIN OrderItems ON Products.SKU = OrderItems.SKU");
            sql.Add("WHERE SKU IN (SELECT SKU FROM OrderItems WHERE OrderID=@OrderID)");

            //transactions
            batch.Append(TransactionsTable.Select().Where("orderid", orderID));

            int shippingAddressID = 0;
            int billingAddressID  = 0;
            int shippingMethodID  = 0;

            //pull it
            var cmd = sql.BuildCommand();

            using (var rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection)) {
                if (rdr.Read())
                {
                    result = new Order(OrdersTable.ReadOrderStatusID(rdr))
                    {
                        DateCreated       = OrdersTable.ReadCreatedOn(rdr),
                        DateShipped       = OrdersTable.ReadDateShipped(rdr),
                        UserName          = OrdersTable.ReadUserName(rdr),
                        DiscountAmount    = OrdersTable.ReadDiscountAmount(rdr),
                        DiscountReason    = OrdersTable.ReadDiscountReason(rdr),
                        EstimatedDelivery = OrdersTable.ReadEstimatedDelivery(rdr),
                        ID              = orderID,
                        OrderNumber     = OrdersTable.ReadOrderNumber(rdr),
                        ShippingAmount  = OrdersTable.ReadShippingAmount(rdr),
                        ShippingService = OrdersTable.ReadShippingService(rdr),
                        TaxAmount       = OrdersTable.ReadTaxAmount(rdr),
                    };

                    billingAddressID  = OrdersTable.ReadBillingAddressID(rdr);
                    shippingAddressID = OrdersTable.ReadShippingAddressID(rdr);
                }

                //load the items
                result.Items = new List <OrderLine>();
                if (rdr.NextResult())
                {
                    while (rdr.Read())
                    {
                        var product = productRepository.LoadProduct(rdr);
                        var item    = new OrderLine(OrderItemsTable.ReadDateAdded(rdr), OrderItemsTable.ReadQuantity(rdr), product);
                        result.Items.Add(item);
                    }
                }

                //transactions
                result.Transactions = new List <Transaction>();
                if (rdr.NextResult())
                {
                    while (rdr.Read())
                    {
                        Transaction t = new Transaction(
                            TransactionsTable.ReadTransactionID(rdr),
                            orderID,
                            TransactionsTable.ReadAmount(rdr),
                            TransactionsTable.ReadTransactionDate(rdr),
                            TransactionsTable.ReadAuthorizationCode(rdr),
                            TransactionsTable.ReadNotes(rdr),
                            TransactionsTable.ReadProcessor(rdr));

                        result.Transactions.Add(t);
                    }
                }
            }
            sql = new SqlStatement(connectionStringName);

            //addresses
            batch.Append(AddressesTable.Select().Where("addressid", shippingAddressID));

            batch.Append(AddressesTable.Select().Where("addressid", billingAddressID));

            //shipping method
            batch.Append(ShippingMethodsTable.Select().Where("shippingmethodid", shippingMethodID));

            cmd = batch.BuildCommand(connectionStringName);


            using (var rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection)) {
                //shipping address
                if (rdr.Read())
                {
                    //shipping
                    result.ShippingAddress = customerRepository.LoadAddress(rdr);
                }
                //billing address
                if (rdr.NextResult())
                {
                    if (rdr.Read())
                    {
                        result.BillingAddress = customerRepository.LoadAddress(rdr);
                    }
                }
                //shipping method
                if (rdr.NextResult())
                {
                    if (rdr.Read())
                    {
                        LoadShipping(rdr);
                    }
                }
            }

            return(result);
        }
        public Product GetProduct(string sku)
        {
            Product result=null;
            var batch = new BatchSql();
            batch.Append(ProductsTable.Select().Where(ProductsTable.ColumnsQualified.SKU, sku));

            //desciptors
            batch.Append(ProductDescriptorsTable.Select()
                .Where(ProductDescriptorsTable.ColumnsQualified.SKU, sku)
                .And(ProductDescriptorsTable.ColumnsQualified.LanguageCode,System.Globalization.CultureInfo.CurrentCulture.TwoLetterISOLanguageName));

            //images
            batch.Append(ProductImagesTable.Select().Where(ProductImagesTable.ColumnsQualified.SKU, sku));

            //cross sells
            batch.Append(ProductsTable.Select().Add("\r\nWHERE sku in (SELECT CrossSku FROM Products_CrossSell WHERE Products_CrossSell.SKU=@p0)"));
            //TODO: Fix this

            //related
            batch.Append(ProductsTable.Select().Add("\r\nWHERE sku in (SELECT RelatedSKu FROM Products_Related WHERE Products_Related.SKU=@p0)"));

            var cmd = batch.BuildCommand(connectionStringName);

            using(var rdr=cmd.ExecuteReader(CommandBehavior.CloseConnection)){

                if(rdr.Read()){
                    result = LoadProduct(rdr);
                }
                if (result != null) {
                    //desciptors
                    result.Descriptors = new List<ProductDescriptor>();
                    if (rdr.NextResult()) {
                        while (rdr.Read()) {
                            result.Descriptors.Add(new ProductDescriptor(
                                ProductDescriptorsTable.ReadTitle(rdr),
                                ProductDescriptorsTable.ReadBody(rdr)
                                ));
                        }
                    }

                    //images
                    result.Images = new List<Image>();
                    if (rdr.NextResult()) {
                        while (rdr.Read()) {
                            result.Images.Add(new Image(
                                ProductImagesTable.ReadThumbUrl(rdr),
                                ProductImagesTable.ReadFullImageUrl(rdr)));
                        }
                    }

                    //cross sells
                    result.CrossSells = new List<Product>();
                    if (rdr.NextResult()) {
                        while (rdr.Read()) {
                            result.CrossSells.Add(LoadProduct(rdr));
                        }
                    }
                    //related
                    result.RelatedProducts = new List<Product>();
                    if (rdr.NextResult()) {
                        while (rdr.Read()) {
                            result.RelatedProducts.Add(LoadProduct(rdr));
                        }
                    }
                }
            }

            return result;
        }
        public ProductCategory GetProductCategory(string code)
        {
            ProductCategory result = null;
            var batch = new BatchSql();
            batch.Append(CategoriesTable.Select().Where(CategoriesTable.ColumnsQualified.Code, code));
            batch.Append(ProductsTable.Select().Add("\r\nWHERE SKU in (SELECT SKU FROM Categories_Products WHERE Categories_Products.CategoryCode=@p0)"));

            var cmd = batch.BuildCommand(connectionStringName);

            using (var rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
            {
                if (rdr.Read())
                {
                    result = LoadProductCategory(rdr);

                    if (result != null)
                    {
                        result.Products = new List<Product>();
                        if (rdr.NextResult())
                        {
                            while (rdr.Read())
                            {
                                result.Products.Add(LoadProduct(rdr));
                            }
                        }
                    }
                }
            }
            return result;
        }
        public Order GetOrder(Guid orderID)
        {
            SimpleProductRepository productRepository = new SimpleProductRepository();
            SimpleCustomerRepository customerRepository = new SimpleCustomerRepository();
            Order result = null;

            var batch = new BatchSql();

            batch.Append(OrdersTable.Select().Where("OrderID", orderID));

            //items

            //products for the items
            var sql = new SqlStatement(connectionStringName);
            sql.Add("SELECT ");
            sql.Add(ProductsTable.COLUMN_LIST);
            sql.Add(OrderItemsTable.COLUMN_LIST);
            sql.Add("FROM Products INNER JOIN OrderItems ON Products.SKU = OrderItems.SKU");
            sql.Add("WHERE SKU IN (SELECT SKU FROM OrderItems WHERE OrderID=@OrderID)");

            //transactions
            batch.Append(TransactionsTable.Select().Where("orderid", orderID));

            int shippingAddressID = 0;
            int billingAddressID = 0;
            int shippingMethodID=0;

            //pull it
            var cmd = sql.BuildCommand();

            using (var rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection)) {
                if (rdr.Read()) {
                    result = new Order(OrdersTable.ReadOrderStatusID(rdr))
                    {
                        DateCreated =OrdersTable.ReadCreatedOn(rdr),
                        DateShipped = OrdersTable.ReadDateShipped(rdr),
                        UserName = OrdersTable.ReadUserName(rdr),
                        DiscountAmount = OrdersTable.ReadDiscountAmount(rdr),
                        DiscountReason =OrdersTable.ReadDiscountReason(rdr),
                        EstimatedDelivery = OrdersTable.ReadEstimatedDelivery(rdr),
                        ID = orderID,
                        OrderNumber = OrdersTable.ReadOrderNumber(rdr),
                        ShippingAmount = OrdersTable.ReadShippingAmount(rdr),
                        ShippingService=OrdersTable.ReadShippingService(rdr),
                        TaxAmount = OrdersTable.ReadTaxAmount(rdr),

                    };

                    billingAddressID = OrdersTable.ReadBillingAddressID(rdr);
                    shippingAddressID = OrdersTable.ReadShippingAddressID(rdr);

                }

                //load the items
                result.Items = new List<OrderLine>();
                if (rdr.NextResult()) {
                    while (rdr.Read()) {
                        var product = productRepository.LoadProduct(rdr);
                        var item = new OrderLine(OrderItemsTable.ReadDateAdded(rdr),OrderItemsTable.ReadQuantity(rdr),product);
                        result.Items.Add(item);
                    }
                }

                //transactions
                result.Transactions = new List<Transaction>();
                if (rdr.NextResult()) {
                    while (rdr.Read()) {
                        Transaction t = new Transaction(
                            TransactionsTable.ReadTransactionID(rdr),
                            orderID,
                            TransactionsTable.ReadAmount(rdr),
                            TransactionsTable.ReadTransactionDate(rdr),
                            TransactionsTable.ReadAuthorizationCode(rdr),
                            TransactionsTable.ReadNotes(rdr),
                            TransactionsTable.ReadProcessor(rdr));

                        result.Transactions.Add(t);

                    }
                }

            }
            sql = new SqlStatement(connectionStringName);

            //addresses
            batch.Append(AddressesTable.Select().Where("addressid", shippingAddressID));

            batch.Append(AddressesTable.Select().Where("addressid", billingAddressID));

            //shipping method
            batch.Append(ShippingMethodsTable.Select().Where("shippingmethodid", shippingMethodID));

            cmd = batch.BuildCommand(connectionStringName);

            using (var rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection)) {

                //shipping address
                if (rdr.Read()) {
                    //shipping
                    result.ShippingAddress = customerRepository.LoadAddress(rdr);
                }
                //billing address
                if (rdr.NextResult()) {
                    if (rdr.Read()) {
                        result.BillingAddress = customerRepository.LoadAddress(rdr);
                    }
                }
                //shipping method
                if (rdr.NextResult()) {
                    if (rdr.Read()) {
                        LoadShipping(rdr);
                    }
                }

            }

            return result;
        }