public IActionResult Index() { var courses = _courseAccessLayer.GetAllCourses(); if (courses.Count() > 0) { return(Ok(courses)); } else { return(NotFound("No courses were found")); } }
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()); } }