private async Task <RoundSelectorComponentModel> GetRoundSelectorComponentModel(TeamEntity team, CancellationToken cancellationToken) { var tir = new TeamInRoundEntity(); if (!team.IsNew) { // The team could already be assigned to a round tir = (await _appDb.TeamInRoundRepository.GetTeamInRoundAsync( new PredicateExpression(TeamInRoundFields.TeamId == team.Id & RoundFields.TournamentId == _tenantContext.TournamentContext.ApplicationTournamentId), cancellationToken)) .FirstOrDefault(); } return(new RoundSelectorComponentModel { RoundNotSpecifiedKey = null, SelectedRoundId = tir?.RoundId, ShowSelector = await _appDb.MatchRepository.GetMatchCountAsync( new PredicateExpression( RoundFields.TournamentId == _tenantContext.TournamentContext.ApplicationTournamentId), cancellationToken) == 0, TournamentId = _tenantContext.TournamentContext.ApplicationTournamentId, HtmlFieldPrefix = nameof(TeamEditModel.Round) }); }
public void MapFormFieldsToEntity(TeamInRoundEntity teamInRoundEntity) { teamInRoundEntity.IsNew = IsNew; teamInRoundEntity.TeamId = TeamId; teamInRoundEntity.RoundId = RoundId; teamInRoundEntity.TeamNameForRound = TeamNameForRound; }
[TestCase(2, 4, false)] // RoundId 4 does not belong to any tournament public async Task TeamInRound_RoundId_Should_Belong_To_Tournament(long teamTournamentId, long teamInRoundRoundId, bool expected) { var tournament = await _appDb.TournamentRepository.GetTournamentAsync(new PredicateExpression(TournamentFields.Id == teamTournamentId), CancellationToken.None); var rounds = await _appDb.RoundRepository.GetRoundsWithTypeAsync( new PredicateExpression(RoundFields.TournamentId == teamTournamentId), CancellationToken.None); var team = new TeamInRoundEntity { TeamId = 1, RoundId = teamInRoundRoundId }; _organizationContext.TeamTournamentId = teamTournamentId; var tv = new TeamInRoundValidator(team, (_organizationContext, teamTournamentId)); var factResult = await tv.CheckAsync(TeamInRoundValidator.FactId.RoundBelongsToTournament, CancellationToken.None); Assert.Multiple(() => { Assert.IsTrue(factResult.Enabled); Assert.AreEqual(expected, factResult.Success); if (!factResult.Success) { Assert.IsTrue(factResult.Message.Contains(tournament.Description)); } Assert.IsNull(factResult.Exception); }); }
public void MapEntityToFormFields(TeamInRoundEntity teamInRoundEntity) { IsNew = teamInRoundEntity.IsNew; Id = teamInRoundEntity.Id; TeamId = teamInRoundEntity.TeamId; RoundId = teamInRoundEntity.RoundId; TeamNameForRound = teamInRoundEntity.TeamNameForRound; }
public async Task <IActionResult> Confirm(bool done, CancellationToken cancellationToken) { var sessionModel = await GetModelFromSession(cancellationToken); if (!sessionModel.IsFromSession) { return(RedirectToAction(nameof(SelectTeam), new { Organization = _tenantContext.SiteContext.UrlSegmentValue })); } try { var teamInRoundEntity = new TeamInRoundEntity(); try { // If the team had already been registered for another round, we have get the existing entity if (!sessionModel.TeamInRound.IsNew) { teamInRoundEntity = (await _appDb.TeamInRoundRepository.GetTeamInRoundAsync( new PredicateExpression(TeamInRoundFields.Id == sessionModel.TeamInRound.Id), cancellationToken)).First(); } } catch (Exception e) { _logger.LogCritical(e, $"{nameof(TeamInRoundEntity)} with ID {sessionModel.TeamInRound.Id} for team ID {sessionModel.TeamInRound.TeamId} not found"); throw; } var isNewApplication = teamInRoundEntity.IsNew; sessionModel.TeamInRound.MapFormFieldsToEntity(teamInRoundEntity); teamInRoundEntity.Team = new TeamEntity(); sessionModel.Team.MapFormFieldsToEntity(teamInRoundEntity.Team); teamInRoundEntity.Team.Venue = new VenueEntity(); sessionModel.Venue.MapFormFieldsToEntity(teamInRoundEntity.Team.Venue); // Adds the current user as team manager, unless she already is team manager await AddManagerToTeamEntity(teamInRoundEntity.Team, cancellationToken); if (await _appDb.GenericRepository.SaveEntityAsync(teamInRoundEntity, true, true, cancellationToken)) { HttpContext.Session.Remove(TeamApplicationSessionName); TempData.Put <TeamApplicationMessageModel.TeamApplicationMessage>( nameof(TeamApplicationMessageModel.TeamApplicationMessage), new TeamApplicationMessageModel.TeamApplicationMessage { AlertType = SiteAlertTagHelper.AlertType.Success, MessageId = TeamApplicationMessageModel.MessageId.ApplicationSuccess }); _sendEmailTask.SetMessageCreator(new ConfirmTeamApplicationCreator { Parameters = { CultureInfo = CultureInfo.DefaultThreadCurrentUICulture, TeamId = teamInRoundEntity.TeamId, IsNewApplication = isNewApplication, RoundId = teamInRoundEntity.RoundId, RegisteredByUserId = GetCurrentUserId(), UrlToEditApplication = Url.Action(nameof(EditTeam), nameof(TeamApplication),new { Organization = _tenantContext.SiteContext.UrlSegmentValue, teamId = teamInRoundEntity.TeamId }, Request.Scheme, Request.Host.ToString()) } }); _queue.QueueTask(_sendEmailTask); return(RedirectToAction(nameof(List), new { Organization = _tenantContext.SiteContext.UrlSegmentValue })); } throw new Exception($"Saving the {nameof(TeamInRoundEntity)} failed."); } catch (Exception e) { _logger.LogCritical(e, "Team application could not be saved."); HttpContext.Session.Remove(TeamApplicationSessionName); TempData.Put <TeamApplicationMessageModel.TeamApplicationMessage>( nameof(TeamApplicationMessageModel.TeamApplicationMessage), new TeamApplicationMessageModel.TeamApplicationMessage { AlertType = SiteAlertTagHelper.AlertType.Danger, MessageId = TeamApplicationMessageModel.MessageId.ApplicationFailure }); return(RedirectToAction(nameof(List), new { Organization = _tenantContext.SiteContext.UrlSegmentValue })); } }
public async Task <IActionResult> Confirm(bool done, CancellationToken cancellationToken) { var sessionModel = await GetModelFromSession(cancellationToken); if (!sessionModel.IsFromSession) { return(RedirectToAction(nameof(SelectTeam), new { Organization = _siteContext.UrlSegmentValue })); } var teamInRoundEntity = new TeamInRoundEntity(); sessionModel.TeamInRound.MapFormFieldsToEntity(teamInRoundEntity); teamInRoundEntity.Team = new TeamEntity(); sessionModel.Team.MapFormFieldsToEntity(teamInRoundEntity.Team); teamInRoundEntity.Team.Venue = new VenueEntity(); sessionModel.Venue.MapFormFieldsToEntity(teamInRoundEntity.Team.Venue); try { var isNewApplication = teamInRoundEntity.IsNew; // Adds the current user as team manager, unless she already is team manager await AddManagerToTeamEntity(teamInRoundEntity.Team, cancellationToken); if (await _appDb.GenericRepository.SaveEntityAsync(teamInRoundEntity, true, true, cancellationToken)) { HttpContext.Session.Remove(TeamApplicationSessionName); TempData.Put <TeamApplicationMessageModel.TeamApplicationMessage>( nameof(TeamApplicationMessageModel.TeamApplicationMessage), new TeamApplicationMessageModel.TeamApplicationMessage { AlertType = SiteAlertTagHelper.AlertType.Success, MessageId = TeamApplicationMessageModel.MessageId.ApplicationSuccess }); _teamApplicationEmailTask.Model = new ApplicationEmailViewModel { RegisteredByUserId = GetCurrentUserId(), TeamId = teamInRoundEntity.TeamId, TeamName = teamInRoundEntity.TeamNameForRound, IsNewApplication = isNewApplication, TournamentName = sessionModel.TournamentName, RoundId = teamInRoundEntity.RoundId, OrganizationContext = _siteContext, UrlToEditApplication = Url.Action(nameof(EditTeam), nameof(TeamApplication), new { Organization = _siteContext.UrlSegmentValue, teamId = teamInRoundEntity.TeamId }, Request.Scheme, Request.Host.ToString()) }; _teamApplicationEmailTask.Subject = _localizer["Registration for team '{0}'", _teamApplicationEmailTask.Model.TeamName].Value; _teamApplicationEmailTask.EmailCultureInfo = CultureInfo.DefaultThreadCurrentUICulture; _teamApplicationEmailTask.Timeout = TimeSpan.FromMinutes(5); _teamApplicationEmailTask.ViewNames = new[] { null, ViewNames.Emails.ConfirmTeamApplicationTxt }; _queue.QueueTask(_teamApplicationEmailTask); return(RedirectToAction(nameof(List), new { Organization = _siteContext.UrlSegmentValue })); } throw new Exception($"Saving the {nameof(TeamInRoundEntity)} failed."); } catch (Exception e) { _logger.LogCritical(e, "Team application could not be saved."); HttpContext.Session.Remove(TeamApplicationSessionName); TempData.Put <TeamApplicationMessageModel.TeamApplicationMessage>( nameof(TeamApplicationMessageModel.TeamApplicationMessage), new TeamApplicationMessageModel.TeamApplicationMessage { AlertType = SiteAlertTagHelper.AlertType.Danger, MessageId = TeamApplicationMessageModel.MessageId.ApplicationFailure }); return(RedirectToAction(nameof(List), new { Organization = _siteContext.UrlSegmentValue })); } }