Skip to content

Commit

Permalink
Added get meals advanced linq query, with grouping which with the cur…
Browse files Browse the repository at this point in the history
…rent EF Core version still fails with --- System.InvalidOperationException: The LINQ expression '(GroupByShaperExpression: ...
  • Loading branch information
hidegh committed Dec 30, 2019
1 parent 7ccbe11 commit a85ac8e
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 5 deletions.
33 changes: 33 additions & 0 deletions R-Meals Test (Public).postman_collection.json
Original file line number Diff line number Diff line change
Expand Up @@ -774,6 +774,39 @@
}
},
"response": []
},
{
"name": "ADV Get saul's all meals",
"request": {
"method": "GET",
"header": [
{
"key": "Content-Type",
"name": "Content-Type",
"type": "text",
"value": "application/json"
},
{
"key": "Authorization",
"type": "text",
"value": "Bearer {{netAppApi_token}}"
}
],
"url": {
"raw": "https://{{netAppApi_domain}}/Users/4/meals/advanced",
"protocol": "https",
"host": [
"{{netAppApi_domain}}"
],
"path": [
"Users",
"4",
"meals",
"advanced"
]
}
},
"response": []
}
],
"protocolProfileBehavior": {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ orderby m.Date ascending
// In memory grouping due EF core limitations
var mealsDailySummary = mealsDailySummaryQuery.ToList();
var meals = mealsTimeFilteredQuery.ToList();

var groupQuery =
from ms in mealsDailySummary
select new MealDailySummaryDto()
Expand All @@ -243,15 +243,15 @@ from ms in mealsDailySummary
MealsCount = ms.MealsCount,
DailyCaloriesConsumed = ms.DailyCaloriesConsumed,
DailyCaloriesExceeded = ms.DailyCaloriesExceeded,
Meals =
from m in meals
Meals =
from m in meals
where m.Date.Date == ms.Day
select new MealDailySummaryDto.MealDailyItemDto()
select new MealDailySummaryDto.MealDailyItemDto()
{
Id = m.Id,
Date = m.Date,
Calories = m.Calories,
Description = m.Description
Description = m.Description
}
};

Expand All @@ -263,6 +263,42 @@ from m in meals
var result = groupQuery; // .ToList();
return Ok(result);
}

/// <summary>
/// Gets a list of mails (with possible filtering).
/// </summary>
/// <param name="userId"></param>
/// <returns></returns>
[HttpGet]
[Route("advanced")]
[ODataQueryableAttribute]
public async Task<ActionResult<IEnumerable<MealDailySummaryDto>>> GetMealsAdvanced(
long userId)
{
var user = dbContext.Set<User>().Include(u => u.Profile).First(u => u.Id == userId);

var mealsDailySummaryQuery =
from u in dbContext.Set<User>()
from m in u.Meals
group m by m.Date.Date into g
select new MealDailySummaryDto()
{
Day = g.Key,
MealsCount = g.Count(),
DailyCaloriesConsumed = g.Sum(m => m.Calories),
DailyCaloriesExceeded = g.Sum(m => m.Calories) > user.Profile.AllowedCalories,
Meals = g
.Select(m => new MealDailySummaryDto.MealDailyItemDto()
{
Id = m.Id,
Date = m.Date,
Calories = m.Calories,
Description = m.Description
})
};

return Ok(mealsDailySummaryQuery);
}
}
}

Expand Down

0 comments on commit a85ac8e

Please sign in to comment.