public void CreateClub(Club club)
        {
            _connection.Execute($@"INSERT INTO {Tables.Club} 
                    (ClubId, MemberGroupKey, MemberGroupName, ClubRoute)
                    VALUES
                    (@ClubId, @MemberGroupKey, @MemberGroupName, @ClubRoute)",
                                new
            {
                club.ClubId,
                club.MemberGroupKey,
                club.MemberGroupName,
                club.ClubRoute
            });

            _connection.Execute($@"INSERT INTO {Tables.ClubVersion}
                    (ClubVersionId, ClubId, ClubName, ComparableName, FromDate, UntilDate)
                    VALUES
                    (@ClubVersionId, @ClubId, @ClubName, @ComparableName, @FromDate, @UntilDate)",
                                new
            {
                ClubVersionId = Guid.NewGuid(),
                club.ClubId,
                club.ClubName,
                ComparableName = club.ComparableName(),
                FromDate       = new DateTimeOffset(2000, 1, 1, 0, 0, 0, TimeSpan.Zero),
                UntilDate      = club.UntilDate?.UtcDateTime
            });
        }
        public async Task Update_club_does_not_add_club_version_if_name_is_unchanged()
        {
            var club      = _databaseFixture.TestData.Teams.Where(x => x.Club != null).First().Club;
            var auditable = new Club
            {
                ClubId    = club.ClubId,
                ClubName  = club.ClubName,
                ClubRoute = club.ClubRoute
            };

            int?existingVersions = null;

            (Guid? clubVersionId, string clubName, string comparableName, DateTimeOffset? fromDate)currentVersion = (null, null, null, null);
            using (var connection = _databaseFixture.ConnectionFactory.CreateDatabaseConnection())
            {
                existingVersions = await connection.ExecuteScalarAsync <int>($"SELECT COUNT(*) FROM {Tables.ClubVersion} WHERE ClubId = @ClubId", auditable).ConfigureAwait(false);

                currentVersion = await connection.QuerySingleAsync <(Guid clubVersionId, string clubName, string comparableName, DateTimeOffset?fromDate)>(
                    $"SELECT ClubVersionId, ClubName, ComparableName, FromDate FROM {Tables.ClubVersion} WHERE ClubId = @ClubId AND UntilDate IS NULL", auditable).ConfigureAwait(false);
            }

            var routeGenerator = new Mock <IRouteGenerator>();

            routeGenerator.Setup(x => x.GenerateUniqueRoute(club.ClubRoute, "/clubs", club.ClubName, NoiseWords.ClubRoute, It.IsAny <Func <string, Task <int> > >())).Returns(Task.FromResult(club.ClubRoute));
            var copier = new Mock <IStoolballEntityCopier>();

            copier.Setup(x => x.CreateAuditableCopy(club)).Returns(auditable);

            var repo = new SqlServerClubRepository(_databaseFixture.ConnectionFactory, Mock.Of <IAuditRepository>(), Mock.Of <ILogger>(), routeGenerator.Object, Mock.Of <IRedirectsRepository>(), copier.Object);

            var updated = await repo.UpdateClub(club, Guid.NewGuid(), "Person 1").ConfigureAwait(false);

            using (var connection = _databaseFixture.ConnectionFactory.CreateDatabaseConnection())
            {
                var totalVersions = await connection.ExecuteScalarAsync <int>($"SELECT COUNT(*) FROM {Tables.ClubVersion} WHERE ClubId = @ClubId", auditable).ConfigureAwait(false);

                Assert.Equal(existingVersions, totalVersions);

                var versionResult = await connection.QuerySingleAsync <(Guid clubVersionId, string clubName, string comparableName, DateTimeOffset?fromDate)>(
                    $"SELECT ClubVersionId, ClubName, ComparableName, FromDate FROM {Tables.ClubVersion} WHERE ClubId = @ClubId AND UntilDate IS NULL", auditable).ConfigureAwait(false);

                Assert.Equal(currentVersion.clubVersionId, versionResult.clubVersionId);
                Assert.Equal(auditable.ClubName, versionResult.clubName);
                Assert.Equal(auditable.ComparableName(), versionResult.comparableName);
                Assert.Equal(currentVersion.fromDate.Value, versionResult.fromDate.Value);
            }
        }
        public async Task Update_club_updates_name_and_route()
        {
            var club      = _databaseFixture.TestData.Teams.Where(x => x.Club != null).First().Club;
            var auditable = new Club
            {
                ClubId    = club.ClubId,
                ClubName  = club.ClubName + Guid.NewGuid(),
                ClubRoute = club.ClubRoute,
                Teams     = club.Teams.Select(x => new Team {
                    TeamId = x.TeamId
                }).ToList()
            };
            var routeGenerator = new Mock <IRouteGenerator>();

            routeGenerator.Setup(x => x.GenerateUniqueRoute(club.ClubRoute, "/clubs", auditable.ClubName, NoiseWords.ClubRoute, It.IsAny <Func <string, Task <int> > >())).Returns(Task.FromResult(club.ClubRoute + "-123"));
            var copier = new Mock <IStoolballEntityCopier>();

            copier.Setup(x => x.CreateAuditableCopy(club)).Returns(auditable);

            var repo       = new SqlServerClubRepository(_databaseFixture.ConnectionFactory, Mock.Of <IAuditRepository>(), Mock.Of <ILogger>(), routeGenerator.Object, Mock.Of <IRedirectsRepository>(), copier.Object);
            var memberKey  = Guid.NewGuid();
            var memberName = "Person 1";

            var updatedClub = await repo.UpdateClub(club, memberKey, memberName).ConfigureAwait(false);

            using (var connection = _databaseFixture.ConnectionFactory.CreateDatabaseConnection())
            {
                var clubResult = await connection.QuerySingleOrDefaultAsync <Club>($"SELECT ClubRoute FROM {Tables.Club} WHERE ClubId = @ClubId", new { updatedClub.ClubId }).ConfigureAwait(false);

                Assert.NotNull(clubResult);
                Assert.Equal(club.ClubRoute + "-123", clubResult.ClubRoute);

                var versionResult = await connection.QuerySingleOrDefaultAsync <ClubVersion>($"SELECT ClubName, ComparableName FROM {Tables.ClubVersion} WHERE ClubId = @ClubId", new { updatedClub.ClubId }).ConfigureAwait(false);

                Assert.NotNull(versionResult);
                Assert.Equal(auditable.ClubName, versionResult.ClubName);
                Assert.Equal(auditable.ComparableName(), versionResult.ComparableName);
            }
        }
        public async Task Create_club_succeeds()
        {
            var club = new Club
            {
                ClubName        = "New club " + Guid.NewGuid(),
                MemberGroupKey  = Guid.NewGuid(),
                MemberGroupName = "Test group"
            };

            var route          = "/clubs/" + Guid.NewGuid();
            var routeGenerator = new Mock <IRouteGenerator>();

            routeGenerator.Setup(x => x.GenerateUniqueRoute("/clubs", club.ClubName, NoiseWords.ClubRoute, It.IsAny <Func <string, Task <int> > >())).Returns(Task.FromResult(route));
            var copier = new Mock <IStoolballEntityCopier>();

            copier.Setup(x => x.CreateAuditableCopy(club)).Returns(club);

            var repo = new SqlServerClubRepository(_databaseFixture.ConnectionFactory, Mock.Of <IAuditRepository>(), Mock.Of <ILogger>(), routeGenerator.Object, Mock.Of <IRedirectsRepository>(), copier.Object);

            var createdClub = await repo.CreateClub(club, Guid.NewGuid(), "Person 1").ConfigureAwait(false);

            using (var connection = _databaseFixture.ConnectionFactory.CreateDatabaseConnection())
            {
                var clubResult = await connection.QuerySingleOrDefaultAsync <Club>($"SELECT ClubId, MemberGroupKey, MemberGroupName, ClubRoute FROM {Tables.Club} WHERE ClubId = @ClubId", new { createdClub.ClubId }).ConfigureAwait(false);

                Assert.NotNull(clubResult);
                Assert.Equal(club.MemberGroupKey, clubResult.MemberGroupKey);
                Assert.Equal(club.MemberGroupName, clubResult.MemberGroupName);
                Assert.Equal(club.ClubRoute, clubResult.ClubRoute);

                var versionResult = await connection.QuerySingleOrDefaultAsync <ClubVersion>($"SELECT ClubName, ComparableName, FromDate FROM {Tables.ClubVersion} WHERE ClubId = @ClubId", new { createdClub.ClubId }).ConfigureAwait(false);

                Assert.NotNull(versionResult);
                Assert.Equal(club.ClubName, versionResult.ClubName);
                Assert.Equal(club.ComparableName(), versionResult.ComparableName);
                Assert.Equal(DateTime.UtcNow.Date, versionResult.FromDate.Date);
            }
        }