示例#1
0
        public async Task <IActionResult> GetLeague(Guid id)
        {
            Maybe <League> league = await _fantasyCriticService.GetLeagueByID(id);

            if (league.HasNoValue)
            {
                return(NotFound());
            }

            var currentUser = await _userManager.FindByNameAsync(User.Identity.Name);

            var playersInLeague = await _fantasyCriticService.GetUsersInLeague(league.Value);

            bool userIsInLeague = playersInLeague.Any(x => x.UserID == currentUser.UserID);

            var inviteesToLeague = await _fantasyCriticService.GetOutstandingInvitees(league.Value);

            bool userIsInvitedToLeague = inviteesToLeague.Any(x => x == currentUser.EmailAddress);

            if (!userIsInLeague && !userIsInvitedToLeague)
            {
                return(Unauthorized());
            }

            bool neverStarted = true;

            foreach (var year in league.Value.Years)
            {
                var leagueYear = await _fantasyCriticService.GetLeagueYear(league.Value.LeagueID, year);

                if (leagueYear.Value.PlayStatus.PlayStarted)
                {
                    neverStarted = false;
                }
            }

            bool isManager       = (league.Value.LeagueManager.UserID == currentUser.UserID);
            var  leagueViewModel = new LeagueViewModel(league.Value, isManager, playersInLeague, inviteesToLeague,
                                                       userIsInvitedToLeague, neverStarted);

            return(Ok(leagueViewModel));
        }