public async Task <IActionResult> Edit(ImageAdminEditViewModel inputModel) { if (!this.ModelState.IsValid) { return(this.View(inputModel)); } await this.imagesService.EditImageAsync(inputModel); return(this.RedirectToAction("All")); }
public async Task EditImageAsyncShouldEditImageSuccessfully() { var options = new DbContextOptionsBuilder <MyCalisthenicAppDbContext>() .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString()) .Options; var dbContext = new MyCalisthenicAppDbContext(options); IHttpContextAccessor httpContextAccessor = new HttpContextAccessor(); var usersService = new UsersService(httpContextAccessor, dbContext, null); var mockMapper = new MapperConfiguration(cfg => { cfg.AddProfile(new MyCalisthenicAppProfile()); }); var mapper = mockMapper.CreateMapper(); var categoriesService = new CategoriesService(dbContext, mapper); var postsService = new PostsService(dbContext, mapper, usersService, categoriesService); var productsService = new ProductsService(dbContext, mapper, usersService, categoriesService); var programsService = new ProgramsService(dbContext, mapper, usersService, categoriesService); var exercisesService = new ExercisesService(dbContext, mapper, usersService, categoriesService); var imagesService = new ImagesService(dbContext, mapper, postsService, exercisesService, productsService, programsService); var image = new Image { Id = ImageId, Url = ImageUrl, }; await dbContext.Images.AddAsync(image); await dbContext.SaveChangesAsync(); var imageModel = new ImageAdminEditViewModel { Id = ImageId, Url = ImageEditUrl, }; await imagesService.EditImageAsync(imageModel); var actual = dbContext.Images.FirstOrDefaultAsync(i => i.Id == image.Id); Assert.Equal(imageModel.Url, actual.Result.Url); }
public async Task EditImageAsync(ImageAdminEditViewModel inputModel) { if (inputModel.PostId != null && !this.dbContext.Post.Any(p => p.Id == inputModel.PostId)) { throw new ArgumentNullException(string.Format(ServicesConstants.InvalidPostId, inputModel.PostId)); } if (inputModel.ProductId != null && !this.dbContext.Products.Any(p => p.Id == inputModel.ProductId)) { throw new ArgumentNullException(string.Format(ServicesConstants.InvalidProductId, inputModel.ProductId)); } if (inputModel.ProgramId != null && !this.dbContext.Programs.Any(p => p.Id == inputModel.ProgramId)) { throw new ArgumentNullException(string.Format(ServicesConstants.InvalidProgramId, inputModel.ProgramId)); } if (inputModel.ExerciseId != null && !this.dbContext.Exercises.Any(p => p.Id == inputModel.ExerciseId)) { throw new ArgumentNullException(string.Format(ServicesConstants.InvalidExerciseId, inputModel.ExerciseId)); } var image = await this.dbContext.Images .FirstOrDefaultAsync(i => i.Id == inputModel.Id); if (image == null) { throw new ArgumentNullException(string.Format(ServicesConstants.InvalidImage)); } image.IsDeleted = inputModel.IsDeleted; image.DeletedOn = inputModel.DeletedOn; image.CreatedOn = inputModel.CreatedOn; image.ModifiedOn = inputModel.ModifiedOn; image.Url = inputModel.Url; image.PostId = inputModel.PostId; image.ProductId = inputModel.ProductId; image.ProgramId = inputModel.ProgramId; image.ExerciseId = inputModel.ExerciseId; this.dbContext.Update(image); await this.dbContext.SaveChangesAsync(); }
public async Task EditImageAsyncShouldThrowExceptionIfProgramIdIsIncorrect() { var options = new DbContextOptionsBuilder <MyCalisthenicAppDbContext>() .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString()) .Options; var dbContext = new MyCalisthenicAppDbContext(options); IHttpContextAccessor httpContextAccessor = new HttpContextAccessor(); var usersService = new UsersService(httpContextAccessor, dbContext, null); var mockMapper = new MapperConfiguration(cfg => { cfg.AddProfile(new MyCalisthenicAppProfile()); }); var mapper = mockMapper.CreateMapper(); var categoriesService = new CategoriesService(dbContext, mapper); var postsService = new PostsService(dbContext, mapper, usersService, categoriesService); var productsService = new ProductsService(dbContext, mapper, usersService, categoriesService); var programsService = new ProgramsService(dbContext, mapper, usersService, categoriesService); var exercisesService = new ExercisesService(dbContext, mapper, usersService, categoriesService); var imagesService = new ImagesService(dbContext, mapper, postsService, exercisesService, productsService, programsService); var image = new Image { Id = ImageId, Url = ImageUrl, }; await dbContext.Images.AddAsync(image); await dbContext.SaveChangesAsync(); var imageModel = new ImageAdminEditViewModel { Url = ImageUrl, ProgramId = ProgramId, }; var exception = await Assert.ThrowsAsync <ArgumentNullException>(async() => await imagesService.EditImageAsync(imageModel)); Assert.IsType <ArgumentNullException>(exception); }