public async Task AddNewGameAsync_addsNewGame()
        {
            // Arrange:
            var title            = "New game";
            var mockedGamesCount = Context.Games.Count();
            var gameToAdd        = new GameDTO {
                Title = title
            };
            var service = new GameService(Context, _mapper);

            // Act:
            await service.AddNewGameAsync(gameToAdd);

            // Assert:
            Assert.True(Context.Games.Count() == mockedGamesCount + 1);
            Assert.Contains(Context.Games, x => x.Title == title);
        }
Exemple #2
0
        public async Task <IActionResult> OnPostAsync(string[] selectedCategories)
        {
            var newGame = new GameDTO();

            if (selectedCategories != null)
            {
                newGame.GameCategories = new List <GameCategoryDTO>();
                foreach (var category in selectedCategories)
                {
                    var categoryToAdd = new GameCategoryDTO
                    {
                        CategoryId = int.Parse(category)
                    };
                    newGame.GameCategories.Add(categoryToAdd);
                }
            }

            if (await _gameService.DoesGameTitleExistAsync(Game.Title))
            {
                ToastMessage = _locService.GetLocalizedString("Game with that title is already registered!");
                ToastType    = "error";
            }
            else if (Game.BoardGameGeekId.HasValue && await _gameService.DoesBoardGameGeekIdExistAsync(Game.BoardGameGeekId.Value))
            {
                ToastMessage = _locService.GetLocalizedString("BoardGameGeek Id is already registered!");
                ToastType    = "error";
            }
            else
            {
                if (await TryUpdateModelAsync <GameDTO>(
                        newGame,
                        "game", // prefix from table in "view" part of this page
                        g => g.Title, g => g.Publisher, g => g.Description, g => g.BoardGameGeekId, g => g.SendBackAfterConvention))
                {
                    await _gameService.AddNewGameAsync(newGame);

                    ToastMessage = _locService.GetLocalizedString("Record added");
                    ToastType    = "success";
                    _logger.LogWarning($"Game '{newGame.Title}' was created by '{User.Identity.Name}'.");
                    return(RedirectToPage("./Create"));
                }
            }

            AssignedCategoryDataList = _gameService.GetAssignedCategoryData(newGame);
            return(Page());
        }