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

            var model = new TeamViewModel(contentModel.Content, Services?.UserService)
            {
                Team = await _teamDataSource.ReadTeamByRoute(Request.RawUrl, true).ConfigureAwait(false)
            };


            if (model.Team == null)
            {
                return(new HttpNotFoundResult());
            }
            else
            {
                model.IsAuthorized = _authorizationPolicy.IsAuthorized(model.Team);

                model.Metadata.PageTitle = "Edit " + model.Team.TeamName;

                model.Breadcrumbs.Add(new Breadcrumb {
                    Name = Constants.Pages.Teams, Url = new Uri(Constants.Pages.TeamsUrl, UriKind.Relative)
                });
                if (model.Team.Club != null)
                {
                    model.Breadcrumbs.Add(new Breadcrumb {
                        Name = model.Team.Club.ClubName, Url = new Uri(model.Team.Club.ClubRoute, UriKind.Relative)
                    });
                }
                model.Breadcrumbs.Add(new Breadcrumb {
                    Name = model.Team.TeamName, Url = new Uri(model.Team.TeamRoute, UriKind.Relative)
                });

                return(CurrentTemplate(model));
            }
        }
Beispiel #2
0
        public async Task <ActionResult> UpdateTeam([Bind(Prefix = "Team", Include = "TeamName,TeamType,AgeRangeLower,AgeRangeUpper,UntilYear,PlayerType,MatchLocations,Facebook,Twitter,Instagram,YouTube,Website")] Team team)
        {
            if (team is null)
            {
                throw new System.ArgumentNullException(nameof(team));
            }

            var beforeUpdate = await _teamDataSource.ReadTeamByRoute(Request.RawUrl).ConfigureAwait(false);

            team.TeamId    = beforeUpdate.TeamId;
            team.TeamRoute = beforeUpdate.TeamRoute;

            // get this from the unvalidated form instead of via modelbinding so that HTML can be allowed
            team.Introduction          = Request.Unvalidated.Form["Team.Introduction"];
            team.PlayingTimes          = Request.Unvalidated.Form["Team.PlayingTimes"];
            team.Cost                  = Request.Unvalidated.Form["Team.Cost"];
            team.PublicContactDetails  = Request.Unvalidated.Form["Team.PublicContactDetails"];
            team.PrivateContactDetails = Request.Unvalidated.Form["Team.PrivateContactDetails"];

            if (team.AgeRangeLower < 11 && !team.AgeRangeUpper.HasValue)
            {
                ModelState.AddModelError("Team.AgeRangeUpper", "School teams and junior teams must specify a maximum age for players");
            }

            if (team.AgeRangeUpper.HasValue && team.AgeRangeUpper < team.AgeRangeLower)
            {
                ModelState.AddModelError("Team.AgeRangeUpper", "The maximum age for players cannot be lower than the minimum age");
            }

            // We're not interested in validating the details of the selected locations
            foreach (var key in ModelState.Keys.Where(x => x.StartsWith("Team.MatchLocations", StringComparison.OrdinalIgnoreCase)))
            {
                ModelState[key].Errors.Clear();
            }

            var isAuthorized = _authorizationPolicy.IsAuthorized(beforeUpdate);

            if (isAuthorized[AuthorizedAction.EditTeam] && ModelState.IsValid)
            {
                var currentMember = Members.GetCurrentMember();
                var updatedTeam   = await _teamRepository.UpdateTeam(team, currentMember.Key, currentMember.Name).ConfigureAwait(false);

                _cacheOverride.OverrideCacheForCurrentMember(CacheConstants.TeamListingsCacheKeyPrefix);

                // redirect back to the team actions
                return(Redirect(updatedTeam.TeamRoute + "/edit"));
            }

            var model = new TeamViewModel(CurrentPage, Services.UserService)
            {
                Team = team,
            };

            model.IsAuthorized       = isAuthorized;
            model.Metadata.PageTitle = $"Edit {team.TeamName}";

            model.Breadcrumbs.Add(new Breadcrumb {
                Name = Constants.Pages.Teams, Url = new Uri(Constants.Pages.TeamsUrl, UriKind.Relative)
            });
            if (model.Team.Club != null)
            {
                model.Breadcrumbs.Add(new Breadcrumb {
                    Name = model.Team.Club.ClubName, Url = new Uri(model.Team.Club.ClubRoute, UriKind.Relative)
                });
            }
            model.Breadcrumbs.Add(new Breadcrumb {
                Name = model.Team.TeamName, Url = new Uri(model.Team.TeamRoute, UriKind.Relative)
            });

            return(View("EditTeam", model));
        }
Beispiel #3
0
        public async Task <ActionResult> UpdateTransientTeam([Bind(Prefix = "Team", Include = "TeamName,AgeRangeLower,AgeRangeUpper,PlayerType,Facebook,Twitter,Instagram,YouTube,Website")] Team team)
        {
            if (team is null)
            {
                throw new ArgumentNullException(nameof(team));
            }

            var beforeUpdate = await _teamDataSource.ReadTeamByRoute(Request.RawUrl).ConfigureAwait(false);

            team.TeamId    = beforeUpdate.TeamId;
            team.TeamRoute = beforeUpdate.TeamRoute;

            // get this from the unvalidated form instead of via modelbinding so that HTML can be allowed
            team.Introduction          = Request.Unvalidated.Form["Team.Introduction"];
            team.Cost                  = Request.Unvalidated.Form["Team.Cost"];
            team.PublicContactDetails  = Request.Unvalidated.Form["Team.PublicContactDetails"];
            team.PrivateContactDetails = Request.Unvalidated.Form["Team.PrivateContactDetails"];

            if (team.AgeRangeLower < 11 && !team.AgeRangeUpper.HasValue)
            {
                ModelState.AddModelError("Team.AgeRangeUpper", "School teams and junior teams must specify a maximum age for players");
            }

            if (team.AgeRangeUpper.HasValue && team.AgeRangeUpper < team.AgeRangeLower)
            {
                ModelState.AddModelError("Team.AgeRangeUpper", "The maximum age for players cannot be lower than the minimum age");
            }

            var isAuthorized = _authorizationPolicy.IsAuthorized(beforeUpdate);

            if (isAuthorized[AuthorizedAction.EditTeam] && ModelState.IsValid)
            {
                var currentMember = Members.GetCurrentMember();
                var updatedTeam   = await _teamRepository.UpdateTransientTeam(team, currentMember.Key, currentMember.Name).ConfigureAwait(false);

                // redirect back to the team, as there is no actions page for a transient team
                return(Redirect(updatedTeam.TeamRoute));
            }

            var model = new TeamViewModel(CurrentPage, Services.UserService)
            {
                Team = team,
            };

            model.IsAuthorized = isAuthorized;

            model.Matches = new MatchListingViewModel(CurrentPage, Services?.UserService)
            {
                Matches = await _matchDataSource.ReadMatchListings(new MatchFilter
                {
                    TeamIds = new List <Guid> {
                        model.Team.TeamId.Value
                    },
                    IncludeMatches = false
                }, MatchSortOrder.MatchDateEarliestFirst).ConfigureAwait(false),
                DateTimeFormatter = _dateFormatter
            };

            var match = model.Matches.Matches.First();

            model.Metadata.PageTitle = $"Edit {model.Team.TeamName}, {_dateFormatter.FormatDate(match.StartTime, false, false)}";

            model.Breadcrumbs.Add(new Breadcrumb {
                Name = match.MatchName, Url = new Uri(match.MatchRoute, UriKind.Relative)
            });

            return(View("EditTransientTeam", model));
        }