Ejemplo n.º 1
0
        public async Task EditCategory_Edits_Category_When_Correct_Id_Provided()
        {
            int targetId = 1;
            int categoriesCountBefore = categoriesRepository.All().Count();

            var editedCategory = new CategoryOutInDto
            {
                Id          = targetId,
                Title       = "EditedTitle",
                CategoryId  = 5,
                Description = "EditedDescription"
            };

            categoryService.EditCategory(editedCategory);
            int categoriesCountAfter = categoriesRepository.All().Count();

            Assert.Equal(categoriesCountAfter, categoriesCountBefore);
            var categoryFound = await categoriesRepository.All().FirstOrDefaultAsync(x => x.Id == targetId);

            Assert.NotNull(categoryFound);
            Assert.Equal(editedCategory.Title, categoryFound.Title);
            Assert.Equal(editedCategory.CategoryId, categoryFound.CategoryId);
            Assert.Equal(editedCategory.Description, categoryFound.Description);

            var categoriesLeft = categoriesRepository.All().ToArray();

            categoriesRepository.RemoveRange(categoriesLeft);
            await categoriesRepository.SaveChangesAsync();
        }
Ejemplo n.º 2
0
        public async Task GetCategoryInfo_Returns_CategoryInfo_When_CorrectId_Provided(int id, string title, int?categoryId)
        {
            CategoryOutInDto categoryInfo = await categoryService.GetCategoryInfoAsync(id);

            Assert.NotNull(categoryInfo);
            Assert.Equal(id, categoryInfo.Id);
            Assert.Equal(title, categoryInfo.Title);
            Assert.Equal(categoryId, categoryInfo.CategoryId);
        }
 public IActionResult Edit(CategoryOutInDto dto)
 {
     memoryCache.Remove(GlobalConstants.CasheCategoriesInButtonName);
     ViewData["ExistingCategories"] = categoryService.GetAllMinified();
     if (((ICollection <CategoryMiniOutDto>)ViewData["ExistingCategories"]).Any(x => x.Title.ToLower() == dto.Title.ToLower() && x.Id != dto.Id))
     {
         ModelState.AddModelError("NameTaken", $"Name {dto.Title} is already used for category name!");
     }
     if (dto.CategoryId == dto.Id)
     {
         ModelState.AddModelError("Circular Reference", $"Category can not be in itself");
     }
     if (ModelState.IsValid)
     {
         categoryService.EditCategory(dto);
         return(RedirectToAction(nameof(Manage)));
     }
     return(View(dto));
 }
 public void EditCategory(CategoryOutInDto dto)
 {
     lock (LockObject)
     {
         if (dto.CategoryId == -1)
         {
             dto.CategoryId = null;
         }
         var fatherCategoryIsValid = dto.CategoryId == null || categoryRepository.All().Any(x => x.Id == dto.CategoryId.Value);
         var category            = categoryRepository.All().FirstOrDefault(x => x.Id == dto.Id);
         var editedTitleIsUnique = !categoryRepository.All().Any(x => x.Id != dto.Id && x.Title.ToLower() == dto.Title.ToLower());
         if (category != null && fatherCategoryIsValid && editedTitleIsUnique)
         {
             category.Title       = dto.Title;
             category.CategoryId  = dto.CategoryId;
             category.Description = dto.Description;
             categoryRepository.SaveChangesAsync().GetAwaiter().GetResult();
         }
     }
 }
Ejemplo n.º 5
0
        public async Task EditCategory_DoNotEditCategory_When_NotCorrect_Id_Provided()
        {
            int targetId = -10;
            int categoriesCountBefore = categoriesRepository.All().Count();
            var editedCategory        = new CategoryOutInDto
            {
                Id          = targetId,
                Title       = "EditedTitle",
                CategoryId  = 5,
                Description = "EditedDescription"
            };

            categoryService.EditCategory(editedCategory);
            int categoriesCountAfter = categoriesRepository.All().Count();

            Assert.Equal(categoriesCountAfter, categoriesCountBefore);
            var categoryFound = await categoriesRepository.All().FirstOrDefaultAsync(x => x.Id == targetId);

            Assert.Null(categoryFound);
            Assert.False(categoriesRepository.All().Any(x => x.Title == editedCategory.Title || x.Description == editedCategory.Description));
        }
Ejemplo n.º 6
0
        public async Task EditCategory_DoNotEditCategory_When_ExistingTitle_Provided()
        {
            int    targetId              = 1;
            string existingTitle         = "Primary2";
            int    categoriesCountBefore = categoriesRepository.All().Count();
            var    editedCategory        = new CategoryOutInDto
            {
                Id          = targetId,
                Title       = existingTitle,
                CategoryId  = null,
                Description = "EditedDescription"
            };

            categoryService.EditCategory(editedCategory);
            int categoriesCountAfter = categoriesRepository.All().Count();

            Assert.Equal(categoriesCountAfter, categoriesCountBefore);
            var categoryFound = await categoriesRepository.All().FirstOrDefaultAsync(x => x.Id == targetId);

            Assert.NotNull(categoryFound);
            Assert.NotEqual(categoryFound.Title, editedCategory.Title);
            Assert.NotEqual(categoryFound.Description, editedCategory.Description);
        }
Ejemplo n.º 7
0
        public async Task EditCategory_DoNotEditCategory_When_Incorrect_TargetId_Provided()
        {
            int targetId              = 1;
            int invalidTargetId       = -10;
            int categoriesCountBefore = categoriesRepository.All().Count();
            var editedCategory        = new CategoryOutInDto
            {
                Id          = targetId,
                Title       = "EditedTitle",
                CategoryId  = invalidTargetId,
                Description = "EditedDescription"
            };

            categoryService.EditCategory(editedCategory);
            int categoriesCountAfter = categoriesRepository.All().Count();

            Assert.Equal(categoriesCountAfter, categoriesCountBefore);
            var categoryFound = await categoriesRepository.All().FirstOrDefaultAsync(x => x.Id == targetId);

            Assert.NotNull(categoryFound);
            Assert.NotEqual(categoryFound.Title, editedCategory.Title);
            Assert.NotEqual(categoryFound.Description, editedCategory.Description);
        }