public Picture GetPictureById(int id)
 {
     using (var db = new BatikStoreEntities())
     {
         return db.Pictures.SingleOrDefault(c => c.Id == id);
     }
 }
 public List<Picture> GetPicturesByProductId(int productId)
 {
     using (var db = new BatikStoreEntities())
     {
         return db.Pictures.Where(c => c.Product_ProductId == productId).ToList();
     }
 }
 public User GetUserByEmail(string email)
 {
     using (var db = new BatikStoreEntities())
     {
         return db.Users.SingleOrDefault(c => c.Email == email);
     }
 }
 public Product GetByNumber(string number)
 {
     using (var db = new BatikStoreEntities())
     {
         return db.Products.Include("Model").Include("Size").Include("Type").Include("Origin").Include("Pictures").SingleOrDefault(c => c.Number.Equals(number));
     }
 }
 public List<Model> GetAll()
 {
     using (var db = new BatikStoreEntities())
     {
         return db.Models.ToList();
     }
 }
 public List<Size> GetAll()
 {
     using (var db = new BatikStoreEntities())
     {
         return db.Sizes.ToList();
     }
 }
 public List<Origin> GetAll()
 {
     using (var db = new BatikStoreEntities())
     {
         return db.Origins.ToList();
     }
 }
 public Origin GetOriginById(int id)
 {
     using (var db = new BatikStoreEntities())
     {
         return db.Origins.FirstOrDefault(c => c.Id == id);
     }
 }
 public List<Product> GetLatestProducts()
 {
     using (var db = new BatikStoreEntities())
     {
         return db.Products.Include("Model").Include("Size").Include("Type").Include("Origin").Include("Pictures").OrderByDescending(c => c.DateEntered).Take(Constant.LatestProducts).ToList();
     }
 }
 public Product GetById(int Id)
 {
     using (var db = new BatikStoreEntities())
     {
         return db.Products.Include("Model").Include("Size").Include("Type").Include("Origin").Include("Pictures").SingleOrDefault(c => c.Id == Id);
     }
 }
 public Model GetModelById(int id)
 {
     using (var db = new BatikStoreEntities())
     {
         return db.Models.FirstOrDefault(c => c.Id == id);
     }
 }
 public List<Product> GetAll()
 {
     using (var db = new BatikStoreEntities())
     {
         return db.Products.Include("Model").Include("Size").Include("Type").Include("Origin").Include("Pictures").ToList();
     }
 }
 public Size GetSizeById(int id)
 {
     using (var db = new BatikStoreEntities())
     {
         return db.Sizes.FirstOrDefault(c => c.Id == id);
     }
 }
 public void Add(Size entity)
 {
     using (var db = new BatikStoreEntities())
     {
         db.Sizes.AddObject(entity);
         db.SaveChanges();
     }
 }
 public void Add(Product entity)
 {
     using (var db = new BatikStoreEntities())
     {
         db.Products.AddObject(entity);
         db.SaveChanges();
     }
 }
        public List<Product> GetRandomPromotedProducts()
        {
            using (var db = new BatikStoreEntities())
            {
                var products = db.Products.Include("Model").Include("Size").Include("Type").Include("Origin").Include("Pictures").Where(c => c.Promoted == true).ToList();

                return products.GetRandomFromList(Constant.PromotedProducts).ToList();
            }
        }
 public void Delete(int id)
 {
     using (var db = new BatikStoreEntities())
     {
         var existing = db.Sizes.SingleOrDefault(c => c.Id == id);
         if (existing != null)
         {
             db.Sizes.DeleteObject(existing);
             db.SaveChanges();
         }
     }
 }
 public void Update(Size entity)
 {
     using (var db = new BatikStoreEntities())
     {
         var existing = db.Sizes.SingleOrDefault(c => c.Id == entity.Id);
         if (existing != null)
         {
             db.Sizes.ApplyCurrentValues(entity);
             db.SaveChanges();
         }
     }
 }
 public bool IsOriginAlreadyUsed(int originId)
 {
     using (var db = new BatikStoreEntities())
     {
         return db.Products.Any(c => c.Origin_OriginId == originId);
     }
 }
 public bool IsTypeAlreadyUsed(int typeId)
 {
     using (var db = new BatikStoreEntities())
     {
         return db.Products.Any(c => c.Type_TypeId == typeId);
     }
 }
 public List<Product> Search(string searchCriteria, int skip)
 {
     using (var db = new BatikStoreEntities())
     {
         if(String.IsNullOrEmpty(searchCriteria))
             return db.Products.Include("Model").Include("Size").Include("Type").Include("Origin").Include("Pictures").OrderByDescending(c => c.DateEntered).Skip(skip).Take(Constant.ItemPerPage).ToList();
         else
             return db.Products.Include("Model").Include("Size").Include("Type").Include("Origin").Include("Pictures").Where(searchCriteria).OrderByDescending(c => c.DateEntered).Skip(skip).Take(Constant.ItemPerPage).ToList();
     }
 }
 public bool IsModelAlreadyUsed(int modelId)
 {
     using (var db = new BatikStoreEntities())
     {
         return db.Products.Any(c => c.Model_ModelId == modelId);
     }
 }
 public bool IsSizeAlreadyUsed(int sizeId)
 {
     using (var db = new BatikStoreEntities())
     {
         return db.Products.Any(c => c.Size_SizeId == sizeId);
     }
 }