コード例 #1
0
        public async Task AddCategoryAsync(CategoryCreationBindingModel model)
        {
            var category = this.Mapper.Map <Category>(model);

            await this.Context.Categories.AddAsync(category);

            await this.Context.SaveChangesAsync();
        }
コード例 #2
0
        public async Task AddCategories_WithNullCategory_ShouldThrowException()
        {
            //Arrange

            CategoryCreationBindingModel categoryModel = null;

            //Act
            Func <Task> addCourse = () => this.service.AddCategoryAsync(categoryModel);

            //Assert
            var exception = await Assert.ThrowsExceptionAsync <ArgumentException>(addCourse);

            Assert.AreEqual(ValidationConstants.CategoryIsDefinedMessage, exception.Message);
        }
コード例 #3
0
        public async Task <int> AddCategoryAsync(CategoryCreationBindingModel model)
        {
            Validator.EnsureNotNull(model, ValidationConstants.CategoryIsDefinedMessage);
            Validator.EnsureStringIsNotNullOrEmpty(model.Name, ValidationConstants.CategoryNameMessage);
            Validator.EnsureStringIsNotNullOrEmpty(model.Slug, ValidationConstants.CategorySlugMessage);

            var category = this.Mapper.Map <Category>(model);

            await this.DbContext.Categories.AddAsync(category);

            await this.DbContext.SaveChangesAsync();

            return(category.Id);
        }
コード例 #4
0
        public async Task <IActionResult> Create(CategoryCreationBindingModel model)
        {
            if (this.categoryService.NameExits(model.Name))
            {
                ModelState.AddModelError(AppConstants.CategoryModelErrorKey, AppConstants.CategoryExistsErrorMessage);
            }

            if (!ModelState.IsValid)
            {
                return(View());
            }

            await this.categoryService.AddCategoryAsync(model);

            return(RedirectToAction(AppConstants.IndexRedirect));
        }
コード例 #5
0
        public async Task <IActionResult> Create(CategoryCreationBindingModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View());
            }


            var categoryId = await this.categoryService.AddCategoryAsync(model);

            this.TempData.Put("__Message", new MessageModel()
            {
                Type    = MessageType.Success,
                Message = "Category created successfully."
            });

            return(RedirectToAction("Details", new { id = categoryId, slug = model.Slug }));
        }
コード例 #6
0
        public async Task AddCategories_WithCategoryWithNullName_ShouldThrowException()
        {
            //Arrange

            CategoryCreationBindingModel categoryModel = new CategoryCreationBindingModel()
            {
                Name           = null,
                Slug           = "null",
                CategoryPicUrl = picLink
            };

            //Act
            Func <Task> addCourse = () => this.service.AddCategoryAsync(categoryModel);

            //Assert
            var exception = await Assert.ThrowsExceptionAsync <ArgumentException>(addCourse);

            Assert.AreEqual(ValidationConstants.CategoryNameMessage, exception.Message);
        }
コード例 #7
0
        public async Task AddCategories_WithProperCategory_ShouldAddCorrectly()
        {
            //Arrange
            const string categoryName  = "New Category Name";
            const string categorySlug  = "new-category-name";
            var          categoryModel = new CategoryCreationBindingModel()
            {
                Name           = categoryName,
                Slug           = categorySlug,
                CategoryPicUrl = picLink
            };

            //Act
            await this.service.AddCategoryAsync(categoryModel);

            //Assert
            var category = this.dbContext.Categories.First();

            Assert.AreEqual(1, this.dbContext.Categories.Count());
            Assert.AreEqual(categoryName, category.Name);
            Assert.AreEqual(categorySlug, category.Slug);
            Assert.AreEqual(picLink, category.CategoryPicUrl);
        }