public async Task <IActionResult> SetDraftOrder([FromBody] DraftOrderRequest request)
    {
        var leagueYearRecord = await GetExistingLeagueYear(request.LeagueID, request.Year, ActionProcessingModeBehavior.Allow, RequiredRelationship.LeagueManager, RequiredYearStatus.YearNotFinishedDraftNotStarted);

        if (leagueYearRecord.FailedResult is not null)
        {
            return(leagueYearRecord.FailedResult);
        }
        var validResult = leagueYearRecord.ValidResult !;
        var leagueYear  = validResult.LeagueYear;

        var activeUsers = await _leagueMemberService.GetActivePlayersForLeagueYear(leagueYear.League, request.Year);

        var readyToSetDraftOrder = _draftService.LeagueIsReadyToSetDraftOrder(leagueYear.Publishers, activeUsers);

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

        List <KeyValuePair <Publisher, int> > draftPositions = new List <KeyValuePair <Publisher, int> >();

        for (var index = 0; index < request.PublisherDraftPositions.Count; index++)
        {
            var requestPublisher = request.PublisherDraftPositions[index];
            var publisher        = leagueYear.GetPublisherByID(requestPublisher);
            if (publisher is null)
            {
                return(BadRequest());
            }

            draftPositions.Add(new KeyValuePair <Publisher, int>(publisher, index + 1));
        }

        var result = await _draftService.SetDraftOrder(leagueYear, draftPositions);

        if (result.IsFailure)
        {
            return(BadRequest(result.Error));
        }

        return(Ok());
    }
示例#2
0
        public async Task <IActionResult> SetDraftOrder([FromBody] DraftOrderRequest 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(Forbid());
            }

            var leagueYear = await _fantasyCriticService.GetLeagueYear(league.Value.LeagueID, request.Year);

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

            if (leagueYear.Value.PlayStatus.PlayStarted)
            {
                return(BadRequest());
            }

            var activeUsers = await _leagueMemberService.GetActivePlayersForLeagueYear(league.Value, request.Year);

            var publishersInLeague = await _publisherService.GetPublishersInLeagueForYear(leagueYear.Value);

            var readyToSetDraftOrder = _draftService.LeagueIsReadyToSetDraftOrder(publishersInLeague, activeUsers);

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

            List <KeyValuePair <Publisher, int> > draftPositions = new List <KeyValuePair <Publisher, int> >();

            for (var index = 0; index < request.PublisherDraftPositions.Count; index++)
            {
                var requestPublisher = request.PublisherDraftPositions[index];
                var publisher        = publishersInLeague.SingleOrDefault(x => x.PublisherID == requestPublisher);
                if (publisher is null)
                {
                    return(BadRequest());
                }

                draftPositions.Add(new KeyValuePair <Publisher, int>(publisher, index + 1));
            }

            var result = await _draftService.SetDraftOrder(leagueYear.Value, draftPositions);

            if (result.IsFailure)
            {
                return(BadRequest(result.Error));
            }

            return(Ok());
        }