simpleGetMethod

 avatar
unknown
csharp
3 years ago
957 B
3
Indexable
app.MapGet("topScore", async (StackUnderdoseContext db) =>
{
    var users = await db.Users
        .Include(x => x.Comments)
        .Include(x => x.Answers)
        .Include(x => x.Questions)
        .ToListAsync();

    int topScore = 0;
    Guid topUserId = new Guid();
     

    foreach(var user in users)
    {
        int scoreCount = 0;

        foreach (var question in user.Questions)
        {
            scoreCount += question.Score;
        }
        foreach (var answer in user.Answers)
        {
            scoreCount += answer.Score;
        }
        foreach (var comment in user.Comments)
        {
            scoreCount += comment.Score;
        }

        if (scoreCount > topScore)
        {
            topScore = scoreCount;
            topUserId = user.Id;
        }
    }

    var topUser = await db.Users.FirstAsync(x => x.Id == topUserId);

    var topUserAndScore = new { topUser, topScore };

    return topUserAndScore;
});
Editor is loading...