public void MapToFixture_Success() { model = new MatchResultViewModel() { FixtureId = 10, HomeTeamName = "Velocity", AwayTeamName = "Vikings", HomeTeamScore = 99, AwayTeamScore = 78, HasPlayerStats = true, Report = "123", IsPenaltyAllowed = true, }; Fixture f = new Fixture() { HomeTeamLeague = new TeamLeague(), AwayTeamLeague = new TeamLeague() }; f = model.MapToFixture(f); Assert.That(f.Id, Is.Not.EqualTo(model.FixtureId)); Assert.That(f.HomeTeamLeague.TeamNameLong, Is.Not.EqualTo(model.HomeTeamName)); Assert.That(f.AwayTeamLeague.TeamNameLong, Is.Not.EqualTo(model.AwayTeamName)); Assert.That(f.HomeTeamScore, Is.EqualTo(model.HomeTeamScore)); Assert.That(f.AwayTeamScore, Is.EqualTo(model.AwayTeamScore)); Assert.That(f.HasPlayerStats, Is.EqualTo("Y")); Assert.That(f.Report, Is.EqualTo(model.Report)); Assert.That(f.IsPenaltyAllowed, Is.EqualTo(true)); Assert.That(f.LastUpdated, Is.Not.Null); Assert.That(f.LastUpdatedBy, Is.Null); }
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)); }