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 <AthleteDto> GetProductsAsync(int userId, DateTime?date) { if (date is null) { date = DateTime.Today; } var athlete = await _athleteRepository.FindByCondition(condition : a => a.UserId == userId, include : source => source.Include(a => a.AthleteProducts.Where(ap => ap.DateCreated.Date == date)) .ThenInclude(ap => ap.Product) .ThenInclude(p => p.CategoryOfProduct)); return(_mapper.Map <AthleteDto>(athlete)); }
public async Task AddAsync(int userId, int productId, double weight) { var athlete = await _athleteRepository.FindByCondition(condition : a => a.UserId == userId, include : source => source.Include(a => a.AthleteProducts.Where(ap => ap.ProductId == productId))); if (athlete is null) { throw new AthleteNotFoundException(userId); } var product = await _productRepository.GetOrFailAsync(productId); athlete.AthleteProducts.Add(AthleteProduct.Create(athlete, product, weight)); await UpdateDietStats(athlete, userId, product, weight, '+', DateTime.Today); }
public async Task <AthleteDto> GetDietStatsAsync(int userId, DateTime?date) { if (date is null) { date = DateTime.Today; } var start = date; var end = date?.AddDays(1); var athlete = await _athleteRepository.FindByCondition(a => a.UserId == userId, include : source => source.Include(a => a.AthleteDietStats .Where(ads => ads.DateCreated >= start && ads.DateCreated <= end)).ThenInclude(ads => ads.DietStat)); return(_mapper.Map <AthleteDto>(athlete)); }
public async Task AddAsync_ShouldAddProduct_WhenAthleteExistsAndProductExistsAndWeightIsValid(int userId, int productId, double weight) { var product = _fixture.Build <Product>().With(p => p.Id, productId).Create(); var athlete = _fixture.Build <Athlete>() .With(a => a.UserId, userId) .Without(a => a.AthleteExercises) .Create(); _athleteRepository.FindByCondition(Arg.Any <Expression <Func <Athlete, bool> > >(), Arg.Any <Func <IQueryable <Athlete>, IIncludableQueryable <Athlete, object> > >()).Returns(athlete); _productRepository.GetAsync(productId).Returns(product); await _sut.AddAsync(userId, productId, weight); await _athleteRepository.Received(1).UpdateAsync(Arg.Is(athlete)); }
public async Task GetProductsAsync_ShouldReturnAthleteAndAllProductsAdded(int athleteId) { var athlete = _fixture.Build <Athlete>() .With(a => a.Id, athleteId) .Without(a => a.AthleteExercises) .Create(); _athleteRepository.FindByCondition(Arg.Any <Expression <Func <Athlete, bool> > >(), Arg.Any <Func <IQueryable <Athlete>, IIncludableQueryable <Athlete, object> > >()).Returns(athlete); var dto = await _sut.GetProductsAsync(athleteId, DateTime.Today); dto.ShouldNotBeNull(); dto.Id.ShouldBe(athleteId); dto.Products.Count.ShouldBe(athlete.AthleteProducts.Count); var athleteProduct = athlete.AthleteProducts.FirstOrDefault(); dto.Products[0].Weight.ShouldBe(athleteProduct.Weight); }
public async Task GetDietStatsAsync_ShouldReturnAthleteDietStats(int userId) { var dietStat = _fixture.Create <DietStat>(); var athleteDietStats = new List <AthleteDietStats>() { new AthleteDietStats { Id = 1, DateCreated = DateTime.Now, DateUpdated = DateTime.Now, UserId = userId, DietStat = dietStat } }; var athlete = _fixture.Build <Athlete>() .With(a => a.UserId, userId) .With(a => a.AthleteDietStats, athleteDietStats) .Without(a => a.AthleteExercises) .Create(); _athleteRepository.FindByCondition(Arg.Any <Expression <Func <Athlete, bool> > >(), Arg.Any <Func <IQueryable <Athlete>, IIncludableQueryable <Athlete, object> > >()).ReturnsForAnyArgs(athlete); var dto = await _sut.GetDietStatsAsync(athlete.UserId, new DateTime()); dto.ShouldNotBeNull(); dto.UserId.ShouldBe(athlete.UserId); dto.DietStats.Count.ShouldBe(1); dto.DietStats.First().DietStat.TotalCalories = dietStat.TotalCalories; dto.DietStats.First().DietStat.TotalProteins = dietStat.TotalProteins; dto.DietStats.First().DietStat.TotalCarbohydrates = dietStat.TotalCarbohydrates; dto.DietStats.First().DietStat.TotalFats = dietStat.TotalFats; }
public async Task AddAsync_ShouldAddExercise_WhenAthleteExistsAndExerciseExistsAndDataIsValid(int userId, int exerciseId, double weight, int numberOfSets, int numberOfReps, string dayName) { var athlete = _fixture.Build <Athlete>() .With(a => a.UserId, userId) .With(a => a.AthleteExercises, new List <AthleteExercise>()) .With(a => a.AthleteProducts, new List <AthleteProduct>()) .Create(); var exercise = _fixture.Build <Exercise>() .With(e => e.Id, exerciseId) .Create(); _athleteRepository.FindByCondition(Arg.Any <Expression <Func <Athlete, bool> > >(), Arg.Any <Func <IQueryable <Athlete>, IIncludableQueryable <Athlete, object> > >()).Returns(athlete); _exerciseRepository.GetAsync(exerciseId).Returns(exercise); await _sut.AddAsync(userId, exerciseId, weight, numberOfSets, numberOfReps, dayName); await _athleteRepository.Received(1).UpdateAsync(athlete); }