public async override Task <ActionResult> Index(ContentModel contentModel) { if (contentModel is null) { throw new ArgumentNullException(nameof(contentModel)); } var model = new EditFriendlyMatchViewModel(contentModel.Content, Services?.UserService) { Match = new Match { MatchType = MatchType.FriendlyMatch, MatchLocation = new MatchLocation() } }; if (Request.RawUrl.StartsWith("/teams/", StringComparison.OrdinalIgnoreCase)) { model.Team = await _teamDataSource.ReadTeamByRoute(Request.RawUrl, true).ConfigureAwait(false); if (model.Team == null) { return(new HttpNotFoundResult()); } var possibleSeasons = _createMatchSeasonSelector.SelectPossibleSeasons(model.Team.Seasons, model.Match.MatchType); model.PossibleSeasons = _editMatchHelper.PossibleSeasonsAsListItems(possibleSeasons); model.HomeTeamId = model.Team.TeamId; model.HomeTeamName = model.Team.TeamName; model.MatchLocationId = model.Team.MatchLocations.FirstOrDefault()?.MatchLocationId; model.MatchLocationName = model.Team.MatchLocations.FirstOrDefault()?.NameAndLocalityOrTownIfDifferent(); } else if (Request.RawUrl.StartsWith("/competitions/", StringComparison.OrdinalIgnoreCase)) { model.Match.Season = model.Season = await _seasonDataSource.ReadSeasonByRoute(Request.RawUrl, false).ConfigureAwait(false); if (model.Season == null || !model.Season.MatchTypes.Contains(MatchType.FriendlyMatch)) { return(new HttpNotFoundResult()); } } model.IsAuthorized[AuthorizedAction.CreateMatch] = User.Identity.IsAuthenticated; model.Breadcrumbs.Add(new Breadcrumb { Name = Constants.Pages.Matches, Url = new Uri(Constants.Pages.MatchesUrl, UriKind.Relative) }); _editMatchHelper.ConfigureAddMatchModelMetadata(model); return(CurrentTemplate(model)); }
public async Task <ActionResult> CreateMatch([Bind(Prefix = "Match", Include = "Season")] Match postedMatch) { if (postedMatch is null) { throw new ArgumentNullException(nameof(postedMatch)); } var model = new EditFriendlyMatchViewModel(CurrentPage, Services.UserService) { Match = postedMatch }; model.Match.MatchType = MatchType.FriendlyMatch; _editMatchHelper.ConfigureModelFromRequestData(model, Request.Unvalidated.Form, Request.Form, ModelState); if (Request.RawUrl.StartsWith("/competitions/", StringComparison.OrdinalIgnoreCase)) { model.Season = model.Match.Season = await _seasonDataSource.ReadSeasonByRoute(Request.RawUrl, false).ConfigureAwait(false); } else 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.AtLeastOneTeamId(model, ModelState); model.IsAuthorized[AuthorizedAction.CreateMatch] = User.Identity.IsAuthenticated; if (model.IsAuthorized[AuthorizedAction.CreateMatch] && ModelState.IsValid && (model.Season == null || model.Season.MatchTypes.Contains(MatchType.FriendlyMatch))) { 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, false).ConfigureAwait(false); var possibleSeasons = _createMatchSeasonSelector.SelectPossibleSeasons(model.Team.Seasons, model.Match.MatchType); model.PossibleSeasons = _editMatchHelper.PossibleSeasonsAsListItems(possibleSeasons); model.Metadata.PageTitle = $"Add a {MatchType.FriendlyMatch.Humanize(LetterCasing.LowerCase)} for {model.Team.TeamName}"; } else if (Request.RawUrl.StartsWith("/competitions/", StringComparison.OrdinalIgnoreCase)) { model.Metadata.PageTitle = $"Add a {MatchType.FriendlyMatch.Humanize(LetterCasing.LowerCase)} in the {model.Season.SeasonFullName()}"; } if (!string.IsNullOrEmpty(Request.Form["HomeTeamName"])) { model.HomeTeamName = Request.Form["HomeTeamName"]; } if (!string.IsNullOrEmpty(Request.Form["AwayTeamName"])) { model.AwayTeamName = Request.Form["AwayTeamName"]; } model.Breadcrumbs.Add(new Breadcrumb { Name = Constants.Pages.Matches, Url = new Uri(Constants.Pages.MatchesUrl, UriKind.Relative) }); return(View("CreateFriendlyMatch", model)); }
public async Task <ActionResult> UpdateMatch([Bind(Prefix = "Match", Include = "Season,MatchResultType")] Match postedMatch) { if (postedMatch is null) { throw new ArgumentNullException(nameof(postedMatch)); } var beforeUpdate = await _matchDataSource.ReadMatchByRoute(Request.RawUrl).ConfigureAwait(false); // This controller is only for matches in the future if (beforeUpdate.StartTime <= DateTime.UtcNow) { return(new HttpNotFoundResult()); } var model = new EditFriendlyMatchViewModel(CurrentPage, Services.UserService) { Match = postedMatch, DateFormatter = _dateTimeFormatter }; model.Match.MatchId = beforeUpdate.MatchId; 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.AtLeastOneTeamId(model, ModelState); model.IsAuthorized = _authorizationPolicy.IsAuthorized(beforeUpdate); if (model.IsAuthorized[AuthorizedAction.EditMatch] && ModelState.IsValid) { 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("EditFriendlyMatch", model)); }
public async override Task <ActionResult> Index(ContentModel contentModel) { if (contentModel is null) { throw new ArgumentNullException(nameof(contentModel)); } var model = new EditFriendlyMatchViewModel(contentModel.Content, Services?.UserService) { Match = await _matchDataSource.ReadMatchByRoute(Request.RawUrl).ConfigureAwait(false), DateFormatter = _dateFormatter }; if (model.Match == null) { return(new HttpNotFoundResult()); } else { // This page is only for matches in the future if (model.Match.StartTime <= DateTime.UtcNow) { return(new HttpNotFoundResult()); } model.IsAuthorized = _authorizationPolicy.IsAuthorized(model.Match); if (model.Match.Season != null) { model.Match.Season = await _seasonDataSource.ReadSeasonByRoute(model.Match.Season.SeasonRoute, true).ConfigureAwait(false); model.SeasonFullName = model.Match.Season.SeasonFullName(); } model.MatchDate = model.Match.StartTime; if (model.Match.StartTimeIsKnown) { model.StartTime = model.Match.StartTime; } model.HomeTeamId = model.Match.Teams.SingleOrDefault(x => x.TeamRole == TeamRole.Home)?.Team.TeamId; model.HomeTeamName = model.Match.Teams.SingleOrDefault(x => x.TeamRole == TeamRole.Home)?.Team.TeamName; model.AwayTeamId = model.Match.Teams.SingleOrDefault(x => x.TeamRole == TeamRole.Away)?.Team.TeamId; model.AwayTeamName = model.Match.Teams.SingleOrDefault(x => x.TeamRole == TeamRole.Away)?.Team.TeamName; model.MatchLocationId = model.Match.MatchLocation?.MatchLocationId; model.MatchLocationName = model.Match.MatchLocation?.NameAndLocalityOrTownIfDifferent(); model.Metadata.PageTitle = "Edit " + model.Match.MatchFullName(x => _dateFormatter.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(CurrentTemplate(model)); } }