public async Task <string> EditAsync <T>(EditAccessorieViewModel editModel) { var accessories = this.accessoriesRepository.All().Where(f => f.Id == editModel.Id).FirstOrDefault(); if (accessories == null) { throw new NullReferenceException(string.Format(ExceptionsServices.Null_Accessories_Id_ErrorMessage, editModel.Id)); } var photoUrl = string.Empty; if (editModel.ImagePath == null) { photoUrl = accessories.ImagePath; } else { photoUrl = await this.cloudinaryService.UploadPhotoAsync( editModel.ImagePath, $"{editModel.Name}_{editModel.Id}", GlobalConstants.CloudFolderForAccessoriesPhotos); } var product = this.productService.GetById(accessories.ProductId); product.Name = editModel.Name; accessories.Description = editModel.Description; accessories.ImagePath = photoUrl; // To upload the new image and ad the new await this.accessoriesRepository.SaveChangesAsync(); return(accessories.Id); }
public async Task EditProject_WithCorrectData_ShouldSuccessfullyEdit() { // 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 EditAccessorieViewModel { Id = "abc1", Description = "Some description test 1", ImagePath = file, Name = "Аксесоар 1 сменено", }; var seeder = new DbContextTestsSeeder(); await seeder.SeedUsersAsync(context); await seeder.SeedGroupAsync(context); await seeder.SeedAccessorieAsync(context); // Act AutoMapperConfig.RegisterMappings(typeof(EditAccessorieViewModel).Assembly); var result = await accessorieService.EditAsync <EditAccessorieViewModel>(accessorie); var actual = context.Accessories.FirstOrDefault(x => x.Product.Name == "Аксесоар 1 сменено"); var expectedName = "Аксесоар 1 сменено"; var expectedDescription = "Some description test 1"; // Assert AssertExtension.EqualsWithMessage(expectedName, actual.Product.Name, string.Format(ErrorMessage, "EditAccessorie returns correct Name")); AssertExtension.EqualsWithMessage(expectedDescription, actual.Description, string.Format(ErrorMessage, "EditAccessorie returns correct Description")); }
public async Task <IActionResult> Edit(EditAccessorieViewModel model) { if (!this.ModelState.IsValid) { return(this.View(model)); } var viewModel = await this.accessoriesService.EditAsync <EditAccessorieViewModel>(model); return(this.RedirectToAction("Details", "Accessorie", new { name = model.Name, area = string.Empty })); }
public async Task EditAccessorie_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 EditAccessorieViewModel { Id = "Test Id", Description = "Some description test 1", Name = "Аксесоар 1", ImagePath = file, }; var seeder = new DbContextTestsSeeder(); await seeder.SeedUsersAsync(context); await seeder.SeedGroupAsync(context); await seeder.SeedProjectAsync(context); // Act AutoMapperConfig.RegisterMappings(typeof(EditAccessorieViewModel).Assembly); await Assert.ThrowsAsync <NullReferenceException>(() => accessorieService.EditAsync <EditAccessorieViewModel>(accessorie)); }