public void ShouldCreateGame() { var platformTypes = new List <PlatformType> { new PlatformType(), new PlatformType() }; var publishers = new List <Publisher> { new Publisher(), new Publisher() }; var genres = new List <Genre> { new Genre(), new Genre() }; _unitOfWorkMock.Setup(m => m.PlatformTypes.Find(It.IsAny <IPipeline <PlatformType> >())) .Returns(platformTypes); _unitOfWorkMock.Setup(m => m.Publishers.Find(It.IsAny <IPipeline <Publisher> >())) .Returns(publishers); _unitOfWorkMock.Setup(m => m.Genres.Find(It.IsAny <IPipeline <Genre> >())) .Returns(genres); _unitOfWorkMock.Setup(m => m.Games.Create(It.IsAny <Game>())); _unitOfWorkMock.Setup(m => m.Save()); var game = new CreatingGameDto { Key = "Key", LanguagesNames = new Dictionary <string, string> { { "ru", "имя" } }, LanguagesDescriptions = new Dictionary <string, string> { { "ru", "описание" } }, Price = 10, UnitsInStock = 2, Discounted = false, PublishingDate = new DateTime(2005, 5, 5), GenreIds = new List <int> { 1, 2 }, PlatformTypeIds = new List <int> { 1, 2 }, PublisherIds = new List <int> { 1, 2 } }; _service.Create(game); _unitOfWorkMock.Verify(m => m.Games.Create(It.IsAny <Game>()), Times.AtLeastOnce); _unitOfWorkMock.Verify(m => m.Save(), Times.AtLeastOnce); }
public int Create(CreatingGameDto creatingGame) { CheckGameTranslationArgumentsValidity(creatingGame.LanguagesNames, creatingGame.LanguagesDescriptions); ICollection <Genre> fullGenreTree; if (creatingGame.GenreIds.Any()) { ICollection <Genre> selectedGenres = GetGenresFromIds(creatingGame.GenreIds); fullGenreTree = BuildGenreTree(selectedGenres); } else { fullGenreTree = new List <Genre> { GetDefaultGenre() }; } Game game = UnitOfWork.Games.SingleOrDefaultDeleted(g => g.Key == creatingGame.Key); if (game == null) { game = new Game { Key = creatingGame.Key }; MapperToModel.MapToModel( creatingGame, game, GetPublishersFromIds(creatingGame.PublisherIds), GetPlatformTypesFromIds(creatingGame.PlatformTypeIds), fullGenreTree.ToList()); AddTranslationsToGame(game, creatingGame.LanguagesNames, creatingGame.LanguagesDescriptions); UnitOfWork.Games.Create(game); } else { MapperToModel.MapToModel( creatingGame, game, GetPublishersFromIds(creatingGame.PublisherIds), GetPlatformTypesFromIds(creatingGame.PlatformTypeIds), fullGenreTree.ToList()); SetTranslationsForGame(game, creatingGame.LanguagesNames, creatingGame.LanguagesDescriptions); UnitOfWork.Games.Update(game); } UnitOfWork.Save(); return(game.Id); }
public void Create_NewGame_ReturnsTrue() { string key = "1qw2"; var game = new CreatingGameDto() { Key = key }; var games = new List <Game>(); _unitOfWorMock.Setup(uof => uof.GameGenericRepository.Find(It.IsAny <Expression <Func <Game, bool> > >())).Returns(games); _unitOfWorMock.Setup(uof => uof.GenreGenericRepository.Find(It.IsAny <Expression <Func <Genre, bool> > >())).Returns(new List <Genre>()); _unitOfWorMock.Setup(uof => uof.PlatformTypeGenericRepository.Find(It.IsAny <Expression <Func <PlatformType, bool> > >())).Returns(new List <PlatformType>()); var result = _sut.Create(game); Assert.True(result.Succeed); }
public OptionalDetails Update(CreatingGameDto gameDto) { var ganres = _unitOfWork.GenreGenericRepository.Find(g => gameDto.GenresIds.Contains(g.Id)).ToList(); var platforms = _unitOfWork.PlatformTypeGenericRepository.Find(p => gameDto.PlatformTypesIds.Contains(p.Id)).ToList(); var game = _unitOfWork.GameGenericRepository.Find(g => g.Id == gameDto.Id).First(); game.Genres.Clear(); game.PlatformTypes.Clear(); game.Genres = ganres; game.PlatformTypes = platforms; _unitOfWork.GameGenericRepository.Update(game); _unitOfWork.Commit(); _logger.Info($"Updated existing game: id = {gameDto.Id}, name = {gameDto.Name}, key = {gameDto.Key}, description = {gameDto.Description}"); return(new OptionalDetails("The game was successfully updated", true)); }
public OptionalDetails Create(CreatingGameDto gameDto) { int gameExist = _unitOfWork.GameGenericRepository.Find(g => g.Key == gameDto.Key).Count(); if (gameExist > 0) { return(new OptionalDetails("The game with this key already exist", false)); } var ganres = _unitOfWork.GenreGenericRepository.Find(g => gameDto.GenresIds.Contains(g.Id)).ToList(); var platforms = _unitOfWork.PlatformTypeGenericRepository.Find(p => gameDto.PlatformTypesIds.Contains(p.Id)).ToList(); var game = _mapper.Map <Game>(gameDto); game.Genres = ganres; game.PlatformTypes = platforms; _unitOfWork.GameGenericRepository.Create(game); _unitOfWork.Commit(); _logger.Info($"Created the game: name = {gameDto.Name}, key = {gameDto.Key}, descriotion = {gameDto.Description} "); return(new OptionalDetails("Game was created", true)); }
public static void MapToModel( CreatingGameDto game, Game model, ICollection <Publisher> publishers, ICollection <PlatformType> platformTypes, ICollection <Genre> genres) { model.Genres.Clear(); model.PlatformTypes.Clear(); model.Publishers.Clear(); model.Comments.Clear(); model.Deleted = false; model.Name = game.LanguagesNames["ru"]; model.Description = game.LanguagesDescriptions["ru"]; model.Price = game.Price; model.UnitsInStock = game.UnitsInStock; model.Discounted = game.Discounted; model.PublishingDate = game.PublishingDate; model.Publishers = publishers; model.PlatformTypes = platformTypes; model.Genres = genres; }
public void Update_ExistingGame_UpdatingTheKey() { string key = "1qw2"; string newKey = "new111"; var game = new CreatingGameDto() { Id = 1, Key = key }; var games = new List <Game>() { new Game() { Id = 1, Key = newKey, Genres = new List <Genre>(), PlatformTypes = new List <PlatformType>() } }; _unitOfWorMock.Setup(uof => uof.GenreGenericRepository.Find(It.IsAny <Expression <Func <Genre, bool> > >())).Returns(new List <Genre>()); _unitOfWorMock.Setup(uof => uof.PlatformTypeGenericRepository.Find(It.IsAny <Expression <Func <PlatformType, bool> > >())).Returns(new List <PlatformType>()); _unitOfWorMock.Setup(uof => uof.GameGenericRepository.Find(It.IsAny <Expression <Func <Game, bool> > >())).Returns(games); var result = _sut.Update(game); Assert.True(result.Succeed); }