public async Task EditExerciseAsyncShouldEditExerciseSuccessfully() { 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 exercisesService = new ExercisesService(dbContext, mapper, usersService, categoriesService); var programCategory = new ProgramCategory { Name = CategoryName, Description = CategoryDescription, }; await dbContext.ProgramCategories.AddAsync(programCategory); await dbContext.SaveChangesAsync(); var exrecise = new Exercise { Id = ExerciseId, Name = ExerciseName, Description = ExerciseDescription, VideoUrl = ExerciseVideoUrl, ProgramCategoryId = programCategory.Id, }; await dbContext.Exercises.AddAsync(exrecise); await dbContext.SaveChangesAsync(); var exerciseModel = new ExerciseAdminEditViewModel { Id = ExerciseId, Name = ExerciseName, Description = ExerciseEditDescription, VideoUrl = ExerciseVideoUrl, ProgramCategoryId = programCategory.Id, }; await exercisesService.EditExerciseAsync(exerciseModel); Assert.Equal(exrecise.Description, exerciseModel.Description); }
public async Task <IActionResult> Edit(ExerciseAdminEditViewModel inputModel) { if (!this.ModelState.IsValid) { return(this.View(inputModel)); } await this.exercisesService.EditExerciseAsync(inputModel); return(this.RedirectToAction("All")); }
public async Task EditExerciseAsync(ExerciseAdminEditViewModel inputModel) { if (!this.dbContext.ProgramCategories.Any(pc => pc.Id == inputModel.ProgramCategoryId)) { throw new ArgumentNullException(string.Format(ServicesConstants.InvalidProgramCategoryId, inputModel.ProgramCategoryId)); } var exercise = await this.dbContext.Exercises .Include(p => p.ProgramCategory) .FirstOrDefaultAsync(p => p.Id == inputModel.Id); if (exercise == null) { throw new ArgumentNullException(string.Format(ServicesConstants.InvalidExerciseId, inputModel.Id)); } exercise.IsDeleted = inputModel.IsDeleted; exercise.DeletedOn = inputModel.DeletedOn; exercise.CreatedOn = inputModel.CreatedOn; exercise.ModifiedOn = inputModel.ModifiedOn; exercise.Name = inputModel.Name; exercise.Description = inputModel.Description; exercise.VideoUrl = inputModel.VideoUrl; exercise.ProgramCategoryId = inputModel.ProgramCategoryId; exercise.ProgramCategory.Name = inputModel.ProgramCategoryName; exercise.ProgramCategory.Description = inputModel.ProgramCategoryDescription; exercise.ProgramCategory.IsDeleted = inputModel.ProgramCategoryIsDeleted; exercise.ProgramCategory.Rating = inputModel.ProgramCategoryRating; this.dbContext.Update(exercise); await this.dbContext.SaveChangesAsync(); }
public async Task EditExerciseAsyncShouldThrowExptionIfExerciseIsNull() { 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 exercisesService = new ExercisesService(dbContext, mapper, usersService, categoriesService); var exerciseModel = new ExerciseAdminEditViewModel { Id = ExerciseId, Name = ExerciseName, Description = ExerciseDescription, VideoUrl = ExerciseVideoUrl, ProgramCategoryId = ProgramCategoryId, }; var exception = await Assert.ThrowsAsync <ArgumentNullException>(async() => await exercisesService.EditExerciseAsync(exerciseModel)); Assert.IsType <ArgumentNullException>(exception); }