Пример #1
0
        public void RemoveBrandShouldSetIsDeletedToTrue()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(databaseName: $"RemoveBrandShouldSetIsDeletedToTrue_Brand_Database")
                          .Options;

            var dbContext = new ApplicationDbContext(options);

            var mapper = this.SetUpAutoMapper();

            var brandService = new BrandsService(dbContext, mapper);

            var brandName  = Guid.NewGuid().ToString();
            var brandName1 = Guid.NewGuid().ToString();
            var model      = new CreateBrandBindingModel {
                Name = brandName
            };
            var model1 = new CreateBrandBindingModel {
                Name = brandName1
            };

            brandService.CreateBrand(model);
            brandService.CreateBrand(model1);
            brandService.RemoveBrand(model);
            var brand1 = brandService.CreateBrand(model);

            Assert.NotNull(brand1);
            Assert.True(brand1.IsDeleted == false);
        }
Пример #2
0
        public void CreateBrandShouldSetDeletedToFalseIfProductAlreadyExistsAndIsDeletedIsTrue()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(databaseName: $"CreateBrandShouldCreateBrand_Brand_Database")
                          .Options;

            var dbContext = new ApplicationDbContext(options);

            var mapper = this.SetUpAutoMapper();

            var brandService = new BrandsService(dbContext, mapper);

            var brandName  = Guid.NewGuid().ToString();
            var brandName1 = Guid.NewGuid().ToString();
            var model      = new CreateBrandBindingModel {
                Name = brandName
            };
            var model1 = new CreateBrandBindingModel {
                Name = brandName1
            };

            brandService.CreateBrand(model);
            brandService.CreateBrand(model1);

            var brandToRemove = dbContext.Brands.FirstOrDefault(x => x.Name == brandName);

            brandToRemove.IsDeleted = true;
            dbContext.Brands.Update(brandToRemove);
            dbContext.SaveChanges();

            var brand1 = brandService.CreateBrand(model);

            Assert.NotNull(brand1);
            Assert.True(brand1.IsDeleted == false);
        }
Пример #3
0
        public void FindAllBrandsShouldReturnOnlyBrandsWhereIsDeletedIsFalse()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(databaseName: $"FindAllBrandsShouldReturnOnlyBrandsWhereIsDeletedIsFalse_Brand_Database")
                          .Options;

            var dbContext = new ApplicationDbContext(options);

            var mapper = this.SetUpAutoMapper();

            var brandService = new BrandsService(dbContext, mapper);

            var brandName  = Guid.NewGuid().ToString();
            var brandName1 = Guid.NewGuid().ToString();
            var brandName2 = Guid.NewGuid().ToString();
            var model      = new CreateBrandBindingModel {
                Name = brandName
            };
            var model1 = new CreateBrandBindingModel {
                Name = brandName1
            };
            var model2 = new CreateBrandBindingModel {
                Name = brandName2
            };

            brandService.CreateBrand(model);
            brandService.CreateBrand(model1);
            brandService.CreateBrand(model2);
            brandService.RemoveBrand(model);
            brandService.RemoveBrand(model1);

            var brands = brandService.FindAllBrands();

            Assert.True(brands.Count == 1);
        }
Пример #4
0
        public BrandDTO CreateBrand(CreateBrandBindingModel model)
        {
            var checkBrand = this.FindBrandByName(model);

            if (checkBrand != null)
            {
                if (checkBrand.IsDeleted == true)
                {
                    checkBrand.IsDeleted = false;
                    this.dbContext.Brands.Update(checkBrand);
                    this.dbContext.SaveChanges();

                    return(this.mapper.Map <BrandDTO>(checkBrand));
                }
                else
                {
                    return(null);
                }
            }
            else
            {
                var brand = this.CreateBrandByName(model);

                this.dbContext.Brands.Add(brand);
                this.dbContext.SaveChanges();

                return(this.mapper.Map <BrandDTO>(brand));
            }
        }
        public async Task CreateAsync(CreateBrandBindingModel model)
        {
            Brand brand = this.Mapper.Map <Brand>(model);
            await Repository.AddAsync(brand);

            await Repository.SaveChangesAsync();
        }
Пример #6
0
        private Brand FindBrandByName(CreateBrandBindingModel model)
        {
            var brand = this.dbContext.Brands
                        .FirstOrDefault(b => b.Name == model.Name);

            return(brand);
        }
Пример #7
0
        private Brand CreateBrandByName(CreateBrandBindingModel model)
        {
            var brand = new Brand {
                Name = model.Name
            };

            return(brand);
        }
Пример #8
0
        public async Task CreateBrandAsync(CreateBrandBindingModel model)
        {
            await _db.Brands
            .AddAsync(new Brand()
            {
                Name = model.Name
            });

            await _db.SaveChangesAsync();
        }
Пример #9
0
        public async Task <IActionResult> CreateBrand(CreateBrandBindingModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View());
            }

            await _adminManager.CreateBrandAsync(model);

            TempData["Success"] = "You successfully created new brand.";
            return(RedirectToAction("CreateBrand"));
        }
Пример #10
0
        public IActionResult Delete(CreateBrandBindingModel model)
        {
            var brand = this.brandService.RemoveBrand(model);

            if (brand == null)
            {
                var creationErrorViewModel = this.errorService.CreateCreateionErrorViewModel(DeletionDoesntExistErrorMessage, HyperLinkForDoesntExistError);
                return(this.RedirectToAction("CreationError", "Error", creationErrorViewModel));
            }

            return(this.Redirect("/Home/Index"));
        }
Пример #11
0
        private Brand FindBrandByModelAndCheckIsDeleted(CreateBrandBindingModel model)
        {
            var brand = this.dbContext.Brands
                        .Where(x => x.IsDeleted == false)
                        .FirstOrDefault(b => b.Name == model.Name);

            if (this.dbContext.Brands.Where(x => x.IsDeleted == false).Count() == 1)
            {
                return(null);
            }

            return(brand);
        }
Пример #12
0
        public BrandDTO RemoveBrand(CreateBrandBindingModel model)
        {
            var brand = this.FindBrandByModelAndCheckIsDeleted(model);

            if (brand == null)
            {
                return(null);
            }

            brand.IsDeleted = true;

            this.dbContext.Update(brand);
            this.dbContext.SaveChanges();

            return(this.mapper.Map <BrandDTO>(brand));
        }
Пример #13
0
        public IActionResult Create(CreateBrandBindingModel model)
        {
            if (!this.ModelState.IsValid)
            {
                return(this.View(model));
            }

            var brand = this.brandService.CreateBrand(model);

            if (brand == null)
            {
                var creationErrorViewModel = this.errorService.CreateCreateionErrorViewModel(CreationAlreadyExistsErrorMessage, HyperLinkForCreationError);
                return(this.RedirectToAction("CreationError", "Error", creationErrorViewModel));
            }

            return(this.Redirect("/Home/Index"));
        }
Пример #14
0
        public void RemoveBrandShouldReturnNullIfBrandIsNull()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(databaseName: $"RRemoveBrandShouldReturnNullIfBrandIsNull_Brand_Database")
                          .Options;

            var dbContext = new ApplicationDbContext(options);

            var mapper = this.SetUpAutoMapper();

            var brandService = new BrandsService(dbContext, mapper);

            var brandName = Guid.NewGuid().ToString();
            var model     = new CreateBrandBindingModel {
                Name = brandName
            };

            var brand = brandService.RemoveBrand(model);

            Assert.Null(brand);
        }
Пример #15
0
        public void CreateBrandShouldCreateBrand()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(databaseName: $"CreateBrandShouldCreateBrand_Brand_Database")
                          .Options;

            var dbContext = new ApplicationDbContext(options);

            var mapper = this.SetUpAutoMapper();

            var brandService = new BrandsService(dbContext, mapper);

            var brandName = Guid.NewGuid().ToString().Substring(0, 15);
            var model     = new CreateBrandBindingModel {
                Name = brandName
            };

            var brand = brandService.CreateBrand(model);

            Assert.True(dbContext.Brands.Count() == 1);
            Assert.True(brand.Name == brandName);
        }
Пример #16
0
        public void CreateBrandShouldntCreateBrandAndShouldReturnNullIfAlreadyExists()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(databaseName: $"CreateBrandShouldntCreateBrandAndShouldReturnNullIfAlreadyExists_Brand_Database")
                          .Options;

            var dbContext = new ApplicationDbContext(options);

            var mapper = this.SetUpAutoMapper();

            var brandService = new BrandsService(dbContext, mapper);

            var brandName = Guid.NewGuid().ToString();
            var model     = new CreateBrandBindingModel {
                Name = brandName
            };

            brandService.CreateBrand(model);
            var brand1 = brandService.CreateBrand(model);

            Assert.True(dbContext.Brands.Count() == 1);
            Assert.Null(brand1);
        }