public IActionResult Index(Guid CourseId) { try { var course = _courseAccessLayer.GetCourse(CourseId); var highestPossibleScore = course.Par + 50; var lowestPossibleScore = course.Par - 10; var players = _playerAccessLayer.GetAllPlayers(); var rounds = _golfRoundAccessLayer.GetAllGolfRounds().Where(r => r.CourseId.Equals(CourseId)); IList <Forecast> forecasts = new List <Forecast>(); foreach (var player in players) { var playerScores = _scoreAccessLayer.GetAllPlayerScores(player.Id); IEnumerable <int> playerScoresForCourse = playerScores.Join(rounds, score => score.GolfRoundId, round => round.Id, (score, round) => score.Value); if (playerScoresForCourse.Count() == 0) { playerScoresForCourse = playerScores.Select(s => s.Value); } var playerForecast = forecast(player, playerScoresForCourse, course, lowestPossibleScore, highestPossibleScore); forecasts.Add(playerForecast); } return(Ok(forecasts)); } catch { return(BadRequest()); } }
public IActionResult Index() { try { var courses = _courseAccessLayer.GetAllCourses(); var players = _playerAccessLayer.GetAllPlayers(); var rounds = _golfRoundAccessLayer.GetAllGolfRounds(); var scores = _scoreAccessLayer.GetAllScores(); var joinRoundScores = from round in rounds join score in scores on round.Id equals score.GolfRoundId where score.GolfRoundId.Equals(round.Id) select new { round.Id, round.Date, round.CourseId, score.PlayerId, score.Value }; var resolveCourses = from round in joinRoundScores join course in courses on round.CourseId equals course.Id where course.Id.Equals(round.CourseId) select new { round.Id, round.Date, course.Name, course.TeeName, course.Holes, course.Par, course.ScratchRating, course.Slope, round.PlayerId, round.Value }; var resolvePlayers = from round in resolveCourses join player in players on round.PlayerId equals player.Id where player.Id.Equals(round.PlayerId) select new { round.Id, round.Date, round.Name, round.TeeName, round.Holes, round.Par, round.ScratchRating, round.Slope, player.FirstName, player.LastName, player.Handicap, round.Value }; Dictionary <Guid, GolfRoundViewModel> roundsforDisplay = new Dictionary <Guid, GolfRoundViewModel>(); foreach (var round in resolvePlayers) { var roundId = round.Id; if (!roundsforDisplay.ContainsKey(roundId)) { roundsforDisplay.Add(roundId, new GolfRoundViewModel { Date = round.Date, Course = new Course { Name = round.Name, TeeName = round.TeeName, Holes = round.Holes, Par = round.Par, ScratchRating = round.ScratchRating, Slope = round.Slope }, Players = new List <PlayerViewModel>() }); } roundsforDisplay[roundId].Players.Add(new PlayerViewModel { Player = new Player { FirstName = round.FirstName, LastName = round.LastName, Handicap = round.Handicap }, Score = round.Value }); } foreach (var roundId in roundsforDisplay.Keys) { roundsforDisplay[roundId].Players = roundsforDisplay[roundId].Players.OrderBy(p => p.Score).ToList(); } return(Ok(roundsforDisplay.Values)); } catch { return(BadRequest()); } }