Beispiel #1
0
        public HttpResponseMessage Put(GameAddUpdateViewModel gameAddUpdateViewModel)
        {
            GameLocalizationViewModel englishLocalization = GetLocalization(gameAddUpdateViewModel, "en");

            if (IsLocalizationEmpty(englishLocalization))
            {
                ModelState.AddModelError("LocalizationError", GlobalRes.EnglishLocalizationShouldExist);
            }

            if (ModelState.IsValid)
            {
                CleanEmptyLocalizations(gameAddUpdateViewModel);

                var gameModel = Mapper.Map <GameModel>(gameAddUpdateViewModel);

                if (!_gameService.GameExists(gameModel.Key))
                {
                    _gameService.Add(gameModel);
                }
                else
                {
                    _gameService.Update(gameModel);
                }

                return(new HttpResponseMessage(HttpStatusCode.OK));
            }

            return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState));
        }
Beispiel #2
0
        private static void CleanEmptyLocalizations(GameAddUpdateViewModel gameAddUpdateViewModel)
        {
            List <GameLocalizationViewModel> emptyLocalizations =
                gameAddUpdateViewModel.GameLocalizations.Where(IsLocalizationEmpty).ToList();

            foreach (GameLocalizationViewModel gameLocalizationViewModel in emptyLocalizations)
            {
                gameAddUpdateViewModel.GameLocalizations.Remove(gameLocalizationViewModel);
            }
        }
Beispiel #3
0
        public ActionResult AddGame()
        {
            var gameAddUpdateViewModel = new GameAddUpdateViewModel();

            gameAddUpdateViewModel.AddedDate = DateTime.Now;

            AdjustCollections(gameAddUpdateViewModel);

            AdjustLocalizations(gameAddUpdateViewModel);

            return(View(gameAddUpdateViewModel));
        }
Beispiel #4
0
        private static GameLocalizationViewModel GetLocalization(GameAddUpdateViewModel gameAddUpdateViewModel, string languageCode)
        {
            if (gameAddUpdateViewModel == null || gameAddUpdateViewModel.GameLocalizations == null)
            {
                return(null);
            }

            GameLocalizationViewModel result = gameAddUpdateViewModel.GameLocalizations
                                               .FirstOrDefault(loc => String.Equals(loc.Language.Code, languageCode, StringComparison.CurrentCultureIgnoreCase));

            return(result);
        }
        public void Check_That_Right_Method_Was_Called_Inside_Update_Action()
        {
            // Arrange
            var mockGameService = new Mock <IGameService>();

            mockGameService.Setup(m => m.Update(It.IsAny <GameModel>())).Verifiable();

            GameController gameController = GetGameController(mockGameService);

            GameAddUpdateViewModel testAddUpdateViewModel = GetGameAddUpdateViewModel();

            // Act
            gameController.UpdateGame(testAddUpdateViewModel);

            // Assert
            mockGameService.Verify(m => m.Update(It.IsAny <GameModel>()));
        }
Beispiel #6
0
        private void AdjustLocalizations(GameAddUpdateViewModel gameAddUpdateViewModel)
        {
            IEnumerable <LanguageModel> languages = _languageService.GetAll();

            gameAddUpdateViewModel.GameLocalizations = gameAddUpdateViewModel.GameLocalizations
                                                       ?? new List <GameLocalizationViewModel>();

            foreach (LanguageModel languageModel in languages)
            {
                if (GetLocalization(gameAddUpdateViewModel, languageModel.Code) == null)
                {
                    var gameLocalization = new GameLocalizationViewModel
                    {
                        LanguageId = languageModel.LanguageId,
                        Language   = Mapper.Map <LanguageViewModel>(languageModel),
                    };

                    gameAddUpdateViewModel.GameLocalizations.Add(gameLocalization);
                }
            }
        }
Beispiel #7
0
        public ActionResult UpdateGame(GameAddUpdateViewModel gameAddUpdateViewModel)
        {
            GameLocalizationViewModel englishLocalization = GetLocalization(gameAddUpdateViewModel, "en");

            if (IsLocalizationEmpty(englishLocalization))
            {
                ModelState.AddModelError("LocalizationError", GlobalRes.EnglishLocalizationShouldExist);
            }

            if (ModelState.IsValid)
            {
                CleanEmptyLocalizations(gameAddUpdateViewModel);

                var gameModel = Mapper.Map <GameModel>(gameAddUpdateViewModel);
                _gameService.Update(gameModel);
                MessageSuccess("The game has been updated successfully!");
                return(RedirectToAction("Get"));
            }

            return(View(gameAddUpdateViewModel));
        }
        private static GameAddUpdateViewModel GetGameAddUpdateViewModel()
        {
            var gameViewModel = new GameAddUpdateViewModel
            {
                GameId                   = 1,
                Key                      = "key",
                Price                    = (decimal)5.0,
                Discontinued             = false,
                Publisher                = new PublisherViewModel(),
                SelectedGenresIds        = new List <int>(),
                SelectedPlatformTypesIds = new List <int>(),
                SelectedPublisherId      = 1,
                UnitsInStock             = 50,
                AddedDate                = DateTime.UtcNow,
                PublicationDate          = DateTime.UtcNow,
                Publishers               = new List <PublisherViewModel>(),
                PlatformTypes            = new List <PlatformTypeViewModel>(),
                Genres                   = new List <GenreViewModel>(),
                GameLocalizations        = new List <GameLocalizationViewModel>
                {
                    new GameLocalizationViewModel
                    {
                        Name        = "name",
                        Description = "description",
                        LanguageId  = 1,
                        Language    = new LanguageViewModel
                        {
                            Name       = "English",
                            Code       = "en",
                            LanguageId = 1,
                        }
                    }
                }
            };

            return(gameViewModel);
        }
Beispiel #9
0
 private void AdjustCollections(GameAddUpdateViewModel gameAddUpdateViewModel)
 {
     gameAddUpdateViewModel.PlatformTypes = Mapper.Map <IEnumerable <PlatformTypeViewModel> >(_platformTypeService.GetAll());
     gameAddUpdateViewModel.Genres        = Mapper.Map <IEnumerable <GenreViewModel> >(_genreService.GetAll());
     gameAddUpdateViewModel.Publishers    = Mapper.Map <IEnumerable <PublisherViewModel> >(_publisherService.GetAll());
 }