public async Task <IActionResult> StartDraft([FromBody] StartDraftRequest request) { var leagueYearRecord = await GetExistingLeagueYear(request.LeagueID, request.Year, ActionProcessingModeBehavior.Allow, RequiredRelationship.LeagueManager, RequiredYearStatus.PlayOpenDraftNotStarted); 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); bool readyToPlay = _draftService.LeagueIsReadyToPlay(leagueYear.SupportedYear, leagueYear.Publishers, activeUsers); if (!readyToPlay) { return(BadRequest()); } var draftComplete = await _draftService.StartDraft(leagueYear); await _hubContext.Clients.Group(leagueYear.GetGroupName).SendAsync("RefreshLeagueYear"); if (draftComplete) { await _hubContext.Clients.Group(leagueYear.GetGroupName).SendAsync("DraftFinished"); } return(Ok()); }
public async Task <IActionResult> StartDraft([FromBody] StartDraftRequest 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.Equals(PlayStatus.NotStartedDraft)) { return(BadRequest()); } var supportedYear = (await _interLeagueService.GetSupportedYears()).SingleOrDefault(x => x.Year == request.Year); if (supportedYear is null) { return(BadRequest()); } var activeUsers = await _leagueMemberService.GetActivePlayersForLeagueYear(league.Value, request.Year); var publishersInLeague = await _publisherService.GetPublishersInLeagueForYear(leagueYear.Value); bool readyToPlay = _draftService.LeagueIsReadyToPlay(supportedYear, publishersInLeague, activeUsers); if (!readyToPlay) { return(BadRequest()); } var draftComplete = await _draftService.StartDraft(leagueYear.Value); await _hubContext.Clients.Group(leagueYear.Value.GetGroupName).SendAsync("RefreshLeagueYear"); if (draftComplete) { await _hubContext.Clients.Group(leagueYear.Value.GetGroupName).SendAsync("DraftFinished"); } return(Ok()); }