public void Create_WithCorrectData_ShouldSuccessfullyCreate()
        {
            string errorMessagePrefix = "ChildCategoriesService Create() method does not work properly.";

            var context = UniShopDbContextInMemoryFactory.InitializeContext();

            this.SeedData(context);
            this.childCategoriesService = new ChildCategoriesService(context, new ParentCategoriesService(context));

            int parentCategoryId = context.ParentCategories.First().Id;

            ChildCategoryServiceModel testChildCategory = new ChildCategoryServiceModel
            {
                Name             = "Настолни компютри",
                ParentCategoryId = parentCategoryId
            };

            int expectedCount = context.ChildCategories.Count() + 1;

            bool actualResult = this.childCategoriesService.Create(testChildCategory);
            int  actualCount  = context.ChildCategories.Count();

            Assert.True(actualResult, errorMessagePrefix);
            Assert.Equal(expectedCount, actualCount);
        }
        public IActionResult Create(ChildCategoryCreateInputModel childCategoryCreateInputModel)
        {
            if (!this.ModelState.IsValid)
            {
                return(this.View());
            }

            ChildCategoryServiceModel childCategoryServiceModel = childCategoryCreateInputModel.To <ChildCategoryServiceModel>();

            this.childCategoriesService.Create(childCategoryServiceModel);

            return(RedirectToAction("All"));
        }
        public void GetChildCategoryById_WithNonExistentId_ShouldReturnNull()
        {
            string errorMessagePrefix = "ChildCategoriesService GetChildCategoryById() method does not work properly.";

            var context = UniShopDbContextInMemoryFactory.InitializeContext();

            this.SeedData(context);
            this.childCategoriesService = new ChildCategoriesService(context, new ParentCategoriesService(context));

            int nonExistentId = 5;

            ChildCategoryServiceModel actualResults = this.childCategoriesService.GetChildCategoryById(nonExistentId);


            Assert.True(actualResults == null, errorMessagePrefix);
        }
        public void GetChildCategoryById_WithExistentId_ShouldReturnCorrectResults()
        {
            string errorMessagePrefix = "ChildCategoriesService GetChildCategoryById() method does not work properly.";

            var context = UniShopDbContextInMemoryFactory.InitializeContext();

            this.SeedData(context);
            this.childCategoriesService = new ChildCategoriesService(context, new ParentCategoriesService(context));


            ChildCategoryServiceModel expectedResults = context.ChildCategories.First().To <ChildCategoryServiceModel>();
            ChildCategoryServiceModel actualResults   = this.childCategoriesService.GetChildCategoryById(expectedResults.Id);


            Assert.True(expectedResults.Id == actualResults.Id, errorMessagePrefix + " " + "Id is not returned properly.");
            Assert.True(expectedResults.Name == actualResults.Name, errorMessagePrefix + " " + "Name is not returned properly.");
            Assert.True(expectedResults.Products.Count() == actualResults.Products.Count(), errorMessagePrefix + " " + "Products Count is not returned properly.");
            Assert.True(expectedResults.ParentCategoryId == actualResults.ParentCategoryId, errorMessagePrefix + " " + "ParentCategoryId is not returned properly.");
        }
        public void Edit_WithNonExistentId_ShouldReturnFalse()
        {
            string errorMessagePrefix = "ChildCategoriesService Edit() method does not work properly.";

            var context = UniShopDbContextInMemoryFactory.InitializeContext();

            this.SeedData(context);
            this.childCategoriesService = new ChildCategoriesService(context, new ParentCategoriesService(context));

            ChildCategoryServiceModel childCategoryFromDb = context.ChildCategories.First().To <ChildCategoryServiceModel>();

            ChildCategoryServiceModel NonExistrentId = childCategoryFromDb;

            NonExistrentId.Id = 1000;

            bool actualResult = this.childCategoriesService.Edit(NonExistrentId);

            Assert.False(actualResult, errorMessagePrefix);
        }
        public bool Create(ChildCategoryServiceModel childCategoryServiceModel)
        {
            var isParentCategoryExists = this.parentCategoriesService.IsHaveParentCategory(childCategoryServiceModel.ParentCategoryId);

            if (!isParentCategoryExists)
            {
                return(false);
            }


            ChildCategory childCategory = new ChildCategory
            {
                Name             = childCategoryServiceModel.Name,
                ParentCategoryId = childCategoryServiceModel.ParentCategoryId
            };

            this.context.ChildCategories.Add(childCategory);
            int result = this.context.SaveChanges();


            return(result > 0);
        }
        public void Edit_WithCorrectData_ShouldPassSuccessfully()
        {
            string errorMessagePrefix = "ChildCategoriesService Edit() method does not work properly.";

            var context = UniShopDbContextInMemoryFactory.InitializeContext();

            this.SeedData(context);
            this.childCategoriesService = new ChildCategoriesService(context, new ParentCategoriesService(context));

            ChildCategoryServiceModel childCategoryFromDb = context.ChildCategories.First().To <ChildCategoryServiceModel>();

            ChildCategoryServiceModel expectedChildCategory = childCategoryFromDb;

            expectedChildCategory.Name = "Edit";

            bool actualResult = this.childCategoriesService.Edit(expectedChildCategory);

            ChildCategoryServiceModel actualChildCategory = context.ChildCategories.FirstOrDefault(p => p.Id == childCategoryFromDb.Id).To <ChildCategoryServiceModel>();

            Assert.True(actualChildCategory.Name == expectedChildCategory.Name, errorMessagePrefix + " " + "Name not editted properly.");
            Assert.True(actualResult, errorMessagePrefix);
        }
        public void Create_WithNonExistentParentCategory_ShouldReturnFalse()
        {
            string errorMessagePrefix = "ChildCategoriesService Create() method does not work properly.";

            var context = UniShopDbContextInMemoryFactory.InitializeContext();

            this.SeedData(context);
            this.childCategoriesService = new ChildCategoriesService(context, new ParentCategoriesService(context));

            ChildCategoryServiceModel testChildCategory = new ChildCategoryServiceModel
            {
                Name             = "Настолни компютри",
                ParentCategoryId = 10
            };

            int expectedCount = context.ChildCategories.Count();

            bool actualResult = this.childCategoriesService.Create(testChildCategory);
            int  actualCount  = context.ChildCategories.Count();

            Assert.False(actualResult, errorMessagePrefix);
            Assert.Equal(expectedCount, actualCount);
        }
        public bool Edit(ChildCategoryServiceModel childCategoryServiceModel)
        {
            var childCategory = this.context.ChildCategories.FirstOrDefault(c => c.Id == childCategoryServiceModel.Id);

            if (childCategory == null)
            {
                return(false);
            }

            var parentCategory = this.context.ParentCategories.FirstOrDefault(p => p.Id == childCategoryServiceModel.ParentCategoryId);

            if (parentCategory == null)
            {
                return(false);
            }

            childCategory.Name             = childCategoryServiceModel.Name;
            childCategory.ParentCategoryId = childCategoryServiceModel.ParentCategoryId;

            this.context.Update(childCategory);
            int result = this.context.SaveChanges();

            return(result > 0);
        }