Exemplo n.º 1
0
        public void Get()
        {
            PTCEntities db = new PTCEntities();

            Products = db.Products.OrderBy(p => p.ProductName).ToList();
            SetUIState(PageConstants.LIST);
        }
Exemplo n.º 2
0
        public Product Get(int productId)
        {
            PTCEntities db = new PTCEntities();

            Entity = db.Products.Find(productId);
            return(Entity);
        }
        public void LoadCategories()
        {
            PTCEntities db = new PTCEntities();

            // Load categories
            Categories.AddRange(db.Categories);
        }
Exemplo n.º 4
0
        public void Delete(int productId)
        {
            PTCEntities db = new PTCEntities();

            Product product = db.Products.Find(productId);

            db.Products.Remove(product);
            db.SaveChanges();

            Get();
        }
Exemplo n.º 5
0
        public void Search()
        {
            PTCEntities db = new PTCEntities();

            // Perform Search
            Products = db.Products.Where(p =>
                                         (SearchEntity.CategoryId == 0 ? true :
                                          p.Category.CategoryId == SearchEntity.CategoryId) &&
                                         (string.IsNullOrEmpty(SearchEntity.ProductName) ? true :
                                          p.ProductName.Contains(SearchEntity.ProductName))).
                       OrderBy(p => p.ProductName).ToList();

            SetUIState(PageConstants.LIST);
        }
Exemplo n.º 6
0
        public void SearchPrice()
        {
            PTCEntities db = new PTCEntities();

            ProductsMaxPrice = db.Products.Where(p =>
                                                 (SearchEntity.MaxPrice) >= (p.Price))
                               .OrderBy(p => p.Price).ToList();

            ProductsMinPrice = db.Products.Where(p =>
                                                 (SearchEntity.MinPrice) <= (p.Price))
                               .OrderBy(p => p.Price).ToList();


            SetUIState(PageConstants.LIST);
        }
Exemplo n.º 7
0
        public void LoadSearchCategories()
        {
            PTCEntities db = new PTCEntities();

            if (Categories.Count == 0)
            {
                SearchCategories.AddRange(db.Categories);
            }
            else
            {
                SearchCategories.AddRange(Categories);
            }

            Category entity = new Category();

            entity.CategoryId   = 0;
            entity.CategoryName = "--- Search All Categories ---";
            SearchCategories.AddRange(Categories);
            SearchCategories.Insert(0, entity);
        }
Exemplo n.º 8
0
        public void Save()
        {
            Messages.Clear();

            PTCEntities db = new PTCEntities();

            // Ensure the correct category is set
            Entity.Category = db.Categories.Find(Entity.Category.CategoryId);

            try {
                // Either Update or Insert product
                if (PageMode == PageConstants.EDIT)
                {
                    db.Entry(Entity).State = EntityState.Modified;
                    db.SaveChanges();
                }
                else if (PageMode == PageConstants.ADD)
                {
                    db.Products.Add(Entity);
                    db.SaveChanges();
                }

                // Get all the data again in case anything changed
                Get();
            }
            catch (DbEntityValidationException ex) {
                IsValid = false;

                // Validation errors
                foreach (var errors in ex.EntityValidationErrors)
                {
                    foreach (var item in errors.ValidationErrors)
                    {
                        Messages.AddModelError(item.PropertyName, item.ErrorMessage);
                    }
                }

                // Set page state
                SetUIState(PageMode);
            }
        }
        public void LoadSearchCategories()
        {
            PTCEntities db = new PTCEntities();

            if (Categories.Count == 0)
            {
                // Load categories
                SearchCategories.AddRange(db.Categories);
            }
            else
            {
                SearchCategories.AddRange(Categories);
            }

            // Add category for 'Search All'
            Category entity = new Category();

            entity.CategoryId   = 0;
            entity.CategoryName = "-- Search All Categories --";
            SearchCategories.AddRange(Categories);
            // Insert "Search" at the top
            SearchCategories.Insert(0, entity);
        }
Exemplo n.º 10
0
        public void Save()
        {
            Messages.Clear();
            PTCEntities db = new PTCEntities();

            Entity.Category = db.Categories.Find(Entity.Category.CategoryId);

            try
            {
                if (PageMode == PageConstants.EDIT)
                {
                    db.Entry(Entity).State = System.Data.Entity.EntityState.Modified;
                    db.SaveChanges();
                }
                else if (PageMode == PageConstants.ADD)
                {
                    db.Products.Add(Entity);
                    db.SaveChanges();
                }

                Get();
            }
            catch (DbEntityValidationException ex)
            {
                IsValid = false;
                foreach (var errors in ex.EntityValidationErrors)
                {
                    foreach (var item in errors.ValidationErrors)
                    {
                        Messages.AddModelError(item.PropertyName, item.ErrorMessage);
                    }
                }

                SetUIState(PageMode);
            }
        }