예제 #1
0
        public IActionResult Index()
        {
            var productCategoryBundle = new ProductCategoryBundle();

            productCategoryBundle.ProductsList = dbContext.Products.ToList();
            return(View("Products", productCategoryBundle));
        }
예제 #2
0
        public IActionResult Categories()
        {
            var productCategoryBundle = new ProductCategoryBundle();

            productCategoryBundle.CategoriesList = dbContext.Categories.ToList();
            return(View("Categories", productCategoryBundle));
        }
예제 #3
0
        public IActionResult ShowCategory(int id)
        {
            var productCategoryBundle = new ProductCategoryBundle();

            // Sets the product and current categories
            productCategoryBundle.Category = dbContext.Categories
                                             .Include(c => c.Associations)
                                             .ThenInclude(a => a.Product)
                                             .FirstOrDefault(c => c.CategoryId == id);

            // Sets the product's not linked categories
            productCategoryBundle.ProductsList = dbContext.Products
                                                 .Include(p => p.Associations)
                                                 .Where(p => p.Associations.All(a => a.CategoryId != id))
                                                 .ToList();

            return(View("Category", productCategoryBundle));
        }
예제 #4
0
        public IActionResult CreateProduct(Product product)
        {
            // Checks model validators. If valid, queries the VB and adds the new product.
            if (ModelState.IsValid)
            {
                // Sets timestamp for product creation
                product.CreatedAt = DateTime.Now;
                product.UpdatedAt = DateTime.Now;

                dbContext.Add(product);
                dbContext.SaveChanges();
                return(RedirectToAction("DisplayProduct"));
            }

            // else, the error messages are returned.
            else
            {
                // creates the bunled model again for display if the data is invalid
                var productCategoryBundle = new ProductCategoryBundle();
                productCategoryBundle.ProductsList = dbContext.Products.ToList();
                return(View("Products", productCategoryBundle));
            }
        }
예제 #5
0
        public IActionResult CreateCategory(Category category)
        {
            // Checks model validators. If valid, queries the VB and adds the new category.
            if (ModelState.IsValid)
            {
                // Sets timestamp for category creation
                category.CreatedAt = DateTime.Now;
                category.UpdatedAt = DateTime.Now;

                // Add Category to DB
                dbContext.Add(category);
                dbContext.SaveChanges();
                return(RedirectToAction("DisplayCategory"));
            }
            // else, the error messages are returned.
            else
            {
                // creates the bunled model again for display if the data is invalid
                var productCategoryBundle = new ProductCategoryBundle();
                productCategoryBundle.CategoriesList = dbContext.Categories.ToList();
                return(View("Categories", productCategoryBundle));
            }
        }