示例#1
0
        public void ValidatePlayerStats_HasPlayerStatsSeventeenPlayers_ThrowsMatchResultMaxPlayersExceededException()
        {
            var stats = new List <PlayerFixtureStats>();

            for (int i = 0; i < 17; i++)
            {
                stats.Add(new PlayerFixtureStats()
                {
                    HasPlayed = true
                });
            }

            stats.Add(new PlayerFixtureStats()
            {
                HasPlayed = false
            });
            stats.Add(new PlayerFixtureStats()
            {
                HasPlayed = false
            });

            Assert.Throws <MatchResultMaxPlayersExceededException>(() => model.ValidatePlayerStats(stats, Arg.Any <int>(), true));
        }
        public ActionResult Edit(MatchResultViewModel model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    model.MapMvps();

                    // Should validation methods go into the view model?
                    model.ValidateFixture();
                    model.ValidatePlayerStats();

                    // I'm not particularly happy using transactions within a controller but at present,
                    // because saving a match result seems to require the use of several services rather
                    // than one, I can't think of a better way to do it
                    // EFCachingProvider doesn't support TransactionScope out of the box so changed source with the following https://gist.github.com/797390
                    using (TransactionScope scope = new TransactionScope())
                    {
                        Fixture fixtureToUpdate = fixtureService.Get(model.FixtureId);
                        fixtureToUpdate = model.MapToFixture(fixtureToUpdate);

                        // Save fixtures
                        matchResultService.SaveMatchResult(fixtureToUpdate, membershipService.GetLoggedInUser(), model.ForfeitingTeamId);

                        // Save all player stats including fixture, season, league and career stats
                        matchResultService.SaveMatchStats(model.HasPlayerStats, model.HomePlayerStats, fixtureToUpdate.HomeTeamLeague, fixtureToUpdate);
                        matchResultService.SaveMatchStats(model.HasPlayerStats, model.AwayPlayerStats, fixtureToUpdate.AwayTeamLeague, fixtureToUpdate);

                        scope.Complete();
                    }

                    SuccessMessage(FormMessages.SaveSuccess);

                    // I really dislike doing this, but it's an easy win so I'll turn a blind eye for now
                    if (membershipService.IsSiteAdmin(membershipService.GetLoggedInUserName()))
                    {
                        return(RedirectToAction("Index", "Fixtures", new { area = "Admin" }));
                    }

                    return(RedirectToAction("Index"));
                }
                catch (MatchResultScoresSameException)
                {
                    ErrorMessage(FormMessages.MatchResultScoresSame);
                }
                catch (MatchResultZeroTeamScoreException)
                {
                    ErrorMessage(FormMessages.MatchResultZeroTeamScore);
                }
                catch (MatchResultMaxPlayersExceededException)
                {
                    ErrorMessage(FormMessages.MatchResultMaxPlayersExceeded);
                }
                catch (MatchResultLessThanFivePlayersEachTeamException)
                {
                    ErrorMessage(FormMessages.MatchResultFivePlayersEachTeam);
                }
                catch (MatchResultSumOfScoresDoesNotMatchTotalException)
                {
                    ErrorMessage(FormMessages.MatchResultSumScoreDoesNotMatch);
                }
                catch (MatchResultNoMvpException)
                {
                    ErrorMessage(FormMessages.MatchResultNoMvp);
                }
                catch (MatchResultNoStatsMoreThanZeroPlayersException)
                {
                    ErrorMessage(FormMessages.MatchResultNoStatsZeroPlayers);
                }
            }

            return(View(model));
        }