public async Task <ActionResult> CreateMatch([Bind(Prefix = "Match", Include = "Season")] Match postedMatch) { if (postedMatch is null) { throw new ArgumentNullException(nameof(postedMatch)); } var model = new EditLeagueMatchViewModel(CurrentPage, Services.UserService) { Match = postedMatch }; model.Match.MatchType = MatchType.LeagueMatch; _editMatchHelper.ConfigureModelFromRequestData(model, Request.Unvalidated.Form, Request.Form, ModelState); if (Request.RawUrl.StartsWith("/competitions/", StringComparison.OrdinalIgnoreCase)) { model.Match.Season = model.Season = await _seasonDataSource.ReadSeasonByRoute(Request.RawUrl, true).ConfigureAwait(false); } else if (model.Match.Season != null && model.Match.Season.SeasonId.HasValue) { // Get the season, to support validation against season dates model.Match.Season = await _seasonDataSource.ReadSeasonById(model.Match.Season.SeasonId.Value).ConfigureAwait(false); } _matchValidator.DateIsValidForSqlServer(() => model.MatchDate, ModelState, "MatchDate", "match"); _matchValidator.DateIsWithinTheSeason(() => model.MatchDate, model.Match.Season, ModelState, "MatchDate", "match"); _matchValidator.TeamsMustBeDifferent(model, ModelState); model.IsAuthorized[AuthorizedAction.CreateMatch] = User.Identity.IsAuthenticated; if (model.IsAuthorized[AuthorizedAction.CreateMatch] && ModelState.IsValid && (model.Team == null || (model.PossibleSeasons != null && model.PossibleSeasons.Any())) && (model.Season == null || model.Season.MatchTypes.Contains(MatchType.LeagueMatch))) { var currentMember = Members.GetCurrentMember(); var createdMatch = await _matchRepository.CreateMatch(model.Match, currentMember.Key, currentMember.Name).ConfigureAwait(false); await _cacheClearer.ClearCacheFor(createdMatch).ConfigureAwait(false); return(Redirect(createdMatch.MatchRoute)); } if (Request.RawUrl.StartsWith("/teams/", StringComparison.OrdinalIgnoreCase)) { model.Team = await _teamDataSource.ReadTeamByRoute(Request.RawUrl, true).ConfigureAwait(false); var possibleSeasons = _createMatchSeasonSelector.SelectPossibleSeasons(model.Team.Seasons, model.Match.MatchType).ToList(); if (possibleSeasons.Count == 1) { model.Match.Season = possibleSeasons.First(); } model.PossibleSeasons = _editMatchHelper.PossibleSeasonsAsListItems(possibleSeasons); await _editMatchHelper.ConfigureModelPossibleTeams(model, possibleSeasons).ConfigureAwait(false); model.Metadata.PageTitle = $"Add a {MatchType.LeagueMatch.Humanize(LetterCasing.LowerCase)} for {model.Team.TeamName}"; } else { model.PossibleSeasons = _editMatchHelper.PossibleSeasonsAsListItems(new[] { model.Match.Season }); model.PossibleHomeTeams = _editMatchHelper.PossibleTeamsAsListItems(model.Season?.Teams); model.PossibleAwayTeams = _editMatchHelper.PossibleTeamsAsListItems(model.Season?.Teams); model.Metadata.PageTitle = $"Add a {MatchType.LeagueMatch.Humanize(LetterCasing.LowerCase)} in the {model.Season.SeasonFullName()}"; } model.Breadcrumbs.Add(new Breadcrumb { Name = Constants.Pages.Matches, Url = new Uri(Constants.Pages.MatchesUrl, UriKind.Relative) }); return(View("CreateLeagueMatch", model)); }
public async Task <ActionResult> UpdateMatch([Bind(Prefix = "Match", Include = "Season,Teams,MatchResultType")] Match postedMatch) { if (postedMatch is null) { throw new ArgumentNullException(nameof(postedMatch)); } var beforeUpdate = await _matchDataSource.ReadMatchByRoute(Request.RawUrl).ConfigureAwait(false); var model = new EditTrainingSessionViewModel(CurrentPage, Services.UserService) { Match = postedMatch, DateFormatter = _dateTimeFormatter }; model.Match.MatchId = beforeUpdate.MatchId; model.Match.MatchType = MatchType.TrainingSession; model.Match.MatchRoute = beforeUpdate.MatchRoute; model.Match.UpdateMatchNameAutomatically = beforeUpdate.UpdateMatchNameAutomatically; _editMatchHelper.ConfigureModelFromRequestData(model, Request.Unvalidated.Form, Request.Form, ModelState); if (model.Match.Season != null && !model.Match.Season.SeasonId.HasValue) { model.Match.Season = null; } else if (model.Match.Season != null) { // Get the season, to support validation against season dates model.Match.Season = await _seasonDataSource.ReadSeasonById(model.Match.Season.SeasonId.Value).ConfigureAwait(false); } _matchValidator.DateIsValidForSqlServer(() => model.MatchDate, ModelState, "MatchDate", "match"); _matchValidator.DateIsWithinTheSeason(() => model.MatchDate, model.Match.Season, ModelState, "MatchDate", "match"); _matchValidator.AtLeastOneTeamInMatch(model.Match.Teams, ModelState); foreach (var team in model.Match.Teams) { team.TeamRole = TeamRole.Training; } model.IsAuthorized = _authorizationPolicy.IsAuthorized(beforeUpdate); if (model.IsAuthorized[AuthorizedAction.EditMatch] && ModelState.IsValid && (model.Season == null || model.Season.MatchTypes.Contains(MatchType.TrainingSession))) { if ((int?)model.Match.MatchResultType == -1) { model.Match.MatchResultType = null; } var currentMember = Members.GetCurrentMember(); var updatedMatch = await _matchRepository.UpdateMatch(model.Match, currentMember.Key, currentMember.Name).ConfigureAwait(false); await _cacheClearer.ClearCacheFor(updatedMatch).ConfigureAwait(false); return(Redirect(updatedMatch.MatchRoute)); } model.Match.MatchName = beforeUpdate.MatchName; model.Metadata.PageTitle = "Edit " + model.Match.MatchFullName(x => _dateTimeFormatter.FormatDate(x, false, false, false)); if (model.Match.Season != null) { model.Breadcrumbs.Add(new Breadcrumb { Name = Constants.Pages.Competitions, Url = new Uri(Constants.Pages.CompetitionsUrl, UriKind.Relative) }); model.Breadcrumbs.Add(new Breadcrumb { Name = model.Match.Season.Competition.CompetitionName, Url = new Uri(model.Match.Season.Competition.CompetitionRoute, UriKind.Relative) }); model.Breadcrumbs.Add(new Breadcrumb { Name = model.Match.Season.SeasonName(), Url = new Uri(model.Match.Season.SeasonRoute, UriKind.Relative) }); } else { model.Breadcrumbs.Add(new Breadcrumb { Name = Constants.Pages.Matches, Url = new Uri(Constants.Pages.MatchesUrl, UriKind.Relative) }); } model.Breadcrumbs.Add(new Breadcrumb { Name = model.Match.MatchName, Url = new Uri(model.Match.MatchRoute, UriKind.Relative) }); return(View("EditTrainingSession", model)); }