public async override Task <ActionResult> Index(ContentModel contentModel) { if (contentModel is null) { throw new ArgumentNullException(nameof(contentModel)); } var model = new EditCloseOfPlayViewModel(contentModel.Content, Services?.UserService) { Match = await _matchDataSource.ReadMatchByRoute(Request.RawUrl).ConfigureAwait(false), DateFormatter = _dateFormatter }; if (model.Match == null || model.Match.Tournament != null) { return(new HttpNotFoundResult()); } else { // This page is only for matches in the past if (model.Match.StartTime > DateTime.UtcNow) { return(new HttpNotFoundResult()); } // This page is not for matches not played if (model.Match.MatchResultType.HasValue && new List <MatchResultType> { MatchResultType.HomeWinByForfeit, MatchResultType.AwayWinByForfeit, MatchResultType.Postponed, MatchResultType.Cancelled }.Contains(model.Match.MatchResultType.Value)) { return(new HttpNotFoundResult()); } model.IsAuthorized = _authorizationPolicy.IsAuthorized(model.Match); model.FormData.MatchResultType = model.Match.MatchResultType; model.FormData.Awards = model.Match.Awards.Select(x => new MatchAwardViewModel { MatchAwardId = x.AwardedToId, PlayerSearch = x.PlayerIdentity.PlayerIdentityName, TeamId = model.Match.Teams.First(team => team.Team.TeamId == x.PlayerIdentity.Team.TeamId).Team.TeamId, Reason = x.Reason }).ToList(); if (!model.Match.MatchResultType.HasValue) { model.Match.MatchResultType = _matchResultEvaluator.EvaluateMatchResult(model.Match); } 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)); } }
public async Task <ActionResult> UpdateMatch([Bind(Prefix = "FormData")] EditCloseOfPlayFormData postedData) { if (postedData is null) { throw new ArgumentNullException(nameof(postedData)); } var beforeUpdate = await _matchDataSource.ReadMatchByRoute(Request.RawUrl).ConfigureAwait(false); if (beforeUpdate.StartTime > DateTime.UtcNow || beforeUpdate.Tournament != null) { return(new HttpNotFoundResult()); } var model = new EditCloseOfPlayViewModel(CurrentPage, Services.UserService) { Match = beforeUpdate, FormData = postedData, DateFormatter = _dateTimeFormatter }; model.Match.MatchResultType = model.FormData.MatchResultType; model.Match.Awards = model.FormData.Awards.Select(x => new MatchAward { AwardedToId = x.MatchAwardId, Award = new Award { AwardName = StatisticsConstants.PLAYER_OF_THE_MATCH_AWARD }, PlayerIdentity = new PlayerIdentity { PlayerIdentityName = x.PlayerSearch, Team = new Team { TeamId = x.TeamId } }, Reason = x.Reason }).ToList(); model.Match.UpdateMatchNameAutomatically = beforeUpdate.UpdateMatchNameAutomatically; model.Match.Teams = beforeUpdate.Teams; model.IsAuthorized = _authorizationPolicy.IsAuthorized(beforeUpdate); if (model.IsAuthorized[AuthorizedAction.EditMatchResult] && ModelState.IsValid) { if ((int)model.Match.MatchResultType == -1) { model.Match.MatchResultType = null; } var currentMember = Members.GetCurrentMember(); var updatedMatch = await _matchRepository.UpdateCloseOfPlay(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("EditCloseOfPlay", model)); }