public async Task DeleteProject_WithCorrectData_ShouldSuccessfullyDelete() { // Arrange var context = ApplicationDbContextInMemoryFactory.InitializeContext(); var groupRepository = new EfDeletableEntityRepository <Product_Group>(context); var productRepository = new EfDeletableEntityRepository <Product>(context); var accessorieRepository = new EfDeletableEntityRepository <Accessorie>(context); var groupService = new GroupService(groupRepository); var prodcutService = new ProductService(productRepository, groupService); var cloudinaryService = new FakeCloudinary(); var accessorieService = new AccessoriesService(accessorieRepository, prodcutService, groupService, cloudinaryService); var user = new ApplicationUser { Id = "abc", FirstName = "Nikolay", LastName = "Doychev", Email = "*****@*****.**", EmailConfirmed = true, }; var fileName = "Img"; IFormFile file = new FormFile( new MemoryStream(Encoding.UTF8.GetBytes("This is a dummy file")), 0, 0, fileName, "dummy.png"); var project = new DeleteAccessorieViewModel { Id = "abc1", Description = "Some description test 1", Name = "Проект 1", ImagePath = fileName, }; var seeder = new DbContextTestsSeeder(); await seeder.SeedUsersAsync(context); await seeder.SeedGroupAsync(context); await seeder.SeedAccessorieAsync(context); // Act AutoMapperConfig.RegisterMappings(typeof(DeleteAccessorieViewModel).Assembly); await accessorieService.DeleteAsync <DeleteAccessorieViewModel>(project); int actual = context.Accessories.Count(); // Assert Assert.True(actual == 1, string.Format(ErrorMessage, "DeleteAccessorie method")); }
public async Task <IActionResult> Delete(DeleteAccessorieViewModel model) { if (!this.ModelState.IsValid) { return(this.View(model)); } await this.accessoriesService.DeleteAsync <DeleteAccessorieViewModel>(model); return(this.RedirectToAction("All", new { area = string.Empty })); }
public async Task DeleteAccessorie_WithNonExistingAccessorie_ShouldThrowException() { // Arrange var context = ApplicationDbContextInMemoryFactory.InitializeContext(); var groupRepository = new EfDeletableEntityRepository <Product_Group>(context); var productRepository = new EfDeletableEntityRepository <Product>(context); var accessorieRepository = new EfDeletableEntityRepository <Accessorie>(context); var groupService = new GroupService(groupRepository); var prodcutService = new ProductService(productRepository, groupService); var cloudinaryService = new FakeCloudinary(); var accessorieService = new AccessoriesService(accessorieRepository, prodcutService, groupService, cloudinaryService); var user = new ApplicationUser { Id = "abc", FirstName = "Nikolay", LastName = "Doychev", Email = "*****@*****.**", EmailConfirmed = true, }; var fileName = "Img"; IFormFile file = new FormFile( new MemoryStream(Encoding.UTF8.GetBytes("This is a dummy file")), 0, 0, fileName, "dummy.png"); var accessorie = new DeleteAccessorieViewModel { Id = "Test Id", Description = "Some description test 1", ImagePath = "Some dummy data", Name = "Проект 1", }; var seeder = new DbContextTestsSeeder(); await seeder.SeedUsersAsync(context); await seeder.SeedGroupAsync(context); await seeder.SeedAccessorieAsync(context); // Act AutoMapperConfig.RegisterMappings(typeof(DeleteAccessorieViewModel).Assembly); await Assert.ThrowsAsync <NullReferenceException>(() => accessorieService.DeleteAsync <DeleteAccessorieViewModel>(accessorie)); }
public async Task DeleteAsync <T>(DeleteAccessorieViewModel deleteModel) { var accessories = this.accessoriesRepository.All().Where(f => f.Id == deleteModel.Id).FirstOrDefault(); if (accessories == null) { throw new NullReferenceException(string.Format(ExceptionsServices.Null_Accessories_Id_ErrorMessage, deleteModel.Id)); } accessories.IsDeleted = true; accessories.DeletedOn = DateTime.UtcNow; await this.productService.DeleteAsync(accessories.ProductId); this.accessoriesRepository.Update(accessories); await this.accessoriesRepository.SaveChangesAsync(); }