public async Task <IActionResult> GetOneRandomly(string variant, string trainingSessionId = null) { variant = Utilities.NormalizeVariantNameCapitalization(variant); List <int> toBeExcluded; double nearRating = randomProvider.RandomRating(); int? userId = await loginHandler.LoggedInUserIdAsync(HttpContext); if (userId.HasValue) { User u = await userRepository.FindByIdAsync(userId.Value); toBeExcluded = u.SolvedPuzzles; if (variant != "Mixed") { nearRating = u.Ratings[variant].Value; } else { nearRating = u.Ratings.Average(x => x.Value.Value); } } else if (trainingSessionId != null) { toBeExcluded = puzzleTrainingSessionRepository.Get(trainingSessionId)?.PastPuzzleIds ?? new List <int>(); } else { toBeExcluded = new List <int>(); } Puzzle puzzle = await puzzleRepository.GetOneRandomlyAsync(toBeExcluded, variant, userId, nearRating); if (puzzle != null) { return(Json(new { success = true, id = puzzle.ID })); } else { return(Json(new { success = true, allDone = true })); } }