public void Save(Customer c)
        {
            //save the customer
            List <DbCommand> commands = new List <DbCommand>();

            SqlStatement sql      = null;
            var          settings = new Dictionary <string, object>()
            {
                { CustomersTable.Columns.Email, c.Email },
                { CustomersTable.Columns.First, c.Email },
                { CustomersTable.Columns.Last, c.Email },
                { CustomersTable.Columns.LanguageCode, c.Email }
            };

            if (SqlHelper.RecordExists(connectionStringName, CustomersTable.TABLE_NAME, CustomersTable.Columns.UserName, c.UserName))
            {
                //update
                sql = CustomersTable.Update(settings)
                      .Where(CustomersTable.Columns.UserName, c.UserName);
            }
            else
            {
                //insert
                settings.Add(CustomersTable.Columns.UserName, c.UserName);
                sql = CustomersTable.Insert(settings);
            }

            sql.BuildCommand().ExecuteNonQuery();
        }
        public static bool RecordExists(string connStringName, string tableName, Dictionary <string, object> values)
        {
            var          sb  = new StringBuilder();
            SqlStatement sql = new SqlStatement(connStringName);

            sql.Add("SELECT " + values.First().Key);
            sql.Add(" FROM " + tableName);

            object result  = null;
            int    indexer = 1;

            foreach (string s in values.Keys)
            {
                if (indexer == 1)
                {
                    sql.Where(s, values[s]);
                }
                else
                {
                    sql.And(s, values[s]);
                }
                indexer++;
            }
            using (var cmd = sql.BuildCommand()) {
                result = cmd.ExecuteScalar();
            }
            return(result != null);
        }
        DbCommand GetAddressCommand(Address address)
        {
            var sql = new SqlStatement(connectionStringName);

            var settings = new Dictionary <string, object>()
            {
                { AddressesTable.Columns.UserName, address.UserName },
                { AddressesTable.Columns.FirstName, address.FirstName },
                { AddressesTable.Columns.LastName, address.LastName },
                { AddressesTable.Columns.Email, address.Email },
                { AddressesTable.Columns.Street1, address.Street1 },
                { AddressesTable.Columns.Street2, address.Street2 != null ? address.Street2 : "" },
                { AddressesTable.Columns.City, address.City },
                { AddressesTable.Columns.StateOrProvince, address.StateOrProvince },
                { AddressesTable.Columns.Zip, address.Zip },
                { AddressesTable.Columns.Country, address.Country },
                { AddressesTable.Columns.IsDefault, address.IsDefault }
            };

            if (SqlHelper.RecordExists(connectionStringName, AddressesTable.TABLE_NAME, new Dictionary <string, object>()
            {
                { AddressesTable.Columns.UserName, address.UserName },
                { AddressesTable.Columns.Street1, address.Street1 },
                { AddressesTable.Columns.City, address.City },
                { AddressesTable.Columns.StateOrProvince, address.StateOrProvince },
            }))
            {
                //update
                sql = AddressesTable.Update(settings)
                      .Where(AddressesTable.Columns.UserName, address.UserName)
                      .And(AddressesTable.Columns.Street1, address.Street1)
                      .And(AddressesTable.Columns.City, address.City)
                      .And(AddressesTable.Columns.StateOrProvince, address.StateOrProvince);
            }
            else
            {
                //insert
                sql = AddressesTable.Insert(settings);
            }
            var cmd = sql.BuildCommand();

            //add the params
            return(cmd);
        }
示例#4
0
        public static bool RecordExists( string connStringName,string tableName, Dictionary<string, object> values)
        {
            var sb = new StringBuilder();
            SqlStatement sql = new SqlStatement(connStringName);
            sql.Add("SELECT " + values.First().Key);
            sql.Add(" FROM " + tableName);

            object result = null;
            int indexer = 1;
            foreach (string s in values.Keys) {
                if (indexer == 1) {
                    sql.Where(s, values[s]);
                } else {
                    sql.And(s, values[s]);
                }
                indexer++;
            }
            using (var cmd = sql.BuildCommand()) {
                result = cmd.ExecuteScalar();
            }
            return result != null;
        }
        DbCommand GetAddressCommand(Address address)
        {
            var sql = new SqlStatement(connectionStringName);

            var settings = new Dictionary<string, object>()
                {
                    {AddressesTable.Columns.UserName, address.UserName},
                    {AddressesTable.Columns.FirstName, address.FirstName},
                    {AddressesTable.Columns.LastName, address.LastName},
                    {AddressesTable.Columns.Email, address.Email},
                    {AddressesTable.Columns.Street1, address.Street1},
                    {AddressesTable.Columns.Street2, address.Street2 != null ? address.Street2 : ""},
                    {AddressesTable.Columns.City, address.City},
                    {AddressesTable.Columns.StateOrProvince, address.StateOrProvince},
                    {AddressesTable.Columns.Zip, address.Zip},
                    {AddressesTable.Columns.Country, address.Country},
                    {AddressesTable.Columns.IsDefault, address.IsDefault}
               };

            if (SqlHelper.RecordExists(connectionStringName,AddressesTable.TABLE_NAME, new Dictionary<string, object>()
                {
                    {AddressesTable.Columns.UserName,address.UserName},
                    {AddressesTable.Columns.Street1,address.Street1},
                    {AddressesTable.Columns.City,address.City},
                    {AddressesTable.Columns.StateOrProvince,address.StateOrProvince},
                }))
            {
                //update
                sql = AddressesTable.Update(settings)
                    .Where(AddressesTable.Columns.UserName, address.UserName)
                    .And(AddressesTable.Columns.Street1, address.Street1)
                    .And(AddressesTable.Columns.City, address.City)
                    .And(AddressesTable.Columns.StateOrProvince, address.StateOrProvince);

            } else {
                //insert
                sql = AddressesTable.Insert(settings);

            }
            var cmd = sql.BuildCommand();

            //add the params
            return cmd;
        }
        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);
        }
示例#7
0
        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;
        }