public void ItReloadsTheCurrentGameDefinitionIfValidationFails()
		{
			var createGameDefinitionRequest = new CreateGameDefinitionViewModel();
			autoMocker.ClassUnderTest.ModelState.AddModelError("key", "message");

			ViewResult actionResult = autoMocker.ClassUnderTest.Create(createGameDefinitionRequest, currentUser) as ViewResult;
			var actualViewModel = (CreateGameDefinitionViewModel)actionResult.ViewData.Model;

			Assert.AreSame(createGameDefinitionRequest, actualViewModel);
		}
        public void ItReturnsABadRequestWithTheTheFirstModelStateErrorValidationMessageIfValidationFails()
        {
            var model = new CreateGameDefinitionViewModel();
            const string MESSAGE = "some message";
            autoMocker.ClassUnderTest.ModelState.AddModelError("key", MESSAGE);

            var result = autoMocker.ClassUnderTest.AjaxCreate(model, currentUser) as HttpStatusCodeResult;

            Assert.AreEqual((int)HttpStatusCode.BadRequest, result.StatusCode);
            Assert.AreEqual(result.StatusDescription, MESSAGE);
        }
		public void ItRedirectsToTheGamingGroupIndexAndGameDefinitionSectionAfterSaving()
		{
			string baseUrl = "base url";
			string expectedUrl = baseUrl + "#" + GamingGroupController.SECTION_ANCHOR_GAMEDEFINITIONS;
			var createGameDefinitionRequest = new CreateGameDefinitionViewModel()
			{
				Name = "game definition name"
			};
			autoMocker.ClassUnderTest.Url.Expect(mock => mock.Action(MVC.GamingGroup.ActionNames.Index, MVC.GamingGroup.Name))
					.Return(baseUrl);

			RedirectResult redirectResult = autoMocker.ClassUnderTest.Create(createGameDefinitionRequest, currentUser) as RedirectResult;

			Assert.AreEqual(expectedUrl, redirectResult.Url);
		}
		public void ItSavesTheGameDefinitionIfValidationPasses()
		{
			var createGameDefinitionRequest = new CreateGameDefinitionViewModel()
			{
				Name = "game definition name"
			};

            autoMocker.ClassUnderTest.Create(createGameDefinitionRequest, currentUser);

            autoMocker.Get<IGameDefinitionSaver>().AssertWasCalled(mock => mock.CreateGameDefinition(
                Arg<CreateGameDefinitionRequest>.Matches(x => x.Name == createGameDefinitionRequest.Name
                                            && x.Description == createGameDefinitionRequest.Description
                                            && x.BoardGameGeekGameDefinitionId == createGameDefinitionRequest.BoardGameGeekGameDefinitionId), 
                Arg<ApplicationUser>.Is.Same(currentUser)));
		}
        public void ItCreatesTheNewGame()
        {
            var createGameDefinitionViewModel = new CreateGameDefinitionViewModel()
            {
                Active = false,
                BoardGameGeekGameDefinitionId = 1,
                Description = "some description",
                Name = "New Game"
            };

            autoMocker.ClassUnderTest.AjaxCreate(createGameDefinitionViewModel, currentUser);

            var arguments = autoMocker.Get<IGameDefinitionSaver>().GetArgumentsForCallsMadeOn(mock => mock.CreateGameDefinition(
                Arg<CreateGameDefinitionRequest>.Is.Anything, 
                Arg<ApplicationUser>.Is.Anything));
            var createGameDefinitionRequest = arguments[0][0] as CreateGameDefinitionRequest;
            Assert.That(createGameDefinitionRequest, Is.Not.Null);
            Assert.That(createGameDefinitionRequest.Active, Is.EqualTo(createGameDefinitionViewModel.Active));
            Assert.That(createGameDefinitionRequest.BoardGameGeekGameDefinitionId, Is.EqualTo(createGameDefinitionViewModel.BoardGameGeekGameDefinitionId));
            Assert.That(createGameDefinitionRequest.Description, Is.EqualTo(createGameDefinitionViewModel.Description));
            Assert.That(createGameDefinitionRequest.Name, Is.EqualTo(createGameDefinitionViewModel.Name));
        }
		public void ItRedirectsBackToThePlayedGameCreateWhenSentFromThere()
		{
		    int expectedGameDefinitionId = 123;
			string returnUrl = "/PlayedGame/Create";
		    var createGameDefinitionRequest = new CreateGameDefinitionViewModel()
		    {
                ReturnUrl = returnUrl,
                Name = "Project-Ariel"
		    };
		    autoMocker.Get<IGameDefinitionSaver>().Expect(mock => mock.CreateGameDefinition(Arg<CreateGameDefinitionRequest>.Is.Anything, Arg<ApplicationUser>.Is.Anything))
		                             .Return(new GameDefinition 
		                             {
		                                 Id = expectedGameDefinitionId
		                             });
            string expectedUrl = returnUrl + "?gameId=" + expectedGameDefinitionId;

            RedirectResult redirectResult = autoMocker.ClassUnderTest.Create(createGameDefinitionRequest, currentUser) as RedirectResult;

			Assert.AreEqual(expectedUrl, redirectResult.Url);
		}
        public virtual ActionResult AjaxCreate(CreateGameDefinitionViewModel createGameDefinitionViewModel, ApplicationUser currentUser)
        {
            if (!Request.IsAjaxRequest())
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }

            if (ModelState.IsValid)
            {
                createGameDefinitionViewModel.Name = createGameDefinitionViewModel.Name.Trim();
                var createGameDefinitionRequest = Mapper.Map<CreateGameDefinitionRequest>(createGameDefinitionViewModel);

                try
                {
                    _gameDefinitionSaver.CreateGameDefinition(createGameDefinitionRequest, currentUser);
                }
                catch (DuplicateKeyException)
                {
                    return new HttpStatusCodeResult(HttpStatusCode.BadRequest, "This Game Definition is already active within your Gaming Group.");
                }
                return Json(currentUser.CurrentGamingGroupId, JsonRequestBehavior.AllowGet);
            }

            string errorDescription = string.Empty;

            errorDescription = GetFirstModelStateError(errorDescription);
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest, errorDescription);
        }
        public virtual ActionResult Create(CreateGameDefinitionViewModel createGameDefinitionViewModel, ApplicationUser currentUser)
        {
            if (ModelState.IsValid)
            {
                createGameDefinitionViewModel.Name = createGameDefinitionViewModel.Name.Trim();
                var gameDefinition = Mapper.Map<CreateGameDefinitionViewModel, CreateGameDefinitionRequest>(createGameDefinitionViewModel);

                var savedResult = _gameDefinitionSaver.CreateGameDefinition(gameDefinition, currentUser);

                if (!String.IsNullOrWhiteSpace(createGameDefinitionViewModel.ReturnUrl))
                    return new RedirectResult(createGameDefinitionViewModel.ReturnUrl + "?gameId=" + savedResult.Id);

                return new RedirectResult(Url.Action(MVC.GamingGroup.ActionNames.Index, MVC.GamingGroup.Name)
                                        + "#" + GamingGroupController.SECTION_ANCHOR_GAMEDEFINITIONS);
            }

            return View(MVC.GameDefinition.Views.Create, createGameDefinitionViewModel);
        }
        public void ItReturnsAFriendlyMessageIfThereIsADuplicateKeyException()
        {
            var model = new CreateGameDefinitionViewModel
            {
                Name = "some name"
            };
            autoMocker.Get<IGameDefinitionSaver>().Expect(mock => mock.CreateGameDefinition(null, null))
                .IgnoreArguments()
                .Throw(new DuplicateKeyException("some duplicate key"));

            var result = autoMocker.ClassUnderTest.AjaxCreate(model, currentUser) as HttpStatusCodeResult;

            Assert.AreEqual((int)HttpStatusCode.BadRequest, result.StatusCode);
            Assert.AreEqual("This Game Definition is already active within your Gaming Group.", result.StatusDescription);
        }