Inheritance: IEditableViewModel, IGamingGroupAssignedViewModel
コード例 #1
0
        public GameDefinitionDetailsViewModel Build(GameDefinitionSummary gameDefinitionSummary, ApplicationUser currentUser)
        {
            BoardGameGeekInfoViewModel boardGameGeekInfoViewModel = null;
            if (gameDefinitionSummary.BoardGameGeekGameDefinitionId.HasValue)
            {
                boardGameGeekInfoViewModel = _transformer.Transform<BoardGameGeekInfoViewModel>(gameDefinitionSummary.BoardGameGeekInfo);
            }
            var viewModel = new GameDefinitionDetailsViewModel
            {
                GameDefinitionId = gameDefinitionSummary.Id,
                GameDefinitionName = gameDefinitionSummary.Name,
                TotalNumberOfGamesPlayed = gameDefinitionSummary.TotalNumberOfGamesPlayed,
                AveragePlayersPerGame = $"{gameDefinitionSummary.AveragePlayersPerGame:0.#}",
                GamingGroupId = gameDefinitionSummary.GamingGroupId,
                GamingGroupName = gameDefinitionSummary.GamingGroupName,
                UserCanEdit = (currentUser != null && gameDefinitionSummary.GamingGroupId == currentUser.CurrentGamingGroupId),
                BoardGameGeekInfo = boardGameGeekInfoViewModel
            };

            if (gameDefinitionSummary.PlayedGames == null)
            {
                viewModel.PlayedGames = new List<PlayedGameDetailsViewModel>();
            }
            else
            {
                viewModel.PlayedGames = (from playedGame in gameDefinitionSummary.PlayedGames
                                         select _playedGameDetailsViewModelBuilder.Build(playedGame, currentUser,true))
                                   .ToList();
            }

            if (!(gameDefinitionSummary.Champion is NullChampion))
            {
                viewModel.ChampionName = PlayerNameBuilder.BuildPlayerName(
                                                                           gameDefinitionSummary.Champion.Player.Name,
                                                                           gameDefinitionSummary.Champion.Player.Active);
                viewModel.ChampionPlayerId = gameDefinitionSummary.Champion.Player.Id;
                viewModel.WinPercentage = gameDefinitionSummary.Champion.WinPercentage;
                viewModel.NumberOfGamesPlayed = gameDefinitionSummary.Champion.NumberOfGames;
                viewModel.NumberOfWins = gameDefinitionSummary.Champion.NumberOfWins;
            }

            if (!(gameDefinitionSummary.PreviousChampion is NullChampion))
            {
                viewModel.PreviousChampionName = PlayerNameBuilder.BuildPlayerName(
                    gameDefinitionSummary.PreviousChampion.Player.Name,
                    gameDefinitionSummary.PreviousChampion.Player.Active);
                viewModel.PreviousChampionPlayerId = gameDefinitionSummary.PreviousChampion.Player.Id;
            }

            viewModel.GameDefinitionPlayersSummary = gameDefinitionSummary.PlayerWinRecords
                    .Select(_transformer.Transform<GameDefinitionPlayerSummaryViewModel>)
                    .ToList();

            return viewModel;
        }
        public void FixtureSetUp()
        {
            _autoMocker = new RhinoAutoMocker<GameDefinitionDetailsViewModelBuilder>();
            _autoMocker.PartialMockTheClassUnderTest();

            _expectedPlayerSummary1 = new GameDefinitionPlayerSummaryViewModel();
            _expectedPlayerSummary2 = new GameDefinitionPlayerSummaryViewModel();

            List<PlayedGame> playedGames = new List<PlayedGame>();
            playedGames.Add(new PlayedGame
            {
                    Id = 10
                });
            _playedGameDetailsViewModel1 = new PlayedGameDetailsViewModel();
            playedGames.Add(new PlayedGame
            {
                Id = 11
            });
            _playedGameDetailsViewModel2 = new PlayedGameDetailsViewModel();
            _championPlayer = new Player
            {
                Name = _championName,
                Id = _championPlayerId,
                Active = true
            };
            _previousChampionPlayer = new Player
            {
                Name = _previousChampionName,
                Id = _previousChampionPlayerId,
                Active = false
            };
            _champion = new Champion
            {
                Player = _championPlayer,
                WinPercentage = _championWinPercentage,
                NumberOfGames = _championNumberOfGames,
                NumberOfWins = _championNumberOfWins
            };
            _previousChampion = new Champion
            {
                Player = _previousChampionPlayer
            };
            _playerWinRecord1 = new PlayerWinRecord
            {
                GamesWon = 1,
                GamesLost = 2,
                PlayerName = "player name",
                WinPercentage = 33,
                PlayerId = 3
            };
            _playerWinRecord2 = new PlayerWinRecord();

            _autoMocker.Get<ITransformer>().Expect(mock => mock.Transform<GameDefinitionPlayerSummaryViewModel>(_playerWinRecord1))
                       .Return(_expectedPlayerSummary1);
            _autoMocker.Get<ITransformer>().Expect(mock => mock.Transform<GameDefinitionPlayerSummaryViewModel>(_playerWinRecord2))
                 .Return(_expectedPlayerSummary2); 

            _gameDefinitionSummary = new GameDefinitionSummary
            {
                Id = 1,
                Name = "game definition name",
                Description = "game definition description",
                GamingGroupId = _gamingGroupid,
                GamingGroupName = "gaming group name",
                PlayedGames = playedGames,
                TotalNumberOfGamesPlayed = 3,
                AveragePlayersPerGame = 2.2M,
                BoardGameGeekGameDefinitionId = 123,
                BoardGameGeekGameDefinition = new BoardGameGeekGameDefinition() { Id = 123},
                Champion = _champion,
                PreviousChampion = _previousChampion,
                PlayerWinRecords = new List<PlayerWinRecord>
                {
                    _playerWinRecord1,
                    _playerWinRecord2
                },
                BoardGameGeekInfo = new BoardGameGeekInfo()
            };
            _currentUser = new ApplicationUser
            {
                CurrentGamingGroupId = _gamingGroupid
            };
            _autoMocker.Get<IPlayedGameDetailsViewModelBuilder>().Expect(mock => mock.Build(_gameDefinitionSummary.PlayedGames[0], _currentUser, true))
                .Return(_playedGameDetailsViewModel1);
            _autoMocker.Get<IPlayedGameDetailsViewModelBuilder>().Expect(mock => mock.Build(_gameDefinitionSummary.PlayedGames[1], _currentUser, true))
                .Return(_playedGameDetailsViewModel2);

            _expectedBoardGameGeekInfo = new BoardGameGeekInfoViewModel();
            _autoMocker.Get<ITransformer>().Expect(mock => mock.Transform<BoardGameGeekInfoViewModel>(_gameDefinitionSummary.BoardGameGeekInfo))
                .Return(_expectedBoardGameGeekInfo);

            _viewModel = _autoMocker.ClassUnderTest.Build(_gameDefinitionSummary, _currentUser);
        }