Пример #1
0
        public long CreateLeague(string name, long userId, SportType sport)
        {
            if (string.IsNullOrEmpty(name))
            {
                throw new BusinessException(ErrorCodes.LeagueNameRequired, "League name is required.");
            }

            var leagueName = name.Trim();

            if (!IsValidateLeagueName(leagueName))
            {
                throw new BusinessException(ErrorCodes.InvalidLeagueName, "League name must be between 5 and 50 alphanumeric characters");
            }

            var league = new League
                {
                    CreatorUserId = userId,
                    Name = leagueName,
                    OwnerUserId = userId,
                    Sport = sport
                };

            var leagueId = _leagueRepository.CreateLeague(league);

            _leagueRepository.AddMember(leagueId, userId);

            return leagueId;
        }
Пример #2
0
        public long CreateLeague(League league)
        {
            using (var conn = GetConnection())
            {
                var cmd = conn.CreateCommand();

                cmd.CommandText = INSERT_LEAGUE;
                cmd.Parameters.AddWithValue("@name", league.Name);
                cmd.Parameters.AddWithValue("@userId", league.CreatorUserId);
                cmd.Parameters.AddWithValue("@sportId", league.Sport);
                cmd.Connection.Open();
                cmd.ExecuteNonQuery();

                return cmd.LastInsertedId;
            }
        }
Пример #3
0
        public void LeagueOwnerCannotModifyTheirOwnStatus()
        {
            var leagueId = 1;
            var ownerUserId = 2;
            var league = new League
                {
                    Id = leagueId,
                    OwnerUserId = ownerUserId
                };

            _leagueRepository
                .Setup(repository => repository.Get(leagueId))
                .Returns(league);

            var exception = Assert.Throws<BusinessException>(() => _leagueManager.UpdateMemberStatus(leagueId, ownerUserId, ownerUserId, false));

            Assert.Equal(ErrorCodes.LeagueOwnerCannotModifyMemberStatus, exception.ErrorCode);
        }
Пример #4
0
        public void UpdatingOwnerMustTransferToValidMember()
        {
            var leagueId = 1;
            var currentOwnerId = 99;
            var newOwnerId = 55;
            var league = new League
                {
                    Id = leagueId,
                    OwnerUserId = currentOwnerId
                };

            _leagueRepository
                .Setup(repository => repository.Get(leagueId))
                .Returns(league);

            var members = new List<LeagueMember>()
                {
                    new LeagueMember { Id = 1, UserId = currentOwnerId },
                    new LeagueMember { Id = 2, UserId = 789 }
                };

            _leagueRepository
                .Setup(repository => repository.GetMembers(leagueId))
                .Returns(members);

            var exception = Assert.Throws<BusinessException>(() => _leagueManager.UpdateLeagueOwner(leagueId, currentOwnerId, newOwnerId));

            Assert.Equal(ErrorCodes.LeagueMemberDoesNotExist, exception.ErrorCode);
        }
Пример #5
0
        public void UpdatingOwnerMustBePerformedByOwner()
        {
            var leagueId = 1;
            var requestingUserId = 99;
            var currentOwnerUserId = 33;
            var newOwnerId = 55;
            var league = new League
                {
                    Id = leagueId,
                    OwnerUserId = currentOwnerUserId
                };

            _leagueRepository
                .Setup(repository => repository.Get(leagueId))
                .Returns(league);

            var members = new List<LeagueMember>()
                {
                    new LeagueMember { Id = 1, UserId = currentOwnerUserId },
                    new LeagueMember { Id = 1, UserId = newOwnerId }
                };

            _leagueRepository
                .Setup(repository => repository.GetMembers(leagueId))
                .Returns(members);

            var exception = Assert.Throws<BusinessException>(() => _leagueManager.UpdateLeagueOwner(leagueId, requestingUserId, newOwnerId));

            Assert.Equal(ErrorCodes.NotAuthorizedLeagueUpdate, exception.ErrorCode);
        }
Пример #6
0
        public void UpdatingOwnerCanBePerformedByOwner()
        {
            var leagueId = 1;
            var currentOwnerId = 99;
            var newOwnerId = 55;
            var league = new League
                {
                    Id = leagueId,
                    OwnerUserId = currentOwnerId
                };

            _leagueRepository
                .Setup(repository => repository.Get(leagueId))
                .Returns(league);

            var members = new List<LeagueMember>()
                {
                    new LeagueMember { Id = 1, UserId = currentOwnerId },
                    new LeagueMember { Id = 1, UserId = newOwnerId }
                };

            _leagueRepository
                .Setup(repository => repository.GetMembers(leagueId))
                .Returns(members);

            _leagueManager.UpdateLeagueOwner(leagueId, currentOwnerId, newOwnerId);

            _leagueRepository.Verify(repository => repository.UpdateLeagueOwner(leagueId, currentOwnerId, newOwnerId));
        }
Пример #7
0
        public void UpdatingNameMustBePerformedByOwner()
        {
            var leagueId = 1;
            var ownerId = 99;
            var league = new League
                {
                    Id = leagueId,
                    OwnerUserId = 33
                };

            _leagueRepository
                .Setup(repository => repository.Get(leagueId))
                .Returns(league);

            var exception = Assert.Throws<BusinessException>(() => _leagueManager.UpdateLeagueName(leagueId, ownerId, "new name"));

            Assert.Equal(ErrorCodes.NotAuthorizedLeagueUpdate, exception.ErrorCode);
        }
Пример #8
0
        public void UpdatingMemberStatusMustBePerformedBySelfOrOwner()
        {
            var leagueId = 1;
            var authenticatedUserId = 2;
            var userIdToModify = 3;
            var league = new League
                {
                    Id = leagueId,
                    OwnerUserId = 5
                };

            _leagueRepository
                .Setup(repository => repository.Get(leagueId))
                .Returns(league);

            var exception = Assert.Throws<BusinessException>(() => _leagueManager.UpdateMemberStatus(leagueId, authenticatedUserId, userIdToModify, false));

            Assert.Equal(ErrorCodes.NotAuthorizedLeagueUpdate, exception.ErrorCode);
        }
Пример #9
0
        public void UpdatingMemberStatusCanBePerformedBySelf()
        {
            var leagueId = 1;
            var ownerUserId = 2;
            var authenticatedUserId = 3;
            var userIdToModify = 3;
            var league = new League
                {
                    Id = leagueId,
                    OwnerUserId = ownerUserId
                };

            _leagueRepository
                .Setup(repository => repository.Get(leagueId))
                .Returns(league);

            var leagueMembers = new List<LeagueMember>();

            leagueMembers.Add(new LeagueMember
                {
                    Id = 1,
                    UserId = authenticatedUserId
                });
            leagueMembers.Add(new LeagueMember
                {
                    Id = 1,
                    UserId = ownerUserId
                });
            leagueMembers.Add(new LeagueMember
                {
                    Id = 1,
                    UserId = userIdToModify
                });

            _leagueRepository
                .Setup(repository => repository.GetMembers(leagueId))
                .Returns(leagueMembers);

            _leagueRepository
                .Setup(repository => repository.UpdateMemberStatus(leagueId, userIdToModify, false))
                .Verifiable();

            _leagueManager.UpdateMemberStatus(leagueId, authenticatedUserId, userIdToModify, false);

            _leagueRepository
                .Verify(repository => repository.UpdateMemberStatus(leagueId, userIdToModify, false), Times.Once);
        }