/// <summary> /// Extension метод для округления полей класса NutritionInfo. /// </summary> /// <param name="nutritionInfo">Экземпляр класса NutritionInfo информации о энергетической ценности.</param> /// <param name="decimals">Количество знаков после запятой.</param> public static void Round(this NutritionInfoDTO nutritionInfo, int decimals) { nutritionInfo.Kcal = Helpers.Utilities.RoundAFZ(nutritionInfo.Kcal, 1); nutritionInfo.Carbohydrate = Helpers.Utilities.RoundAFZ(nutritionInfo.Carbohydrate); nutritionInfo.Protein = Helpers.Utilities.RoundAFZ(nutritionInfo.Protein); nutritionInfo.Fat = Helpers.Utilities.RoundAFZ(nutritionInfo.Fat); nutritionInfo.DietaryFiber = Helpers.Utilities.RoundAFZ(nutritionInfo.DietaryFiber, 2); nutritionInfo.Sugars = Helpers.Utilities.RoundAFZ(nutritionInfo.Sugars, 2); nutritionInfo.SaturatedFat = Helpers.Utilities.RoundAFZ(nutritionInfo.SaturatedFat, 2); nutritionInfo.MonounsaturatedFat = Helpers.Utilities.RoundAFZ(nutritionInfo.MonounsaturatedFat, 2); nutritionInfo.PolyunsaturatedFat = Helpers.Utilities.RoundAFZ(nutritionInfo.PolyunsaturatedFat, 2); nutritionInfo.TransFat = Helpers.Utilities.RoundAFZ(nutritionInfo.TransFat, 2); nutritionInfo.Cholesterol = Helpers.Utilities.RoundAFZ(nutritionInfo.Cholesterol, 2); nutritionInfo.Sodium = Helpers.Utilities.RoundAFZ(nutritionInfo.Sodium, 2); nutritionInfo.Potassium = Helpers.Utilities.RoundAFZ(nutritionInfo.Potassium, 2); nutritionInfo.Calcium = Helpers.Utilities.RoundAFZ(nutritionInfo.Calcium, 2); nutritionInfo.Iron = Helpers.Utilities.RoundAFZ(nutritionInfo.Iron, 2); nutritionInfo.VitaminA = Helpers.Utilities.RoundAFZ(nutritionInfo.VitaminA, 2); nutritionInfo.VitaminC = Helpers.Utilities.RoundAFZ(nutritionInfo.VitaminC, 2); }
/// <summary> /// Extension метод для суммирования полей класса NutritionInfo со значениями класса Serving и количества еды. /// </summary> /// <param name="nutritionInfo">Экземпляр класса NutritionInfo информации о энергетической ценности.</param> /// <param name="serving">Экземпляр класса Serving информации о порции еды.</param> /// <param name="foodQty">Количество еды.</param> public static void Sum(this NutritionInfoDTO nutritionInfo, Serving serving, decimal foodQty) { nutritionInfo.Kcal += serving.KCal.HasValue ? (serving.KCal.Value) * foodQty : 0; nutritionInfo.Carbohydrate += (serving.Carbohydrate) * foodQty; nutritionInfo.Protein += (serving.Protein) * foodQty; nutritionInfo.Fat += (serving.Fat) * foodQty; nutritionInfo.DietaryFiber += (serving.DietaryFiber) * foodQty; nutritionInfo.Sugars += (serving.Sugars) * foodQty; nutritionInfo.SaturatedFat += (serving.SaturatedFat) * foodQty; nutritionInfo.MonounsaturatedFat += (serving.MonounsaturatedFat) * foodQty; nutritionInfo.PolyunsaturatedFat += (serving.PolyunsaturatedFat) * foodQty; nutritionInfo.TransFat += (serving.TransFat) * foodQty; nutritionInfo.Cholesterol += (serving.Cholesterol) * foodQty; nutritionInfo.Sodium += (serving.Sodium) * foodQty; nutritionInfo.Potassium += (serving.Potassium) * foodQty; nutritionInfo.Calcium += (serving.Calcium) * foodQty; nutritionInfo.Iron += (serving.Iron) * foodQty; nutritionInfo.VitaminA += (serving.VitaminA) * foodQty; nutritionInfo.VitaminC += (serving.VitaminC) * foodQty; }
public HttpResponseMessage GetNutritionInfo(long date) { // Получаем смещение времени пользователя int timezoneOffset = RequestHelper.GetTimezoneOffset(Request); var zonedDateTime = TimeHelper.ConvertFromUtc(TimeHelper.FromUnixMsToDateTime(date), timezoneOffset); var zonedDate = zonedDateTime.Date; var foodDiaryEntries = unitOfWork.FoodDiaryEntryRepository.Get( f => (f.CreateUserId == currentUserId && DbFunctions.DiffDays( // Выбрасываем время, оставляем только дату DbFunctions.TruncateTime( // Добавляем смещение временной зоны пользователя, получаем время из БД в часовом формате пользователя DbFunctions.AddMinutes(f.Date, timezoneOffset) ), zonedDate) == 0), null, iF => iF.Food, iS => iS.Food.Servings ); var consumedNInfo = new NutritionInfoDTO(); var plannedNInfo = new NutritionInfoDTO(); int plannedMealsCount = 0; int consumedMealsCount = 0; if (foodDiaryEntries != null && foodDiaryEntries.Count() > 0) { foreach (var entry in foodDiaryEntries) { var serving = entry.Food.Servings.Where(s => (s.ServingId == entry.FtSelectedServingId && entry.FsSelectedServingId == 0) || (s.FSServingId == entry.FsSelectedServingId && entry.FtSelectedServingId == 0) || (s.ServingId == entry.FtSelectedServingId && entry.FsSelectedServingId != 0 && entry.FtSelectedServingId != 0) ).SingleOrDefault(); if (serving != null) { if (entry.IsCompleted == true) { // Суммируем значения энергетической ценности уже потребленной еды. consumedNInfo.Sum(serving, entry.FoodQty); consumedMealsCount++; } else { // Суммируем значения энергетической ценности запланированной к потреблению еды. plannedNInfo.Sum(serving, entry.FoodQty); plannedMealsCount++; } } } #region __Округляем значения__ // Consumed info. consumedNInfo.Round(2); // Planned info. plannedNInfo.Round(2); #endregion } return(Request.CreateResponse( HttpStatusCode.OK, new { consumedNInfo = consumedNInfo, plannedNInfo = plannedNInfo, plannedMealsCount = plannedMealsCount, consumedMealsCount = consumedMealsCount })); }