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); }
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); }
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); }
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(); }
private Brand FindBrandByName(CreateBrandBindingModel model) { var brand = this.dbContext.Brands .FirstOrDefault(b => b.Name == model.Name); return(brand); }
private Brand CreateBrandByName(CreateBrandBindingModel model) { var brand = new Brand { Name = model.Name }; return(brand); }
public async Task CreateBrandAsync(CreateBrandBindingModel model) { await _db.Brands .AddAsync(new Brand() { Name = model.Name }); await _db.SaveChangesAsync(); }
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")); }
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")); }
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); }
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)); }
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")); }
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); }
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); }
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); }