public Player CreatePlayer(CreatePlayerRequest createPlayerRequest, ApplicationUser applicationUser, bool linkCurrentUserToThisPlayer = false) { if (createPlayerRequest == null) { throw new ArgumentNullException(nameof(createPlayerRequest)); } ValidatePlayerNameIsNotNullOrWhiteSpace(createPlayerRequest.Name); int gamingGroupId = createPlayerRequest.GamingGroupId ?? applicationUser.CurrentGamingGroupId; ThrowPlayerAlreadyExistsExceptionIfPlayerExistsWithThisName(createPlayerRequest.Name, gamingGroupId); var newPlayer = new Player { Name = createPlayerRequest.Name, Active = true, ApplicationUserId = linkCurrentUserToThisPlayer ? applicationUser.Id : null, GamingGroupId = gamingGroupId }; newPlayer = _dataContext.Save(newPlayer, applicationUser); _dataContext.CommitAllChanges(); new Task(() => _eventTracker.TrackPlayerCreation(applicationUser)).Start(); return newPlayer; }
public void SetUp() { _createPlayerRequest = new CreatePlayerRequest { Name = "player name" }; }
public void ItReturnsANotModifiedStatusIfValidationFails() { var player = new CreatePlayerRequest(); autoMocker.ClassUnderTest.ModelState.AddModelError("key", "message"); var result = autoMocker.ClassUnderTest.Save(player, currentUser) as HttpStatusCodeResult; Assert.IsNotNull(result); Assert.AreEqual((int)HttpStatusCode.NotModified, result.StatusCode); }
public void ItSavesThePlayer() { var player = new CreatePlayerRequest { Name = "player name" }; autoMocker.ClassUnderTest.Save(player, currentUser); autoMocker.Get<IPlayerSaver>().AssertWasCalled(mock => mock.CreatePlayer( Arg<CreatePlayerRequest>.Matches( x => x.Name == player.Name && x.GamingGroupId == currentUser.CurrentGamingGroupId), Arg<ApplicationUser>.Is.Equal(currentUser), Arg<bool>.Is.Equal(false))); }
public virtual HttpResponseMessage SaveNewPlayer([FromBody]NewPlayerMessage newPlayerMessage, [FromUri]int gamingGroupId) { var requestedPlayer = new CreatePlayerRequest { Name = newPlayerMessage.PlayerName, GamingGroupId = newPlayerMessage.GamingGroupId }; var actualNewlyCreatedPlayer = playerSaver.CreatePlayer(requestedPlayer, CurrentUser); var newlyCreatedPlayerMessage = new NewlyCreatedPlayerMessage { PlayerId = actualNewlyCreatedPlayer.Id, GamingGroupId = actualNewlyCreatedPlayer.GamingGroupId }; return Request.CreateResponse(HttpStatusCode.OK, newlyCreatedPlayerMessage); }
public virtual ActionResult Save(CreatePlayerRequest createPlayerRequest, ApplicationUser currentUser) { if (!Request.IsAjaxRequest()) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } if (ModelState.IsValid) { try { createPlayerRequest.Name = createPlayerRequest.Name.Trim(); createPlayerRequest.GamingGroupId = currentUser.CurrentGamingGroupId; var player = playerSaver.CreatePlayer(createPlayerRequest, currentUser); return Json(player, JsonRequestBehavior.AllowGet); } catch (PlayerAlreadyExistsException playerAlreadyExistsException) { return new HttpStatusCodeResult(HttpStatusCode.Conflict, playerAlreadyExistsException.Message); } } return new HttpStatusCodeResult(HttpStatusCode.NotModified); }
public void ItReturnsAConflictHttpStatusCodeWhenThePlayerExists() { var player = new CreatePlayerRequest { Name = "player name" }; autoMocker.Get<IPlayerSaver>().Expect(x => x.CreatePlayer(player, currentUser)) .Repeat.Once() .Throw(new PlayerAlreadyExistsException(player.Name, 1)); var result = autoMocker.ClassUnderTest.Save(player, currentUser) as HttpStatusCodeResult; Assert.IsNotNull(result); Assert.AreEqual((int)HttpStatusCode.Conflict, result.StatusCode); }
private Player AddUserToGamingGroupAsPlayer(ApplicationUser currentUser) { var createPlayerRequest = new CreatePlayerRequest { Name = currentUser.UserName }; return this.playerSaver.CreatePlayer(createPlayerRequest, currentUser, true); }