Example #1
0
        public async Task <IActionResult> AddNewLeagueYear([FromBody] NewLeagueYearRequest request)
        {
            var currentUser = await _userManager.FindByNameAsync(User.Identity.Name);

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

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

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

            if (league.Value.Years.Contains(request.Year))
            {
                return(BadRequest());
            }

            var supportedYears = await _interLeagueService.GetSupportedYears();

            var selectedSupportedYear = supportedYears.SingleOrDefault(x => x.Year == request.Year);

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

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

            bool yearIsOpen = selectedSupportedYear.OpenForCreation || (userIsBetaUser && selectedSupportedYear.OpenForBetaUsers);

            if (!yearIsOpen)
            {
                return(BadRequest());
            }

            if (!league.Value.Years.Any())
            {
                throw new Exception("League has no initial year.");
            }

            var mostRecentYear       = league.Value.Years.Max();
            var mostRecentLeagueYear = await _fantasyCriticService.GetLeagueYear(league.Value.LeagueID, mostRecentYear);

            if (mostRecentLeagueYear.HasNoValue)
            {
                throw new Exception("Most recent league year could not be found");
            }

            await _fantasyCriticService.AddNewLeagueYear(league.Value, request.Year, mostRecentLeagueYear.Value.Options, mostRecentLeagueYear.Value);

            return(Ok());
        }
    public async Task <IActionResult> AddNewLeagueYear([FromBody] NewLeagueYearRequest request)
    {
        var leagueRecord = await GetExistingLeague(request.LeagueID, RequiredRelationship.LeagueManager);

        if (leagueRecord.FailedResult is not null)
        {
            return(leagueRecord.FailedResult);
        }
        var validResult = leagueRecord.ValidResult !;
        var currentUser = validResult.CurrentUser !;
        var league      = validResult.League;

        if (league.Years.Contains(request.Year))
        {
            return(BadRequest());
        }

        var supportedYears = await _interLeagueService.GetSupportedYears();

        var selectedSupportedYear = supportedYears.SingleOrDefault(x => x.Year == request.Year);

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

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

        bool yearIsOpen = selectedSupportedYear.OpenForCreation || (userIsBetaUser && selectedSupportedYear.OpenForBetaUsers);

        if (!yearIsOpen)
        {
            return(BadRequest());
        }

        if (!league.Years.Any())
        {
            throw new Exception("League has no initial year.");
        }

        var mostRecentYear       = league.Years.Max();
        var mostRecentLeagueYear = await _fantasyCriticService.GetLeagueYear(league.LeagueID, mostRecentYear);

        if (mostRecentLeagueYear is null)
        {
            throw new Exception("Most recent league year could not be found");
        }

        var updatedOptions = mostRecentLeagueYear.Options.UpdateOptionsForYear(request.Year);
        await _fantasyCriticService.AddNewLeagueYear(league, request.Year, updatedOptions, mostRecentLeagueYear);

        return(Ok());
    }
        public async Task <IActionResult> AddNewLeagueYear([FromBody] NewLeagueYearRequest request)
        {
            var currentUser = await _userManager.FindByNameAsync(User.Identity.Name);

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

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

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

            if (league.Value.Years.Contains(request.Year))
            {
                return(BadRequest());
            }

            var supportedYears = await _fantasyCriticService.GetSupportedYears();

            if (!supportedYears.Select(x => x.Year).Contains(request.Year))
            {
                return(BadRequest());
            }

            if (!league.Value.Years.Any())
            {
                throw new Exception("League has no initial year.");
            }

            var mostRecentYear       = league.Value.Years.Max();
            var mostRecentLeagueYear = await _fantasyCriticService.GetLeagueYear(league.Value.LeagueID, mostRecentYear);

            if (mostRecentLeagueYear.HasNoValue)
            {
                throw new Exception("Most recent league year could not be found");
            }

            await _fantasyCriticService.AddNewLeagueYear(league.Value, request.Year, mostRecentLeagueYear.Value.Options);

            return(Ok());
        }