public async Task <IActionResult> Create(ExerciseAdminCreateViewModel exercise) { await this.workOutsService.CreateExerciseAsync( exercise.Name, exercise.Instructions, exercise.ExerciseComplexity); this.TempData["CreateExercise"] = $"You have successfully created {exercise.Name}!"; return(this.RedirectToAction("Index")); }
public async Task <IActionResult> Create(ExerciseAdminCreateViewModel inputModel) { if (!this.ModelState.IsValid) { return(this.View(inputModel)); } await this.exercisesService.CreateExerciseAsync(inputModel); return(this.RedirectToAction("All")); }
public IActionResult Create() { var categories = this.categoriesService.GetAllProgramCategories(); var categoryCreateViewMOdel = new ExerciseAdminCreateViewModel { Categories = categories, }; return(this.View(categoryCreateViewMOdel)); }
public async Task CreateExerciseAsyncShouldCreateExerciseSuccessfully() { 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 exerciseModel = new ExerciseAdminCreateViewModel { Name = ExerciseName, Description = ExerciseDescription, VideoUrl = ExerciseVideoUrl, ProgramCategoryId = programCategory.Id, }; await exercisesService.CreateExerciseAsync(exerciseModel); var actual = await dbContext.Exercises.FirstOrDefaultAsync(e => e.Name == exerciseModel.Name); Assert.Equal(actual.Name, exerciseModel.Name); }
public async Task CreateExerciseAsyncShouldThrowExptionIfProgramCategoryIsNull() { 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 exerciseModel = new ExerciseAdminCreateViewModel { Name = ExerciseName, Description = ExerciseDescription, VideoUrl = ExerciseVideoUrl, ProgramCategoryId = ProgramCategoryId, }; var exception = await Assert.ThrowsAsync <ArgumentNullException>(async() => await exercisesService.CreateExerciseAsync(exerciseModel)); Assert.IsType <ArgumentNullException>(exception); }
public async Task CreateExerciseAsync(ExerciseAdminCreateViewModel inputModel) { if (!this.dbContext.ProgramCategories.Any(pc => pc.Id == inputModel.ProgramCategoryId)) { throw new ArgumentNullException(string.Format(ServicesConstants.InvalidProgramCategoryId, inputModel.ProgramCategoryId)); } var exercise = new Exercise { Name = inputModel.Name, Description = inputModel.Description, VideoUrl = inputModel.VideoUrl, ProgramCategoryId = inputModel.ProgramCategoryId, }; await this.dbContext.Exercises.AddAsync(exercise); await this.dbContext.SaveChangesAsync(); }