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 SetDemandAsync(int userId, double totalCalories, double proteins, double carbohydrates, double fats) { var athlete = await _athleteRepository.GetOrFailAsync(userId); if (athlete.CaloricDemand is not null) { await UpdateDemandAsync(userId, totalCalories, proteins, carbohydrates, fats); return; } athlete.CaloricDemand = CaloricDemand.Create(totalCalories, proteins, carbohydrates, fats); await _athleteRepository.UpdateAsync(athlete); }
private async Task UpdateDietStats(Athlete athlete, int userId, Product product, double weight, char sign, DateTime dateCreated) { athlete.AthleteDietStats.Add(await _athleteDietStatsService.AddDietStatsAsync(userId, product, weight, sign, dateCreated)); await _athleteRepository.UpdateAsync(athlete); }
public async Task DeleteAsync_ShouldDeleteProduct_WhenAthleteExistsAndProductExists(int userId, int productId, int athleteProductId) { var athleteProducts = _fixture.Build <AthleteProduct>() .With(ap => ap.ProductId, productId) .With(ap => ap.AthleteId, userId) .With(ap => ap.Id, athleteProductId) .CreateMany(count: 1); var athlete = _fixture.Build <Athlete>() .With(a => a.UserId, userId) .With(a => a.AthleteProducts, athleteProducts.ToList()) .Without(a => a.AthleteExercises) .Create(); _athleteRepository.FindByCondition(Arg.Any <Expression <Func <Athlete, bool> > >(), Arg.Any <Func <IQueryable <Athlete>, IIncludableQueryable <Athlete, object> > >()).Returns(athlete); await _sut.DeleteAsync(userId, productId, athleteProductId); await _athleteRepository.UpdateAsync(Arg.Is(athlete)); }