Ejemplo n.º 1
0
        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;
        }
Ejemplo n.º 2
0
 public void SetUp()
 {
     _createPlayerRequest = new CreatePlayerRequest
     {
         Name = "player name"
     };
 }
Ejemplo n.º 3
0
        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);
        }
Ejemplo n.º 4
0
        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)));
        }
Ejemplo n.º 5
0
        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);
        }
Ejemplo n.º 6
0
        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);
        }
Ejemplo n.º 7
0
        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);

        }
Ejemplo n.º 8
0
 private Player AddUserToGamingGroupAsPlayer(ApplicationUser currentUser)
 {
     var createPlayerRequest = new CreatePlayerRequest
     {
         Name = currentUser.UserName
     };
     return this.playerSaver.CreatePlayer(createPlayerRequest, currentUser, true);
 }