示例#1
0
        public CategoryModel GetCategoryModel(int CategoryId)
        {
            using (var db = new TankshopDbContext())
            {
                var c = db.Categories.Find(CategoryId);

                var productModels = new List<ProductModel>();

                foreach (var product in c.Products)
                {
                    var imageModels = new List<ImageModel>();

                    foreach (var image in product.Images)
                    {
                        var imageModel = new ImageModel()
                        {
                            ImageId = image.ImageId,
                            ImageUrl = image.ImageUrl,
                            ProductId = image.ProductId
                        };
                        imageModels.Add(imageModel);
                    }

                    var productModel = new ProductModel()
                    {
                        CategoryId = product.CategoryId,
                        CategoryName = product.Category.Name,
                        Description = product.Description,
                        Price = product.Price,
                        ProductId = product.ProductId,
                        ProductName = product.Name,
                        Stock = product.Stock,
                        Images = imageModels
                    };

                    productModels.Add(productModel);
                }

                var categoryModel = new CategoryModel()
                {
                    CategoryId = c.CategoryId,
                    CategoryName = c.Name,
                    Products = productModels
                };

                return categoryModel;
            }
        }
示例#2
0
        public List<CategoryModel> GetAllCategoryModels()
        {
            var db = new TankshopDbContext();

            var dbCategories = db.Categories.ToList();
            var categoryModels = new List<CategoryModel>();

            foreach (var c in dbCategories)
            {
                var productModels = new List<ProductModel>();

                foreach(var product in c.Products)
                {
                    var imageModels = new List<ImageModel>();

                    foreach(var image in product.Images)
                    {
                        var imageModel = new ImageModel()
                        {
                            ImageId = image.ImageId,
                            ImageUrl = image.ImageUrl,
                            ProductId = image.ProductId
                        };
                        imageModels.Add(imageModel);
                    }

                    var productModel = new ProductModel()
                    {
                        CategoryId = product.CategoryId,
                        CategoryName = product.Category.Name,
                        Description = product.Description,
                        Price = product.Price,
                        ProductId = product.ProductId,
                        ProductName = product.Name,
                        Stock = product.Stock,
                        Images = imageModels
                    };

                    productModels.Add(productModel);
                }

                var categoryModel = new CategoryModel()
                {
                    CategoryId = c.CategoryId,
                    CategoryName = c.Name,
                    Products = productModels
                };

                categoryModels.Add(categoryModel);

            }
            return categoryModels;
        }
        public void Category_DeleteCategory_GoodInput()
        {
            //Arrange
            var controller = new CategoryController(new CategoryBLL(new CategoryRepoStub()));
            string categoryId = "2";
            var expectedCategory = new CategoryModel { CategoryId = 2, CategoryName = "test name" };

            //Act
            var viewResult = controller.DeleteCategory(categoryId) as ViewResult;
            var actualCategory = controller.ViewBag.Category;

            //Assert
            Assert.AreEqual(expectedCategory.CategoryId, actualCategory.CategoryId);
            Assert.AreEqual(expectedCategory.CategoryName, actualCategory.CategoryName);

            Assert.AreEqual("", viewResult.ViewName);
        }