// GET: Category
        public ActionResult Index()
        {
            var viewModel = new GetCategoriesViewModel()
            {
                Categories = _unitOfWork.CategoryRepository.GetAll().ToList()
            };

            return(View(viewModel));
        }
        public async Task <IActionResult> Create(GetCategoriesViewModel model)
        {
            if (!this.ModelState.IsValid)
            {
                return(this.View("Create", model));
            }

            await this.categoriesService.CreateCategoryAsync(model);

            return(this.RedirectToAction("All"));
        }
Example #3
0
        public async Task CreateCategoryAsync(GetCategoriesViewModel model)
        {
            var category = this.categoryRepository.All().FirstOrDefault(x => x.Name == model.Name);

            if (category == null)
            {
                category = new Category()
                {
                    Name        = model.Name,
                    Description = model.Description,
                };
            }

            await this.categoryRepository.AddAsync(category);

            await this.categoryRepository.SaveChangesAsync();
        }