public async Task AddAsync(int userId, int exerciseId, double weight, int numberOfSets, int numberOfReps, string dayName) { var athlete = await _athleteRepository.FindByCondition(condition : a => a.UserId == userId, include : source => source.Include(a => a.AthleteExercises) .ThenInclude(ae => ae.Day) .Include(a => a.AthleteExercises.Where(ae => ae.ExerciseId == exerciseId)) .ThenInclude(ae => ae.Exercise) .ThenInclude(e => e.PartOfBody)); if (athlete is null) { throw new AthleteNotFoundException(userId); } if (athlete.AthleteExercises.Any(ae => ae.ExerciseId == exerciseId && ae.DateUpdated.ToShortDateString() == DateTime.Today.ToShortDateString())) { throw new AlreadyAddedTodayException(nameof(Exercise), exerciseId); } var exercise = await _exerciseRepository.GetOrFailAsync(exerciseId); athlete.AthleteExercises.Add(AthleteExercise.Create(athlete, exercise, weight, numberOfSets, numberOfReps, Day.GetDay(dayName))); await _athleteRepository.UpdateAsync(athlete); }
public async Task RemoveExerciseAsync(Athlete athlete, AthleteExercise exercise) { _context.Athletes.Attach(athlete); athlete.AthleteExercises.Remove(exercise); await _context.SaveChangesAsync(); }