Ejemplo n.º 1
0
        public void MapMvps_MvpsSet_MvpsSet()
        {
            var homePlayerStats = new List <PlayerFixtureStats>();

            homePlayerStats.Add(new PlayerFixtureStats()
            {
                PlayerId = 1
            });
            homePlayerStats.Add(new PlayerFixtureStats()
            {
                PlayerId = 2
            });

            var awayPlayerStats = new List <PlayerFixtureStats>();

            awayPlayerStats.Add(new PlayerFixtureStats()
            {
                PlayerId = 3
            });
            awayPlayerStats.Add(new PlayerFixtureStats()
            {
                PlayerId = 4
            });

            model.HomeMvpPlayerId = 1;
            model.AwayMvpPlayerId = 4;

            model.HomePlayerStats = homePlayerStats;
            model.AwayPlayerStats = awayPlayerStats;

            model.HasPlayerStats = true;

            model.MapMvps();

            Assert.That(model.HomePlayerStats.Where(x => x.PlayerId == model.HomeMvpPlayerId).Single().IsMvp, Is.True);
            Assert.That(model.AwayPlayerStats.Where(x => x.PlayerId == model.AwayMvpPlayerId).Single().IsMvp, Is.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));
        }