public ResponseDto <LeagueTableDto> GetLeagueTable(int leagueId)
        {
            var response = new ResponseDto <LeagueTableDto>()
            {
                Object = new LeagueTableDto()
            };

            var getMatchesrequest = _matchService.GetMatchesFromLeague(leagueId);

            if (getMatchesrequest.ErrorOccurred)
            {
                response.Errors = getMatchesrequest.Errors;
                return(response);
            }

            var matches = getMatchesrequest.Object.Matches;

            var getTeamsrequest = _leagueService.GetLeague(leagueId);

            if (getTeamsrequest.ErrorOccurred)
            {
                response.Errors = getTeamsrequest.Errors;
                return(response);
            }

            var teams = getTeamsrequest.Object.Teams;

            teams.ForEach(team =>
            {
                var teamMatchesHost = matches.Where(m => m.Host.Id == team.Id);
                var teamMatchesAway = matches.Where(m => m.Away.Id == team.Id);

                var scoredGoals = CountScoredGoals(teamMatchesHost, teamMatchesAway);
                var lostGoals   = CountLostGoals(teamMatchesHost, teamMatchesAway);
                var tableRow    = new LeagueTableRowDto
                {
                    TeamId        = team.Id,
                    Name          = team.Name,
                    MatchesWon    = CountWins(teamMatchesHost, teamMatchesAway),
                    MatchesDrawn  = CountDraws(teamMatchesHost, teamMatchesAway),
                    MatchesLost   = CountDefeats(teamMatchesHost, teamMatchesAway),
                    MatchesPlayed = CountWins(teamMatchesHost, teamMatchesAway) + CountDraws(teamMatchesHost, teamMatchesAway)
                                    + CountDefeats(teamMatchesHost, teamMatchesAway),
                    Points      = CountPoints(teamMatchesHost, teamMatchesAway),
                    GoalsScored = scoredGoals,
                    GoalsLost   = lostGoals,
                    GoalsBilans = scoredGoals - lostGoals
                };
                response.Object.Teams.Add(tableRow);
            });

            response.Object.Teams = response.Object.Teams
                                    .OrderByDescending(t => t.Points)
                                    .ThenByDescending(t => t.GoalsBilans)
                                    .ThenBy(t => t.Name).ToList();

            return(response);
        }
示例#2
0
        public IActionResult GetLeague(int leagueId)
        {
            var result = _leagueService.GetLeague(leagueId);

            if (result.ErrorOccurred)
            {
                return(BadRequest(result));
            }

            return(Ok(result));
        }
示例#3
0
        public void AddRemoveLeagueTest()
        {
            LeagueDto dto = new LeagueDto
            {
                LeagueId = TEST_ALTERNATE_ID,
                Name     = TEST_LEAGUE_NAME,
                Password = TEST_LEAGUE_PASSWORD
            };

            var result = LeagueService.AddNew(dto);

            Assert.IsTrue(result.IsSuccess);

            //make sure the league is actually in the db
            var league = LeagueService.GetLeague(TEST_ALTERNATE_ID);

            Assert.IsNotNull(league);
            Assert.AreEqual(TEST_LEAGUE_NAME, league.Name);
            Assert.AreEqual(TEST_LEAGUE_PASSWORD, league.Password);

            //make sure an update works
            dto.Name = TEST_LEAGUE_NAME_2;
            var nameChangeResult = LeagueService.Update(dto);

            Assert.IsTrue(nameChangeResult.IsSuccess);

            league = LeagueService.GetLeague(TEST_ALTERNATE_ID);
            Assert.AreEqual(TEST_LEAGUE_NAME_2, league.Name);

            //cleanup by deleting the record
            var deleteResult = LeagueService.RemoveLeague(TEST_ALTERNATE_ID);

            Assert.IsTrue(deleteResult.IsSuccess);

            //make sure the league is gone
            var deletedLeague = LeagueService.GetLeague(TEST_ALTERNATE_ID);

            Assert.IsNull(deletedLeague);
        }
 public ActionResult GetLeague(int leagueId)
 {
     try
     {
         League league = leagueService.GetLeague(leagueId);
         return(Ok(league));
     }
     catch (BusinessLogicException e)
     {
         return(BadRequest(e.Message));
     }
     catch (PersistentStoreException e)
     {
         Console.WriteLine(e.Message, e.ToString());
         return(StatusCode(500));
     }
     catch (Exception e)
     {
         Console.WriteLine(e.Message, e.ToString());
         return(StatusCode(500));
     }
 }
        public ActionResult View(Guid leagueId, int?week)
        {
            var league = _leagueService.GetLeague(leagueId);

            if (league == null)
            {
                AddError("Error opening league details");
                RedirectToAction("Index");
            }

            if (week == null)
            {
                week = -1;
            }

            var isOwner = _leagueService.IsPlayerALeagueOwner(CurrentPlayerId, league);

            IEnumerable <LeagueTable> leagueRows;

            if (week == -1)
            {
                leagueRows = _leagueService.GetLeagueTable(leagueId);
            }
            else
            {
                leagueRows = _leagueService.GetLeagueTable(leagueId, (int)week);
            }

            var leagueStats = _leagueService.GetLeagueStats(leagueId);

            var model = new ViewSingleLeagueViewModel
            {
                LeagueName      = league.Name,
                LeagueId        = league.Id,
                IsLeagueOwner   = isOwner,
                InviteCode      = league.InviteCode,
                LeagueTableRows = leagueRows.Select(lr => new LeagueTableRow
                {
                    PlayerName     = lr.PlayerName,
                    GameweekPoints = lr.GameweekPoints,
                    TotalPoints    = lr.TotalPoints,
                    PlayerId       = lr.PlayerId,
                    GameWeek       = lr.GameweekNumber
                }).ToList(),
                LeagueStats = leagueStats.ToList(),
                Gameweeks   = _leagueService.GetLeagueGameweeks(league.Id).Select(gw => new SelectListItem
                {
                    Text     = "Week " + gw.Number.ToString(),
                    Value    = gw.Number.ToString(),
                    Selected = (gw.Number == week)
                }).ToList()
            };

            model.Gameweeks.Add(new SelectListItem
            {
                Text     = "Overall",
                Value    = "-1",
                Selected = week == -1
            });
            return(View(model));
        }
示例#6
0
        // GET api/<controller>/5
        public LeagueViewModel Get(int id)
        {
            var league = Map(_leagueService.GetLeague(id));

            return(league);
        }