Skip to content

Commit

Permalink
Replace AppDb references with IAppDb
Browse files Browse the repository at this point in the history
  • Loading branch information
axunonb committed Jan 21, 2024
1 parent 439d978 commit e0624f9
Show file tree
Hide file tree
Showing 27 changed files with 103 additions and 40 deletions.
2 changes: 1 addition & 1 deletion League.Tests/Identity/RoleStoreTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace League.Tests.Identity;
public class RoleStoreTests
{
private readonly UnitTestHelpers _uth = new();
private readonly AppDb _appDb;
private readonly IAppDb _appDb;
private readonly RoleStore _roleStore;
private readonly UserStore _userStore;

Expand Down
2 changes: 1 addition & 1 deletion League.Tests/Identity/UserAuthenticationTokenStoreTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace League.Tests.Identity;
public class UserAuthenticationTokenStoreTests
{
private readonly UnitTestHelpers _uth = new();
private readonly AppDb _appDb;
private readonly IAppDb _appDb;
private readonly UserStore _store;

public UserAuthenticationTokenStoreTests()
Expand Down
2 changes: 1 addition & 1 deletion League.Tests/Identity/UserClaimStoreTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace League.Tests.Identity;
public class UserClaimStoreTests
{
private readonly UnitTestHelpers _uth = new();
private readonly AppDb _appDb;
private readonly IAppDb _appDb;
private ApplicationUser _user = new();
private readonly UserStore _store;
private TeamEntity _team = new();
Expand Down
2 changes: 1 addition & 1 deletion League.Tests/Identity/UserLoginStoreTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace League.Tests.Identity;
public class UserLoginStoreTests
{
private readonly UnitTestHelpers _uth = new();
private readonly AppDb _appDb;
private readonly IAppDb _appDb;
private readonly UserStore _store;

public UserLoginStoreTests()
Expand Down
2 changes: 1 addition & 1 deletion League.Tests/Identity/UserRoleStoreTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace League.Tests.Identity;
public class UserRoleStoreTests
{
private readonly UnitTestHelpers _uth = new();
private readonly AppDb _appDb;
private readonly IAppDb _appDb;
private readonly UserStore _userStore;
private ApplicationUser _user = new();

Expand Down
2 changes: 1 addition & 1 deletion League.Tests/Identity/UserStoreTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace League.Tests.Identity;
public class UserStoreTests
{
private readonly UnitTestHelpers _uth = new();
private readonly AppDb _appDb;
private readonly IAppDb _appDb;
private readonly UserStore _store;
private readonly RoleStore _roleStore;

Expand Down
2 changes: 1 addition & 1 deletion League/Components/RoundSelector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace League.Components;
public class RoundSelector : ViewComponent
{
private readonly ITenantContext _tenantContext;
private readonly AppDb _appDb;
private readonly IAppDb _appDb;
private readonly ILogger<RoundSelector> _logger;

public RoundSelector(ITenantContext tenantContext, ILogger<RoundSelector> logger)
Expand Down
2 changes: 1 addition & 1 deletion League/Components/VenueSelector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace League.Components;

public class VenueSelector : ViewComponent
{
private readonly AppDb _appDb;
private readonly IAppDb _appDb;
private readonly ILogger<VenueSelector> _logger;

public VenueSelector(ITenantContext tenantContext, ILogger<VenueSelector> logger)
Expand Down
2 changes: 1 addition & 1 deletion League/Controllers/Contact.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace League.Controllers;
public class Contact : AbstractController
{
#pragma warning disable IDE0052 // Remove unread private members
private readonly AppDb _appDb;
private readonly IAppDb _appDb;
private readonly TenantStore _tenantStore;
#pragma warning restore IDE0052 // Remove unread private members
private readonly ITenantContext _tenantContext;
Expand Down
2 changes: 1 addition & 1 deletion League/Controllers/Map.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class Map : AbstractController
private readonly IConfiguration _configuration;
private readonly ILogger<Map> _logger;
private readonly ITenantContext _tenantContext;
private readonly AppDb _appDb;
private readonly IAppDb _appDb;
private readonly IStringLocalizer<Map> _localizer;
private readonly GoogleConfiguration _googleConfig;

Expand Down
2 changes: 1 addition & 1 deletion League/Controllers/Match.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ namespace League.Controllers;
public class Match : AbstractController
{
private readonly ITenantContext _tenantContext;
private readonly AppDb _appDb;
private readonly IAppDb _appDb;
private readonly IStringLocalizer<Match> _localizer;
private readonly IAuthorizationService _authorizationService;
private readonly Axuno.Tools.DateAndTime.TimeZoneConverter _timeZoneConverter;
Expand Down
2 changes: 1 addition & 1 deletion League/Controllers/Ranking.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class Ranking : AbstractController
{
private readonly ITenantContext _tenantContext;
private readonly IWebHostEnvironment _webHostEnvironment;
private readonly AppDb _appDb;
private readonly IAppDb _appDb;
private readonly ILogger<Ranking> _logger;
private readonly IMemoryCache _memoryCache;

Expand Down
2 changes: 1 addition & 1 deletion League/Controllers/Team.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace League.Controllers;
public class Team : AbstractController
{
private readonly ITenantContext _tenantContext;
private readonly AppDb _appDb;
private readonly IAppDb _appDb;
private readonly IStringLocalizer<Team> _localizer;
private readonly IAuthorizationService _authorizationService;
private readonly ILogger<Team> _logger;
Expand Down
2 changes: 1 addition & 1 deletion League/Controllers/TeamApplication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ namespace League.Controllers;
public class TeamApplication : AbstractController
{
private readonly ITenantContext _tenantContext;
private readonly AppDb _appDb;
private readonly IAppDb _appDb;
private readonly IAuthorizationService _authorizationService;
private readonly ILogger<TeamApplication> _logger;
private readonly Axuno.Tools.DateAndTime.TimeZoneConverter _timeZoneConverter;
Expand Down
2 changes: 1 addition & 1 deletion League/Controllers/Venue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace League.Controllers;
public class Venue : AbstractController
{
private readonly ITenantContext _tenantContext;
private readonly AppDb _appDb;
private readonly IAppDb _appDb;
private readonly IAuthorizationService _authorizationService;
private readonly IStringLocalizer<Venue> _localizer;
private readonly RegionInfo _regionInfo;
Expand Down
2 changes: 1 addition & 1 deletion League/Identity/RoleStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace League.Identity;
/// </summary>
public class RoleStore : IRoleStore<ApplicationRole>, IRoleClaimStore<ApplicationRole>
{
private readonly TournamentManager.MultiTenancy.AppDb _appDb;
private readonly IAppDb _appDb;
private readonly ILogger<UserStore> _logger;
private readonly ILookupNormalizer _keyNormalizer;
private readonly IdentityErrorDescriber _identityErrorDescriber;
Expand Down
2 changes: 1 addition & 1 deletion League/Identity/UserStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace League.Identity;
/// </summary>
public class UserStore : IUserStore<ApplicationUser>, IUserEmailStore<ApplicationUser>, IUserPhoneNumberStore<ApplicationUser>, IUserPasswordStore<ApplicationUser>, IUserRoleStore<ApplicationUser>, IUserClaimStore<ApplicationUser>, IUserSecurityStampStore<ApplicationUser>, IUserLoginStore<ApplicationUser>, IUserAuthenticationTokenStore<ApplicationUser>, IUserLockoutStore<ApplicationUser>
{
private readonly TournamentManager.MultiTenancy.AppDb _appDb;
private readonly IAppDb _appDb;
private readonly ILogger<UserStore> _logger;
private readonly ILookupNormalizer _keyNormalizer;
private readonly IdentityErrorDescriber _identityErrorDescriber;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
using System.Collections.ObjectModel;
using System.Diagnostics;
using Microsoft.Extensions.Logging.Abstractions;
using NUnit.Framework;
using TournamentManager.MultiTenancy;
using TournamentManager.Plan;
using TournamentManager.RoundRobin;

namespace TournamentManager.Tests.Plan;

Expand Down Expand Up @@ -104,6 +102,46 @@ public void CreateMatchesWithUndefinedRefereeTypeShouldThrow()
});
}

[Test]
public void SwappingHomeAndGuestInParticipantCombinationShouldSucceed()
{
var matchCreator = GetMatchCreator(5, RefereeType.Home);
var combination = matchCreator.GetCombinations(LegType.First).First();

var (home, guest) = (combination.Home, combination.Guest);
var stringBeforeSwap = combination.ToString();
combination.SwapHomeGuest();

Assert.Multiple(() =>
{
Assert.That(combination.Home, Is.EqualTo(guest));
Assert.That(combination.Guest, Is.EqualTo(home));
Assert.That(combination.ToString(), Is.Not.EqualTo(stringBeforeSwap));
});
}

[Test]
public void NumberOfCombinationsAndTurns()
{
const int numOfParticipants = 5;
var matchCreator = GetMatchCreator(numOfParticipants, RefereeType.Home);
var firstLeg = matchCreator.GetCombinations(LegType.First);
var firstTurn = firstLeg.GetCombinations(1);
var allTurns = firstLeg.GetTurns().ToList();
foreach(var turn in allTurns)
{
firstLeg.TurnDateTimePeriods.Add(turn, null);
}

Assert.Multiple(() =>
{
Assert.That(firstLeg.Count, Is.EqualTo(numOfParticipants * 2));
Assert.That(firstTurn.Count, Is.EqualTo(2));
Assert.That(allTurns.Count, Is.EqualTo(numOfParticipants));
Assert.That(firstLeg.TurnDateTimePeriods.Count, Is.EqualTo(allTurns.Count));
});
}

private static Collection<long> GetParticipants(int numOfParticipants)
{
var participants = new Collection<long>();
Expand All @@ -115,5 +153,14 @@ private static Collection<long> GetParticipants(int numOfParticipants)
return participants;
}

private static MatchCreator<long, long> GetMatchCreator(int numOfParticipants, RefereeType refereeType)
{
var participants = GetParticipants(numOfParticipants);
var tenantContext = new TenantContext();
tenantContext.TournamentContext.RefereeRuleSet.RefereeType = refereeType;

return new MatchCreator<long, long>(tenantContext, NullLogger<MatchCreator<long, long>>.Instance)
.SetParticipants(participants);
}
}

2 changes: 1 addition & 1 deletion TournamentManager/TournamentManager/MultiTenancy/AppDb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,4 @@ public AppDb(IDbContext dbContext)
public virtual VenueRepository VenueRepository => new(DbContext);
public virtual ExcludedMatchDateRepository ExcludedMatchDateRepository => new(DbContext);
public virtual AvailableMatchDateRepository AvailableMatchDateRepository => new(DbContext);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -94,5 +94,5 @@ public virtual IDataAccessAdapter GetNewAdapter()
/// Gives access to the repositories.
/// </summary>
[YAXLib.Attributes.YAXDontSerialize]
public virtual AppDb AppDb { get; }
public virtual IAppDb AppDb { get; }
}
32 changes: 25 additions & 7 deletions TournamentManager/TournamentManager/MultiTenancy/IAppDb.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,30 @@
namespace TournamentManager.MultiTenancy;
using TournamentManager.Data;

namespace TournamentManager.MultiTenancy;

/// <summary>
/// Interface for accessing the repositories.
/// </summary>
public interface IAppDb
{
/// <summary>
/// The <see cref="MultiTenancy.IDbContext"/> instance to be used to access the repositories.
/// Provides database-specific settings.
/// </summary>
MultiTenancy.IDbContext DbContext { get; }
}
IDbContext DbContext { get; }

GenericRepository GenericRepository { get; }
ManagerOfTeamRepository ManagerOfTeamRepository { get; }
MatchRepository MatchRepository { get; }
PlayerInTeamRepository PlayerInTeamRepository { get; }
RoundRepository RoundRepository { get; }
TeamInRoundRepository TeamInRoundRepository { get; }
TeamRepository TeamRepository { get; }
TournamentRepository TournamentRepository { get; }
RankingRepository RankingRepository { get; }
RoleRepository RoleRepository { get; }
UserRepository UserRepository { get; }
UserRoleRepository UserRoleRepository { get; }
UserClaimRepository UserClaimRepository { get; }
UserLoginRepository UserLoginRepository { get; }
UserTokenRepository UserTokenRepository { get; }
VenueRepository VenueRepository { get; }
ExcludedMatchDateRepository ExcludedMatchDateRepository { get; }
AvailableMatchDateRepository AvailableMatchDateRepository { get; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ public interface IDbContext
/// <summary>
/// Gives access to the repositories.
/// </summary>
AppDb AppDb { get; }
IAppDb AppDb { get; }

/// <summary>
/// Gets a new instance of an <see cref="IDataAccessAdapter"/> which will be used to access repositories.
/// </summary>
/// <returns>Returns a new instance of an <see cref="IDataAccessAdapter"/> which will be used to access repositories.</returns>
IDataAccessAdapter GetNewAdapter();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
#nullable enable

namespace TournamentManager.MultiTenancy;
namespace TournamentManager.MultiTenancy;

[YAXLib.Attributes.YAXSerializeAs(nameof(TenantContext))]
public class TenantContext : ITenantContext
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace TournamentManager.Plan;
internal class AvailableMatchDates
{
private readonly ITenantContext _tenantContext;
private readonly AppDb _appDb;
private readonly IAppDb _appDb;
private readonly Axuno.Tools.DateAndTime.TimeZoneConverter _timeZoneConverter;

private readonly ILogger<AvailableMatchDates> _logger;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace TournamentManager.Plan;
/// </summary>
internal class ExcludeMatchDates
{
private readonly AppDb _appDb;
private readonly IAppDb _appDb;
private readonly ILogger<ExcludeMatchDates> _logger;

/// <summary>
Expand Down
6 changes: 3 additions & 3 deletions TournamentManager/TournamentManager/Plan/MatchScheduler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ namespace TournamentManager.Plan;

/// <summary>
/// Schedules fixtures for all participants of a tournament or round.
/// If the home participant has no venue or home match date/time defined, it will only have away matches.
/// If the home participant has no venue or home match date and time are defined, it will only have away matches.
/// </summary>
internal class MatchScheduler
{
private readonly ITenantContext _tenantContext;
private readonly AppDb _appDb;
private readonly IAppDb _appDb;
private TournamentEntity _tournament = new();
private readonly ILoggerFactory _loggerFactory;
private readonly ILogger<MatchScheduler> _logger;
Expand Down Expand Up @@ -110,7 +110,7 @@ public async Task ScheduleFixturesForRound(RoundEntity round, bool keepExisting,
}
}

// save the matches for the group
// save the matches for the round
await _appDb.GenericRepository.SaveEntitiesAsync(tournamentMatches, true, false, cancellationToken);

await _availableMatchDates.ClearAsync(MatchDateClearOption.OnlyAutoGenerated, cancellationToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ public void SwapHomeGuest()
}

/// <summary>
/// Returns the string representation of the participants combination.
/// Returns the string representation of the participants' combination.
/// </summary>
/// <returns>Returns the string representation of the participants combination.</returns>
/// <returns>Returns the string representation of the participants' combination.</returns>
public override string ToString()
{
return string.Concat(Home.ToString(), " : ", Guest.ToString(), " / ", Referee?.ToString() ?? "-");
Expand Down

0 comments on commit e0624f9

Please sign in to comment.