public async override Task <ActionResult> Index(ContentModel contentModel)
        {
            if (contentModel is null)
            {
                throw new ArgumentNullException(nameof(contentModel));
            }

            var model = new EditMatchFormatViewModel(contentModel.Content, Services?.UserService)
            {
                Match         = await _matchDataSource.ReadMatchByRoute(Request.RawUrl).ConfigureAwait(false),
                DateFormatter = _dateFormatter
            };

            if (model.Match == null || model.Match.Tournament != null || model.Match.MatchType == MatchType.TrainingSession)
            {
                return(new HttpNotFoundResult());
            }
            else
            {
                model.IsAuthorized = _authorizationPolicy.IsAuthorized(model.Match);

                model.Metadata.PageTitle = "Edit " + model.Match.MatchFullName(x => _dateFormatter.FormatDate(x, false, false, false));

                model.FormData.Overs        = model.Match.MatchInnings.FirstOrDefault()?.OverSets.FirstOrDefault()?.Overs;
                model.FormData.MatchInnings = model.Match.MatchInnings.Count;

                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));
            }
        }
Example #2
0
        public async Task <ActionResult> UpdateMatch([Bind(Prefix = "FormData")] EditMatchFormatFormData postedData)
        {
            if (postedData is null)
            {
                throw new ArgumentNullException(nameof(postedData));
            }

            var beforeUpdate = await _matchDataSource.ReadMatchByRoute(Request.RawUrl).ConfigureAwait(false);

            if (beforeUpdate == null || beforeUpdate.Tournament != null || beforeUpdate.MatchType == MatchType.TrainingSession)
            {
                return(new HttpNotFoundResult());
            }

            var model = new EditMatchFormatViewModel(CurrentPage, Services.UserService)
            {
                Match         = beforeUpdate,
                DateFormatter = _dateTimeFormatter,
                FormData      = postedData
            };

            var matchIsInTheFuture = model.Match.StartTime > DateTimeOffset.UtcNow;

            if (postedData.MatchInnings < 2 || postedData.MatchInnings % 2 != 0)
            {
                ModelState.AddModelError("FormData.MatchInnings", $"A match cannot have an odd number of innings or less than 2 innings.");
            }
            else if (!matchIsInTheFuture && postedData.MatchInnings < model.Match.MatchInnings.Count)
            {
                ModelState.AddModelError("FormData.MatchInnings", $"You cannot reduce the number of innings after a match has happened.");
            }
            else
            {
                while (matchIsInTheFuture && postedData.MatchInnings < model.Match.MatchInnings.Count)
                {
                    model.Match.MatchInnings.RemoveAt(model.Match.MatchInnings.Count - 1);
                }

                if (postedData.MatchInnings > model.Match.MatchInnings.Count)
                {
                    while (postedData.MatchInnings > model.Match.MatchInnings.Count)
                    {
                        var battingMatchTeamId = model.Match.MatchInnings.Count % 2 == 0 ? model.Match.MatchInnings[0].BattingMatchTeamId : model.Match.MatchInnings[1].BattingMatchTeamId;
                        var bowlingMatchTeamId = model.Match.MatchInnings.Count % 2 == 1 ? model.Match.MatchInnings[0].BattingMatchTeamId : model.Match.MatchInnings[1].BattingMatchTeamId;
                        var addedInnings       = _matchInningsFactory.CreateMatchInnings(model.Match, battingMatchTeamId, bowlingMatchTeamId);
                        if (postedData.Overs.HasValue)
                        {
                            addedInnings.OverSets[0].Overs = postedData.Overs;
                        }
                        else
                        {
                            addedInnings.OverSets.Clear();
                        }
                        model.Match.MatchInnings.Add(addedInnings);
                    }
                }

                foreach (var innings in model.Match.MatchInnings)
                {
                    foreach (var overSet in innings.OverSets)
                    {
                        if (!matchIsInTheFuture && postedData.Overs < overSet.Overs)
                        {
                            ModelState.AddModelError("FormData.Overs", $"You cannot reduce the number of overs after a match has happened.");
                        }
                        else
                        {
                            overSet.Overs = postedData.Overs;
                        }
                    }
                }
            }

            model.IsAuthorized = _authorizationPolicy.IsAuthorized(beforeUpdate);

            if (model.IsAuthorized[AuthorizedAction.EditMatch] && ModelState.IsValid)
            {
                var currentMember = Members.GetCurrentMember();
                var updatedMatch  = await _matchRepository.UpdateMatchFormat(model.Match, currentMember.Key, currentMember.Name).ConfigureAwait(false);

                return(Redirect(updatedMatch.MatchRoute + "/edit"));
            }

            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("EditMatchFormat", model));
        }