Beispiel #1
0
        public async Task GetByIdAsync_WithValidId_ShouldReturnCorrectly()
        {
            var categoryName = "test";

            var category = new Category
            {
                Name = categoryName
            };

            await context.Categories.AddAsync(category);

            await context.SaveChangesAsync();

            var categoryFromDb = await context.Categories.FirstOrDefaultAsync(c => c.Name == categoryName);

            var actualCategory = await service.GetByIdAsync(categoryFromDb.Id.ToString());

            var expectedCategory = new CategoryServiceModel
            {
                Name         = categoryFromDb.Name,
                CssIconClass = categoryFromDb.CssIconClass,
                Id           = categoryFromDb.Id,
            };

            AssertEx.PropertyValuesAreEquals(actualCategory, expectedCategory);
        }
        public async Task <bool> CreateAllAsync(IList <string> names, IList <string> cssIcons = null)
        {
            if (names == null)
            {
                throw new ArgumentNullException(NullCategoryNamesListErrorMessage);
            }

            if (cssIcons != null && cssIcons.Count != 0 && cssIcons.Count != names.Count)
            {
                throw new ArgumentNullException(InvalidCategoryIconList);
            }

            for (int i = 0; i < names.Count; i++)
            {
                var categoryServiceModel = new CategoryServiceModel
                {
                    Name         = names[i],
                    CssIconClass = null
                };

                if (cssIcons != null)
                {
                    categoryServiceModel.CssIconClass = cssIcons[i];
                }

                await CreateAsync(categoryServiceModel);
            }

            var result = await context.SaveChangesAsync();

            return(result > 0);
        }
        private async Task <CategoryServiceModel> Update(CategoryServiceModel model)
        {
            var category = await _appDbContext.Categories.FindAsync(model.Id);

            category.Name = model.Name.Trim();
            await _appDbContext.SaveChangesAsync();

            return(Mapper.Map <CategoryServiceModel>(category));
        }
Beispiel #4
0
 private LeafSelectorItemViewModel MapFrom(CategoryServiceModel m)
 {
     return(m == null ? null : new LeafSelectorItemViewModel()
     {
         Id = m.Id,
         Text = m.Text,
         IsLeaf = m.IsLeaf
     });
 }
        public async Task <CategoryServiceModel> CreateCategoryAsync(CategoryServiceModel categoryServiceModel)
        {
            Category category = categoryServiceModel.To <Category>();

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

            await this.context.SaveChangesAsync();

            return(categoryServiceModel);
        }
Beispiel #6
0
        public async Task CreateAsync_WithInvalidName_ShouldThrowArgumentNullException()
        {
            var category = new CategoryServiceModel
            {
                Name         = null,
                CssIconClass = null
            };

            Assert.ThrowsAsync <ArgumentNullException>(async() => await service.CreateAsync(category));
        }
Beispiel #7
0
        public async Task <int> CreateAsync(CategoryServiceModel input)
        {
            var category = input.To <Category>();

            await this.categoryRepository.AddAsync(category);

            await this.categoryRepository.SaveChangesAsync();

            return(category.Id);
        }
        public IActionResult CreateCategory(CategoryCreateBindingModel categoryCreateBindingModel)
        {
            CategoryServiceModel categoryServiceModel = new CategoryServiceModel
            {
                Name = categoryCreateBindingModel.Name
            };

            productService.CreateCategory(categoryServiceModel);
            return(Redirect("/"));
        }
Beispiel #9
0
        public async Task <IActionResult> Create([FromBody] CategoryServiceModel model)
        {
            var exists = await this.categories.CategoryExistsAsync(model.Name);

            if (exists)
            {
                return(BadRequest("Category already exists."));
            }

            return(Ok(await this.categories.CreateAsync(model.Name)));
        }
Beispiel #10
0
        public bool CreateCategory(CategoryServiceModel categoryServiceModel)
        {
            Category category = new Category()
            {
                Name = categoryServiceModel.Name
            };

            shopDbContext.Categories.Add(category);
            var result = shopDbContext.SaveChanges();

            return(result > 0);
        }
Beispiel #11
0
        public async Task UpdateAsync(CategoryServiceModel input)
        {
            var category = await this.categoryRepository
                           .GetByIdAsync(input.Id);

            category.Name             = input.Name;
            category.ParentCategoryId = input.ParentCategoryId;
            category.Description      = input.Description;
            category.ImageUrl         = input.ImageUrl;

            this.categoryRepository.Update(category);
            await this.categoryRepository.SaveChangesAsync();
        }
        public async Task <Option <CategoryServiceModel, Error> > UpdateByModel(CategoryServiceModel model)
        {
            if (await Exists(model.Id))
            {
                if (await Exists(model.Name))
                {
                    return(Option.None <CategoryServiceModel, Error>($"Category '{model.Name}' already exists.".ToError()));
                }

                return((await Update(model)).Some <CategoryServiceModel, Error>());
            }

            return(Option.None <CategoryServiceModel, Error>($"Category with ID: {model.Id} does not exists.".ToError()));
        }
Beispiel #13
0
        public async Task CreateAsync_WithValidData_ShouldCreateSuccessfully()
        {
            var category = new CategoryServiceModel
            {
                Name         = "Test",
                CssIconClass = "test"
            };

            var result = await service.CreateAsync(category);

            var actualCategoryCount   = context.Categories.Count();
            var expectedCategoryCount = 1;

            Assert.True(result);
            Assert.AreEqual(expectedCategoryCount, actualCategoryCount);
        }
Beispiel #14
0
        public async Task <CategoryServiceModel> GetByIdAsync(string id)
        {
            if (!await ContainsByIdAsync(id))
            {
                throw new ArgumentException(InvalidIdErrorMessage);
            }

            var category = await context.Categories.SingleOrDefaultAsync(c => c.Id == id);

            var categoryServiceModel = new CategoryServiceModel
            {
                Id           = category.Id,
                Name         = category.Name,
                CssIconClass = category.CssIconClass
            };

            return(categoryServiceModel);
        }
        public void Remove(CategoryServiceModel category)
        {
            if (String.IsNullOrWhiteSpace(category.Name))
            {
                throw new ArgumentException("Category name cannot be null!");
            }

            var categoryToRemove = context.Categories
                                   .FirstOrDefault(c => c.Name == category.Name);

            if (categoryToRemove == null)
            {
                throw new InvalidOperationException("No such category exists!");
            }

            context.Categories.Remove(categoryToRemove);
            context.SaveChanges();
        }
Beispiel #16
0
        public async Task <CategoryServiceModel> GetByNameAsync(string name)
        {
            var category = await context.Categories.SingleOrDefaultAsync(c => c.Name == name);

            if (category == null)
            {
                throw new ArgumentException(InvalidNameErrorMessage);
            }

            var categoryServiceModel = new CategoryServiceModel
            {
                Id           = category.Id,
                Name         = category.Name,
                CssIconClass = category.CssIconClass
            };

            return(categoryServiceModel);
        }
Beispiel #17
0
        public async Task GetAll_ShouldReturnCorrectly()
        {
            var categoryName  = "test";
            var categoryName2 = "test2";

            var category = new Category
            {
                Name = categoryName
            };

            var category2 = new Category
            {
                Name = categoryName2
            };

            await context.Categories.AddAsync(category);

            await context.Categories.AddAsync(category2);

            await context.SaveChangesAsync();

            var categories = service.GetAll().ToList();

            var expectedCategory = new CategoryServiceModel
            {
                Name         = category.Name,
                CssIconClass = category.CssIconClass,
                Id           = category.Id,
            };

            var expectedCategory2 = new CategoryServiceModel
            {
                Name         = category2.Name,
                CssIconClass = category2.CssIconClass,
                Id           = category2.Id,
            };

            var expectedCategoriesCount = 2;
            var actualCategoriesCount   = categories.Count;

            Assert.AreEqual(expectedCategoriesCount, actualCategoriesCount);
            AssertEx.PropertyValuesAreEquals(categories[0], expectedCategory);
            AssertEx.PropertyValuesAreEquals(categories[1], expectedCategory2);
        }
        public async Task <CategoryServiceModel> EditCategoryAsync(CategoryServiceModel categoryServiceModel)
        {
            var category = this.context.Categories
                           .FirstOrDefault(x => x.Id == categoryServiceModel.Id);

            if (category == null)
            {
                throw new ArgumentNullException(nameof(category));
            }

            category.Name           = categoryServiceModel.Name;
            category.MainCategoryId = categoryServiceModel.MainCategoryId;

            this.context.Categories.Update(category);

            await this.context.SaveChangesAsync();

            return(categoryServiceModel);
        }
        public async Task EditAsync_WithNonExistentId_ShouldThrowArgumentNullException()
        {
            // Arrange
            MapperInitializer.InitializeMapper();
            var context              = ApplicationDbContextInMemoryFactory.InitializeContext();
            var categoryRepository   = new EfDeletableEntityRepository <Category>(context);
            var categoryService      = new CategoryService(categoryRepository);
            var categoryServiceModel = new CategoryServiceModel
            {
                Id    = 10000,
                Title = "NonExistent",
            };

            // Act

            // Assert
            await Assert.ThrowsAsync <ArgumentNullException>(async() =>
            {
                await categoryService.EditAsync(categoryServiceModel);
            });
        }
Beispiel #20
0
        public async Task <bool> CreateAsync(CategoryServiceModel categoryServiceModel)
        {
            var category = new Category
            {
                Name         = categoryServiceModel.Name,
                CssIconClass = categoryServiceModel.CssIconClass
            };

            if (string.IsNullOrEmpty(category.Name) ||
                string.IsNullOrWhiteSpace(category.Name))
            {
                throw new ArgumentNullException(NullOrEmptyNameErrorMessage);
            }

            await context.Categories.AddAsync(category);

            var result = await context.SaveChangesAsync();

            var categories = context.Categories.ToList();

            return(result > 0);
        }
        public async Task UpdateAsyncWorksCorrectly()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString()).Options;
            var dbContext = new ApplicationDbContext(options);

            var repository = new EfDeletableEntityRepository <Category>(dbContext);

            var service  = new CategoriesService(repository);
            var category = new Category()
            {
                Id               = 1,
                Name             = "1",
                ParentCategoryId = null,
                Description      = "1",
                ImageUrl         = "1",
            };

            dbContext.Add(category);
            await dbContext.SaveChangesAsync();

            var editedCategory = new CategoryServiceModel()
            {
                Id               = 1,
                Name             = "2",
                ParentCategoryId = 1,
                Description      = "2",
                ImageUrl         = "2",
            };

            await service.UpdateAsync(editedCategory);

            var result = dbContext.Categories.Find(1);

            Assert.Equal(editedCategory.Name, result.Name);
            Assert.Equal(editedCategory.ParentCategoryId, result.ParentCategoryId);
            Assert.Equal(editedCategory.Description, result.Description);
            Assert.Equal(editedCategory.ImageUrl, result.ImageUrl);
        }
Beispiel #22
0
        public async Task GetByNameAsync_WithValidName_ShouldReturnCorrectly()
        {
            var categoryName = "test";

            var category = new Category
            {
                Name = categoryName
            };

            await context.Categories.AddAsync(category);

            await context.SaveChangesAsync();

            var actualCategory = await service.GetByNameAsync(category.Name);

            var expectedCategory = new CategoryServiceModel
            {
                Name         = category.Name,
                CssIconClass = category.CssIconClass,
                Id           = category.Id,
            };

            AssertEx.PropertyValuesAreEquals(actualCategory, expectedCategory);
        }
Beispiel #23
0
 public async Task <IActionResult> Put([FromBody] CategoryServiceModel model) =>
 (await _categoryService.UpdateByModel(model))
 .Match(Ok, Error);