public async Task <IActionResult> Edit(int categoryId)
        {
            ViewData["ExistingCategories"] = categoryService.GetAllMinified();
            CategoryInDto categoryDtos = await categoryService.GetCategoryInfoAsync(categoryId);

            return(View(categoryDtos));
        }
예제 #2
0
        public async Task AddCategory_Adds_NewCategory_When_UniqueTitle_Provided()
        {
            int    categoryId  = 4;
            string title       = "NewAddedCategory";
            string description = "NewCagetoryDescription";

            CategoryInDto categoryNew = new CategoryInDto
            {
                CategoryId  = categoryId,
                Title       = title,
                Description = description
            };

            int expectedCountAfterAdding = categoriesRepository.All().Count() + 1;
            await categoryService.AddCategoryAsync(categoryNew);

            int actualCountAfterAdding = categoriesRepository.All().Count();

            Assert.Equal(expectedCountAfterAdding, actualCountAfterAdding);
            var categoryAdded = categoriesRepository.All().Last();

            Assert.Equal(categoryId, categoryAdded.CategoryId);
            Assert.Equal(title, categoryAdded.Title);
            Assert.Equal(description, categoryAdded.Description);
        }
예제 #3
0
        public async Task AddCategory_DoNot_Add_NewCategory_When_NonExistingTargetId_Provided()
        {
            int    categoryId     = 999;
            string repeatingTitle = "NewCategory2";
            string description    = "NewCagetoryDescription";

            CategoryInDto categoryNew = new CategoryInDto
            {
                CategoryId  = categoryId,
                Title       = repeatingTitle,
                Description = description
            };

            int expectedCountAfterAdding = categoriesRepository.All().Count();
            await categoryService.AddCategoryAsync(categoryNew);

            int actualCountAfterAdding = categoriesRepository.All().Count();

            Assert.Equal(expectedCountAfterAdding, actualCountAfterAdding);
            var categoryAdded = categoriesRepository.All().Last();

            Assert.NotEqual(categoryId, categoryAdded.CategoryId);
            Assert.NotEqual(repeatingTitle, categoryAdded.Title);
            Assert.NotEqual(description, categoryAdded.Description);
        }
        public async Task AddCategoryAsync(CategoryInDto dto)
        {
            var newCategory = mapper.Map <Category>(dto);

            newCategory.CategoryId = newCategory.CategoryId == -1 ? null : newCategory.CategoryId;
            bool isTitleUnique         = categoryRepository.All().Select(x => x.Title).All(x => x.ToLower() != dto.Title.ToLower());
            bool isTargetCategoryValid = (newCategory.CategoryId is null) || categoryRepository.All().Select(x => x.Id).Any(x => x == newCategory.CategoryId.Value);

            if (!isTitleUnique || !isTargetCategoryValid)
            {
                return;
            }
            await categoryRepository.AddAssync(newCategory);

            await categoryRepository.SaveChangesAsync();
        }
        public async Task <IActionResult> Create(CategoryInDto dto)
        {
            memoryCache.Remove(GlobalConstants.CasheCategoriesInButtonName);
            ViewData["ExistingCategories"] = categoryService.GetAllMinified();
            if (((ICollection <CategoryMiniOutDto>)ViewData["ExistingCategories"]).Any(x => x.Title.ToLower() == dto.Title.ToLower()))
            {
                ModelState.AddModelError("NameTaken", $"Name {dto.Title} is already used for category name!");
            }
            if (ModelState.IsValid)
            {
                await categoryService.AddCategoryAsync(dto);

                return(RedirectToAction(nameof(Manage)));
            }
            return(View(dto));
        }
예제 #6
0
        public ActionResult <CategoryOutDto> Create([FromBody] CategoryInDto categoryInDto)
        {
            //Check if inputs are valid
            if (string.IsNullOrEmpty(categoryInDto.ImageUrl))
            {
                return(BadRequest(new Message("Please give valid image Url")));
            }

            if (string.IsNullOrEmpty(categoryInDto.Name))
            {
                return(BadRequest(new Message("Please give valid name")));
            }

            if (string.IsNullOrEmpty(categoryInDto.ParentId))
            {
                return(BadRequest(new Message("Please give valid parent Id")));
            }

            //Check if request is sent by admin.
            if (!AuthorizationHelpers.IsAdmin(HttpContext.User))
            {
                return(Unauthorized(new Message("Unauthorized user.")));
            }

            if (_mainCategoryRepository.GetById(categoryInDto.ParentId) == null)
            {
                return(BadRequest(new Message("There is no main category with id: " + categoryInDto.ParentId)));
            }

            var categoryIn = _mapper.Map <Category>(categoryInDto);

            if (_categoryRepository.Add(categoryIn))
            {
                var categoryOutDto = _mapper.Map <CategoryOutDto>(categoryIn);

                return(Ok(categoryOutDto));
            }

            return(BadRequest(new Message("Error when creating category")));
        }
예제 #7
0
        public ActionResult <CategoryOutDto> Update(string id, [FromBody] CategoryInDto categoryInDto)
        {
            //Check if request is sent by admin.
            if (!AuthorizationHelpers.IsAdmin(HttpContext.User))
            {
                return(Unauthorized(new Message("Unauthorized user.")));
            }

            //Check if there exists a category with given id
            var category = _categoryRepository.GetById(id);

            if (category == null)
            {
                return(NotFound(new Message("No such category with this id: " + id)));
            }

            if (!string.IsNullOrWhiteSpace(categoryInDto.ImageUrl))
            {
                category.ImageUrl = categoryInDto.ImageUrl;
            }

            if (!string.IsNullOrWhiteSpace(categoryInDto.Name))
            {
                category.Name = categoryInDto.Name;
            }

            if (!string.IsNullOrWhiteSpace(categoryInDto.ParentId))
            {
                category.ParentId = categoryInDto.ParentId;
            }

            //Update category
            if (_categoryRepository.Update(category))
            {
                var categoryOutDto = _mapper.Map <CategoryOutDto>(category);
                return(Ok(categoryOutDto));
            }

            return(BadRequest(new Message("Error when updating category")));
        }