simpleGetMethod

mail@pastecode.io avatar
unknown
csharp
2 years ago
957 B
1
Indexable
Never
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;
});