Esempio 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;
        }
Esempio n. 2
0
 public void SetUp()
 {
     _createPlayerRequest = new CreatePlayerRequest
     {
         Name = "player name"
     };
 }
Esempio 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);
        }
Esempio 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)));
        }
Esempio 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);
        }
Esempio 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);
        }
Esempio 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);

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