public async Task <IActionResult> RemovePlayer([FromBody] PlayerRemoveRequest request)
        {
            var currentUser = await _userManager.FindByNameAsync(User.Identity.Name);

            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            var league = await _fantasyCriticService.GetLeagueByID(request.LeagueID);

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

            if (league.Value.LeagueManager.UserID != currentUser.UserID)
            {
                return(Unauthorized());
            }

            if (league.Value.LeagueManager.UserID == request.UserID)
            {
                return(BadRequest("Can't remove the league manager."));
            }

            var removeUser = await _userManager.FindByIdAsync(request.UserID.ToString());

            if (removeUser == null)
            {
                return(BadRequest());
            }

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

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

            if (!userIsInLeague)
            {
                return(BadRequest("That user is not in that league."));
            }

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

                if (leagueYear.Value.PlayStatus.PlayStarted)
                {
                    return(BadRequest("You can't remove a player from a league that has already started playing"));
                }
            }

            await _fantasyCriticService.RemovePlayerFromLeague(league.Value, removeUser);

            return(Ok());
        }
Ejemplo n.º 2
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));
        }