public async Task <IActionResult> ManagerDraftGame([FromBody] ManagerDraftGameRequest request) { var currentUser = await _userManager.FindByNameAsync(User.Identity.Name); if (!ModelState.IsValid) { return(BadRequest()); } var publisher = await _publisherService.GetPublisher(request.PublisherID); if (publisher.HasNoValue) { return(BadRequest()); } var league = await _fantasyCriticService.GetLeagueByID(publisher.Value.LeagueYear.League.LeagueID); if (league.HasNoValue) { return(BadRequest()); } if (league.Value.LeagueManager.UserID != currentUser.UserID) { return(Forbid()); } var leagueYear = await _fantasyCriticService.GetLeagueYear(league.Value.LeagueID, publisher.Value.LeagueYear.Year); if (leagueYear.HasNoValue) { return(BadRequest()); } if (!leagueYear.Value.PlayStatus.DraftIsActive) { return(BadRequest("You can't draft a game if the draft isn't active.")); } var publishersInLeague = await _publisherService.GetPublishersInLeagueForYear(leagueYear.Value); var nextPublisher = _draftService.GetNextDraftPublisher(leagueYear.Value, publishersInLeague); if (nextPublisher.HasNoValue) { return(BadRequest("There are no spots open to draft.")); } if (!nextPublisher.Value.Equals(publisher.Value)) { return(BadRequest("That publisher is not next up for drafting.")); } Maybe <MasterGame> masterGame = Maybe <MasterGame> .None; if (request.MasterGameID.HasValue) { masterGame = await _interLeagueService.GetMasterGame(request.MasterGameID.Value); } var draftPhase = _draftService.GetDraftPhase(leagueYear.Value, publishersInLeague); if (draftPhase.Equals(DraftPhase.StandardGames)) { if (request.CounterPick) { return(BadRequest("Not drafting counterPicks now.")); } } if (draftPhase.Equals(DraftPhase.CounterPicks)) { if (!request.CounterPick) { return(BadRequest("Not drafting standard games now.")); } } var draftStatus = _draftService.GetDraftStatus(leagueYear.Value, publishersInLeague); ClaimGameDomainRequest domainRequest = new ClaimGameDomainRequest(publisher.Value, request.GameName, request.CounterPick, false, request.ManagerOverride, masterGame, draftStatus.DraftPosition, draftStatus.OverallDraftPosition); var result = await _draftService.DraftGame(domainRequest, true, leagueYear.Value, publishersInLeague); var viewModel = new ManagerClaimResultViewModel(result.Result); await _hubContext.Clients.Group(leagueYear.Value.GetGroupName).SendAsync("RefreshLeagueYear"); if (result.DraftComplete) { await _hubContext.Clients.Group(leagueYear.Value.GetGroupName).SendAsync("DraftFinished"); } return(Ok(viewModel)); }
public async Task <IActionResult> ManagerDraftGame([FromBody] ManagerDraftGameRequest request) { var leagueYearPublisherRecord = await GetExistingLeagueYearAndPublisher(request.PublisherID, ActionProcessingModeBehavior.Allow, RequiredRelationship.LeagueManager, RequiredYearStatus.ActiveDraft); if (leagueYearPublisherRecord.FailedResult is not null) { return(leagueYearPublisherRecord.FailedResult); } var validResult = leagueYearPublisherRecord.ValidResult !; var leagueYear = validResult.LeagueYear; var publisher = validResult.Publisher; var nextPublisher = _draftService.GetNextDraftPublisher(leagueYear); if (nextPublisher is null) { return(BadRequest("There are no spots open to draft.")); } if (!nextPublisher.Equals(publisher)) { return(BadRequest("That publisher is not next up for drafting.")); } MasterGame?masterGame = null; if (request.MasterGameID.HasValue) { masterGame = await _interLeagueService.GetMasterGame(request.MasterGameID.Value); } var draftPhase = _draftService.GetDraftPhase(leagueYear); if (draftPhase.Equals(DraftPhase.StandardGames)) { if (request.CounterPick) { return(BadRequest("Not drafting counterPicks now.")); } } if (draftPhase.Equals(DraftPhase.CounterPicks)) { if (!request.CounterPick) { return(BadRequest("Not drafting standard games now.")); } } var draftStatus = _draftService.GetDraftStatus(draftPhase, leagueYear); bool counterPickedGameIsManualWillNotRelease = PlayerGameExtensions.CounterPickedGameIsManualWillNotRelease(leagueYear, request.CounterPick, masterGame, false); ClaimGameDomainRequest domainRequest = new ClaimGameDomainRequest(leagueYear, publisher, request.GameName, request.CounterPick, counterPickedGameIsManualWillNotRelease, request.ManagerOverride, false, masterGame, draftStatus.DraftPosition, draftStatus.OverallDraftPosition); var result = await _draftService.DraftGame(domainRequest, true); var viewModel = new ManagerClaimResultViewModel(result.Result); await _hubContext.Clients.Group(leagueYear.GetGroupName).SendAsync("RefreshLeagueYear"); if (result.DraftComplete) { await _hubContext.Clients.Group(leagueYear.GetGroupName).SendAsync("DraftFinished"); } return(Ok(viewModel)); }