コード例 #1
0
    protected async Task <GenericResultRecord <LeagueRecord> > GetExistingLeague(Guid leagueID, RequiredRelationship requiredRelationship)
    {
        var currentUserRecord = await GetCurrentUser();

        if ((requiredRelationship.MustBeLoggedIn || requiredRelationship.MustBeInOrInvitedToLeague || requiredRelationship.MustBeLeagueManager) && currentUserRecord.IsFailure)
        {
            return(GetFailedResult <LeagueRecord>(Unauthorized()));
        }

        var league = await _fantasyCriticService.GetLeagueByID(leagueID);

        if (league is null)
        {
            return(GetFailedResult <LeagueRecord>(BadRequest("League does not exist.")));
        }

        var playersInLeague = await _leagueMemberService.GetUsersWithRemoveStatus(league);

        bool         isInLeague      = false;
        LeagueInvite?leagueInvite    = null;
        bool         isLeagueManager = false;
        bool         userIsAdmin     = false;

        if (currentUserRecord.IsSuccess)
        {
            userIsAdmin = await _userManager.IsInRoleAsync(currentUserRecord.Value, "Admin");

            isLeagueManager = league.LeagueManager.Id == currentUserRecord.Value.Id;
            if (requiredRelationship.MustBeLeagueManager && !isLeagueManager)
            {
                return(GetFailedResult <LeagueRecord>(Forbid()));
            }

            if (isLeagueManager)
            {
                isInLeague = true;
            }
            else
            {
                isInLeague = playersInLeague.Any(x => x.User.Id == currentUserRecord.Value.Id);
                if (!isInLeague)
                {
                    var inviteesToLeague = await _leagueMemberService.GetOutstandingInvitees(league);

                    leagueInvite = inviteesToLeague.GetMatchingInvite(currentUserRecord.Value.Email);
                }
            }
        }

        if (!isInLeague && leagueInvite is null && requiredRelationship.MustBeInOrInvitedToLeague)
        {
            return(GetFailedResult <LeagueRecord>(Forbid()));
        }

        LeagueUserRelationship relationship = new LeagueUserRelationship(leagueInvite, isInLeague, isLeagueManager, userIsAdmin);

        return(new GenericResultRecord <LeagueRecord>(new LeagueRecord(currentUserRecord.ToNullable(), league, playersInLeague, relationship), null));
    }
コード例 #2
0
    public async Task <IActionResult> DeleteLeague([FromBody] DeleteLeagueRequest request)
    {
        League?league = await _fantasyCriticService.GetLeagueByID(request.LeagueID);

        if (league is null)
        {
            return(BadRequest());
        }

        if (!league.TestLeague)
        {
            return(BadRequest());
        }

        await _fantasyCriticService.DeleteLeague(league);

        return(Ok());
    }
コード例 #3
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));
        }
コード例 #4
0
        public async Task <IActionResult> AvailableYears(Guid id)
        {
            var currentUser = await _userManager.FindByNameAsync(User.Identity.Name);

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

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

            var league = await _fantasyCriticService.GetLeagueByID(id);

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

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

            IReadOnlyList <SupportedYear> supportedYears = await _interLeagueService.GetSupportedYears();

            var openYears      = supportedYears.Where(x => x.OpenForCreation).Select(x => x.Year);
            var availableYears = openYears.Except(league.Value.Years);

            var userIsBetaUser = await _userManager.IsInRoleAsync(currentUser, "BetaTester");

            if (userIsBetaUser)
            {
                var betaYears = supportedYears.Where(x => x.OpenForBetaUsers).Select(x => x.Year);
                availableYears = availableYears.Concat(betaYears).Distinct();
            }

            return(Ok(availableYears));
        }
コード例 #5
0
        public async Task <IActionResult> AvailableYears(Guid id)
        {
            var currentUser = await _userManager.FindByNameAsync(User.Identity.Name);

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

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

            var league = await _fantasyCriticService.GetLeagueByID(id);

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

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

            var supportedYears = await _fantasyCriticService.GetSupportedYears();

            var openYears      = supportedYears.Where(x => x.OpenForCreation).Select(x => x.Year);
            var availableYears = openYears.Except(league.Value.Years);

            return(Ok(availableYears));
        }