internal bool UpdateCarbsInLastMeal(string restaurnatId, int menuPartId, int dishId, double totalCarbohydrate)
{
var userProfile = GetUserProfile();
var userData = userProfile.GetUserData();
if (userData.UserMeals == null)
{
log.WarnFormat("[UpdateCarbsInLastMeal] User={0} tried to update postmeal sugar, but meal doesn't exists.", userProfile.UserName);
return false;
}
var userMeal = userData.UserMeals.OrderByDescending(c => c.CreatedAt).ToList().FirstOrDefault();
bool updateFlag = false;
if(restaurnatId == userMeal.RestaurantId)
{
foreach (var portion in userMeal.Portions)
{
if (portion.MenuPartId == menuPartId && portion.PrtionFrom.Id == dishId)
{
portion.CarbAmount = totalCarbohydrate;
updateFlag = true;
}
}
}
if (updateFlag)
{
userProfile.SetUserData(userData);
userProfile.Save();
log.InfoFormat("[UpdateCarbsInLastMeal] user={0}, restaurnatId={1}, menuPartId={2}, dishId={3}, totalCarbohydrate={4}.", userProfile.UserName, restaurnatId, menuPartId, dishId, totalCarbohydrate);
return true;
}
else
{
log.WarnFormat("[UpdateCarbsInLastMeal] Was unsuccessful. Incorrect meal. user={0}, restaurnatId={1}, menuPartId={2}, dishId={3}, totalCarbohydrate={4}.", userProfile.UserName, restaurnatId, menuPartId, dishId, totalCarbohydrate);
return false;
}
}