public void CadAddNull_ShouldThrowArgumentNullException() { using var context = new DataContext(ContextOptions); var exerciseRepo = new ExerciseRepository(context); Assert.Throws <ArgumentNullException>(() => exerciseRepo.Add(null)); }
public void Add(Exercise exercise) { var userId = new Guid(User.Identity.GetUserId()); exercise.UserId = userId; ExerciseRepository.Add(exercise); }
private async Task AddExercise(DataContext context) { var repo = new ExerciseRepository(context); var exercise = Exercise.Create("Exercise 1", await GetUser(context)); repo.Add(exercise); await repo.SaveAll(); }
public async Task <ActionResult <ExerciseForReturnDto> > CreateExercise([FromBody] ExerciseForCreationDto exercise) { var newExercise = mapper.Map <Exercise>(exercise); if (exercise.PrimaryMuscleId != null) { var primaryMuscle = await muscleRepository.GetByIdAsync(exercise.PrimaryMuscleId.Value); newExercise.PrimaryMuscle = primaryMuscle; } if (exercise.SecondaryMuscleId != null) { var secondaryMuscle = await muscleRepository.GetByIdAsync(exercise.SecondaryMuscleId.Value); newExercise.SecondaryMuscle = secondaryMuscle; } if (exercise.EquipmentIds != null) { newExercise.Equipment = exercise.EquipmentIds.Select(eqId => new EquipmentExercise { EquipmentId = eqId }).ToList(); } if (exercise.ExerciseCategoryIds != null) { newExercise.ExerciseCategorys = exercise.ExerciseCategoryIds.Select(ecId => new ExerciseCategoryExercise { ExerciseCategoryId = ecId }).ToList(); } exerciseRepository.Add(newExercise); var saveResult = await exerciseRepository.SaveAllAsync(); if (!saveResult) { return(BadRequest(new ProblemDetailsWithErrors("Unable to create exercise.", 400, Request))); } var exerciseToReturn = mapper.Map <ExerciseForReturnDto>(newExercise); return(CreatedAtRoute("GetExercise", new { id = newExercise.Id }, exerciseToReturn)); }
public void Should_add_new_exercise(User user) { var sut = new ExerciseRepository(_dbContext); var exercise = new Exercise { Date = DateTime.Now.AddHours(1), Description = "Do 100 pushups", Duration = TimeSpan.FromMinutes(50), UserId = user.Id, Id = Guid.NewGuid() }; // add and save to db sut.Add(exercise); sut.UnitOfWork.SaveChangesAsync().GetAwaiter().GetResult(); // get the result from the DB var savedExercise = _dbContext.Exercises.SingleOrDefault(e => e.Id == exercise.Id); Assert.NotNull(savedExercise); Assert.Equal(exercise, savedExercise, new EntityComparer <Exercise>()); }