private static bool IsLanguageSupported(string languageCode)
 {
     if (supportedLang == null)
     {
         //Load the items. 
         using (DB context = new DB())
         {
             var supportLangQuery = (from lang in context.CategoryCultureDetails
                                     select lang.Culture.LanguageCode).Distinct();
             supportedLang = supportLangQuery.ToArray<String>();
         }
     }
     return supportedLang.Contains(languageCode); 
 }
        public void Increment(int productID, int amount, string notes) {


            using (DB db = new DB()) {
                Commerce.Data.SqlRepository.InventoryRecord record = new Commerce.Data.SqlRepository.InventoryRecord();
                record.ProductID = productID;
                record.Notes = notes;
                record.Increment = amount;
                record.DateEntered = DateTime.Now;
                db.InventoryRecords.InsertOnSubmit(record);

                db.SubmitChanges();
            }

        }
        public void Save(Commerce.Data.UserEvent userEvent)
        {


            using (DB db = new DB())
            {
                //make sure there's a user
                int userCount = (from u in db.Users
                                 where u.UserName == userEvent.UserName
                                 select u).Count();

                //if not, need to add one
                if (userCount == 0)
                {
                    Commerce.Data.SqlRepository.User newUser = new Commerce.Data.SqlRepository.User();
                    newUser.UserName = userEvent.UserName;
                    newUser.CreatedOn = DateTime.Now;
                    newUser.ModifiedOn = DateTime.Now;
                    db.Users.InsertOnSubmit(newUser);
                }

                //there is no updating of user events - it's always an insert
                Commerce.Data.SqlRepository.UserEvent newEvent = new Commerce.Data.SqlRepository.UserEvent();

                //some left/right
                newEvent.IP = userEvent.IP;
                newEvent.UserName = userEvent.UserName;
                newEvent.ProductID = userEvent.ProductID;
                newEvent.CategoryID = userEvent.CategoryID;
                newEvent.EventDate = DateTime.Now;
                if (userEvent.OrderID != Guid.Empty)
                    newEvent.OrderID = userEvent.OrderID;
                else
                    newEvent.OrderID = null;
                newEvent.UserBehaviorID = (int)userEvent.Behavior;

                db.UserEvents.InsertOnSubmit(newEvent);
                db.SubmitChanges();
            }
        }
 public SqlInventoryRepository(DB dataContext) {
     _db = dataContext;
 }
 public SqlPersonalizationRepository(DB db) {
     _db = db;
 }
        public void DeleteTransaction(Guid transactionID)
        {
            using (DB db = new DB())
            {
                db.Transactions.DeleteAllOnSubmit(from t in db.Transactions where t.TransactionID==transactionID select t);
                db.SubmitChanges();
            }

        }
        public void SaveOrder(Order order)
        {

            //save down the addresses
            if(order.ShippingAddress!=null)
                SaveAddress(order.ShippingAddress);
            
            if(order.BillingAddress!=null)
                SaveAddress(order.BillingAddress);


            using (DB db = new DB())
            {
                
                //pull the order
                Commerce.Data.SqlRepository.Order existingOrder = (from o in db.Orders
                                                               where o.OrderID == order.ID
                                                               select o).SingleOrDefault();
                if (existingOrder == null)
                   throw new InvalidOperationException("There is no order with the ID "+order.ID.ToString());

                //marry up the orders
                existingOrder.TaxAmount=order.TaxAmount;

                if (order.ShippingMethod != null) {
                    existingOrder.ShippingAmount = order.ShippingMethod.Cost;
                    existingOrder.ShippingMethodID = order.ShippingMethod.ID;
                    
                    //shipping method bits
                    existingOrder.EstimatedDelivery = DateTime.Now.AddDays(order.ShippingMethod.DaysToDeliver);
                }
                existingOrder.SubTotal=order.SubTotal;
                existingOrder.OrderStatusID=(int)order.Status;

                if(order.ShippingAddress!=null)
                    existingOrder.ShippingAddressID = order.ShippingAddress.ID;
                
                if (order.BillingAddress != null)
                    existingOrder.BillingAddressID = order.BillingAddress.ID;
                
                existingOrder.ExecutedOn = DateTime.Now;
                existingOrder.OrderNumber = order.OrderNumber;
                existingOrder.UserName = order.UserName;
                existingOrder.DiscountAmount = order.DiscountAmount;
                existingOrder.DiscountReason = order.DiscountReason;

                //save this down so we know how to correspond in the future
                existingOrder.UserLanguageCode = System.Globalization.CultureInfo.CurrentUICulture.TwoLetterISOLanguageName;

                foreach (Transaction t in order.Transactions)
                {
                    Commerce.Data.SqlRepository.Transactions newTransaction = new Commerce.Data.SqlRepository.Transactions();

                    //a little left/right action...
                    newTransaction.TransactionID = t.ID;
                    newTransaction.OrderID = t.OrderID;
                    newTransaction.Notes = t.Notes;
                    newTransaction.AuthorizationCode = t.AuthorizationCode;
                    newTransaction.Amount = t.Amount;
                    newTransaction.ProcessorID = (int)t.Processor;
                    newTransaction.TransactionDate = t.DateExecuted;

                    db.Transactions.InsertOnSubmit(newTransaction);


                }

                //cross your fingers!
                db.SubmitChanges();
            }


        }
        public void SaveItems(Order order) {
            //TODO: I know this need to be rewritting
            //the problem I have here is that I need it ALL to be in the scope of a single DB transaction

            //Ayende <3 this method :p

            using (DB db = new DB()) {

                //see if there is an order in the DB already
                Commerce.Data.SqlRepository.Order
                    existingorder = (from o in db.Orders
                                    where o.OrderID==order.ID
                                    select o).SingleOrDefault();

                //if not, create it
                if (existingorder == null) {

                    existingorder = new Commerce.Data.SqlRepository.Order();
                    existingorder.UserName = order.UserName;
                    existingorder.CreatedOn = DateTime.Now;
                    existingorder.ModifiedOn = DateTime.Now;
                    existingorder.OrderID = order.ID;
                    existingorder.OrderStatusID = (int) order.Status;
                    existingorder.UserLanguageCode = System.Globalization.CultureInfo.CurrentUICulture.TwoLetterISOLanguageName;
                    db.Orders.InsertOnSubmit(existingorder);

                } else {

                    //there's a order - pull all the ProductIDs from our Model
                    var productsInBasket = from p in order.Items
                                  
                                           select p.Product.ID;

                    //first, drop the items in the DB that aren't in the order
                    var deletedProducts = from si in db.OrderItems
                               where !productsInBasket.Contains(si.ProductID) 
                               && si.OrderID == order.ID
                               select si;

                    db.OrderItems.DeleteAllOnSubmit(deletedProducts);
                           

                    //update the ones that have changed - this applies to Quantity
                    foreach (Commerce.Data.SqlRepository.OrderItem dbItem in existingorder.OrderItems) {

                        OrderItem orderItem = order.Items.Where(
                                x => x.OrderID == dbItem.OrderID 
                                && x.Product.ID == dbItem.ProductID
                                ).SingleOrDefault();

                        //if the quantity has changed, update it
                        if (orderItem != null && dbItem.Quantity != orderItem.Quantity) {
                            dbItem.Quantity = orderItem.Quantity;
                        }

                    }

                }


                //finally, add the items that are new (ID==0)
                //setup the items to load up
                foreach (OrderItem newItem in order.Items) {

                    //see if the product is in the existing order
                    Commerce.Data.SqlRepository.OrderItem existingItem = (from items in existingorder.OrderItems
                                                                                     where items.ProductID == newItem.Product.ID
                                                                                     select items).SingleOrDefault();

                    if (existingItem == null) {
                        existingItem = new Commerce.Data.SqlRepository.OrderItem();
                        existingItem.DateAdded = DateTime.Now;
                        existingItem.OrderID = existingorder.OrderID;
                        existingItem.ProductID = newItem.Product.ID;
                        existingItem.LineItemPrice = newItem.LineItemPrice; 
                       
                    }

                    existingItem.Quantity = newItem.Quantity;
                    existingorder.OrderItems.Add(existingItem);
                }



                if (order.ShippingAddress != null)
                    existingorder.ShippingAddressID = order.ShippingAddress.ID;


                //save it in a batch - this is a transaction
                db.SubmitChanges();

            }


        }
        public bool DeleteOrder(Guid orderID) {
            bool result = false;
            

           //delete the items first
            using(DB db=new DB())
            {

                //items
                var delItems = from oi in db.OrderItems
                               where oi.OrderID == orderID
                               select oi;
                
                //order
                var delOrder = from o in db.Orders
                               where o.OrderID == orderID
                               select o;


                db.OrderItems.DeleteAllOnSubmit(delItems);
            
                //delete the order
                db.Orders.DeleteAllOnSubmit(delOrder);

                db.SubmitChanges();
                result = true;
            }

            return result;
        }
        public void SaveAddress(Address address)
        {

            if (address != null) {
                using (DB db = new DB()) {
                    //see if it's in the db
                    Commerce.Data.SqlRepository.Address add;

                    if (address.ID > 0) {
                        add = (from a in _db.Addresses
                               where a.AddressID == address.ID
                               select a).SingleOrDefault() ?? new Commerce.Data.SqlRepository.Address();

                    }
                    else {
                        add = (from a in _db.Addresses
                               where a.UserName == address.UserName && a.Street1 == address.Street1 && a.City == address.City
                               select a).SingleOrDefault() ?? new Commerce.Data.SqlRepository.Address();
                    }

                    //synch it
                    add.City = address.City;
                    add.Street2 = address.Street2;
                    add.Street1 = address.Street1;
                    add.StateOrProvince = address.StateOrProvince;
                    add.LastName = address.LastName;
                    add.FirstName = address.FirstName;
                    add.Email = address.Email;
                    add.Country = address.Country;
                    add.UserName = address.UserName;
                    add.Zip = address.Zip;
                    add.Longitude = address.Longitude;
                    add.Latitude = address.Latitude;

                    //save it
                    if (add.AddressID == 0)
                        db.Addresses.InsertOnSubmit(add);

                    db.SubmitChanges();

                    address.ID = add.AddressID;
                }
            }
        }
 public SqlOrderRepository(DB db, IShippingRepository shippingRepository)
 {
     _shippingRepository = shippingRepository;
     _db = db;
 }
 public SqlMailerRepository(DB db) {
     _db=db;
 }
 public SqlMailerRepository() {
     _db = new DB();
 }
        public void SaveProduct(Product product) {
            
            using(DB db=new DB()){
                
                //see if the product is in the system
                Commerce.Data.SqlRepository.Product dbProduct = 
                    db.Products.Where(x => x.ProductID == product.ID).SingleOrDefault();
                bool isNew = false;
                if (dbProduct == null) {
                    dbProduct = new Commerce.Data.SqlRepository.Product();
                    isNew = true;
                }
                else {
                    //remove them for refresh
                    //wish there was a better way to do this but...
                    db.ProductDescriptors.DeleteAllOnSubmit(from pd in db.ProductDescriptors where pd.ProductID == product.ID select pd);
                }

                //add the descriptors
                foreach (ProductDescriptor pd in product.Descriptors) {
                    Commerce.Data.SqlRepository.ProductDescriptor dbPd = new Commerce.Data.SqlRepository.ProductDescriptor();
                    dbPd.ProductID = product.ID;
                    dbPd.Title = pd.Title;
                    dbPd.Body = pd.Body;
                    dbProduct.ProductDescriptors.Add(dbPd);
                }

                //some left/right
                dbProduct.AllowBackOrder = product.AllowBackOrder;
                dbProduct.BaseUnitPrice = product.Price;
                dbProduct.DeliveryMethodID = (int)product.Delivery;
                dbProduct.DiscountPercent = product.DiscountPercent;
                dbProduct.EstimatedDelivery = product.EstimatedDelivery;
                dbProduct.InventoryStatusID = (int)product.Inventory;
                dbProduct.Manufacturer = product.Manufacturer;
                dbProduct.ProductCode = product.ProductCode;
                dbProduct.ProductName = product.Name;
                dbProduct.WeightInPounds = product.WeightInPounds;
                


                if (isNew)
                    db.Products.InsertOnSubmit(dbProduct);


                db.SubmitChanges();
            
            }
        }
 public SqlTaxRepository(DB db)
 {
     _db = db;
 }
        public void DeleteAddress(int addressID)
        {
            using(DB db=new DB())
            {
                var delAdd = from a in db.Addresses
                             where a.AddressID == addressID
                             select a;
                db.Addresses.DeleteAllOnSubmit(delAdd);
                db.SubmitChanges();
            }

        }
 public SqlShippingRepository(DB db)
 {
     _db = db;
 }
 public SqlCatalogRepository(DB dataContext) {
     //override the current context
     //with the one passed in
     _db = dataContext;
     
 }