public async Task HandleAsync(JoinLeague command, ICorrelationContext context) { PlayerInternalIdDto playerIdDto = await _playersService.GetInternalIdAsync(command.PlayerExternalId); if (playerIdDto is null) { throw new FliGenException(ErrorCodes.NoPlayerWithSuchExternalId, $"There is no player with external id: {command.PlayerExternalId}"); } int playerId = playerIdDto.InternalId; var leagueSettingsRepo = _uow.GetRepositoryAsync <Domain.Entities.LeagueSettings>(); Domain.Entities.LeagueSettings leagueSettings = await leagueSettingsRepo.SingleAsync(x => x.LeagueId == command.LeagueId); if (leagueSettings is null) { throw new FliGenException(ErrorCodes.NoLeagueSettings, $"There is no league settings for league: {command.LeagueId}"); } var lplinksRepo = _uow.GetRepositoryAsync <LeaguePlayerLink>(); LeaguePlayerLink lastLink = await lplinksRepo.SingleAsync(x => x.PlayerId == playerId && x.LeagueId == command.LeagueId && x.Actual); if (lastLink is null || lastLink.InLeftStatus()) { LeaguePlayerLink link = leagueSettings.RequireConfirmation ? LeaguePlayerLink.CreateWaitingLink(command.LeagueId, playerId) : LeaguePlayerLink.CreateJoinedLink(command.LeagueId, playerId); if (leagueSettings.RequireConfirmation) { LeaguePlayerLink.CreateWaitingLink(command.LeagueId, playerId); } else { LeaguePlayerLink.CreateJoinedLink(command.LeagueId, playerId); } await lplinksRepo.AddAsync(link); }
public async Task JoinLeagueShouldChangePlayerStatus() { var fix = new Fixture(); int leagueId = fix.Create <int>(); int playerId = fix.Create <int>(); var command = new JoinLeague() { LeagueId = leagueId, PlayerExternalId = Guid.NewGuid().ToString() }; var retDto = new PlayerInternalIdDto() { InternalId = playerId }; var uow = Substitute.For <IUnitOfWork>(); var playersService = Substitute.For <IPlayersService>(); playersService.GetInternalIdAsync(command.PlayerExternalId).ReturnsForAnyArgs(retDto); var leagueSettingsRepo = Substitute.For <IRepositoryAsync <LeagueSettings> >(); var leaguePlayerLinksRepo = Substitute.For <IRepositoryAsync <LeaguePlayerLink> >(); var lsWithoutConfirm = LeagueSettings.Create(true, false, leagueId); var lsWithConfirm = LeagueSettings.Create(true, true, leagueId); var leftLpl = LeaguePlayerLink.CreateJoinedLink(leagueId, playerId); leftLpl.UpdateToLeft(); var hs = new HashSet <(LeagueSettings, LeaguePlayerLink, Action)> { ( lsWithoutConfirm, null, () => leaguePlayerLinksRepo.Received().AddAsync(Arg.Is <LeaguePlayerLink>(x => x.InJoinedStatus())) ), ( lsWithoutConfirm, leftLpl, () => leaguePlayerLinksRepo.Received().AddAsync(Arg.Is <LeaguePlayerLink>(x => x.InJoinedStatus())) ), ( lsWithoutConfirm, LeaguePlayerLink.CreateWaitingLink(leagueId, playerId), () => leaguePlayerLinksRepo.Received().UpdateAsync(Arg.Is <LeaguePlayerLink>(x => x.InJoinedStatus())) ), ( lsWithoutConfirm, LeaguePlayerLink.CreateJoinedLink(leagueId, playerId), () => leaguePlayerLinksRepo.Received().UpdateAsync(Arg.Is <LeaguePlayerLink>(x => x.InLeftStatus())) ), ( lsWithConfirm, null, () => leaguePlayerLinksRepo.Received().AddAsync(Arg.Is <LeaguePlayerLink>(x => x.InWaitingStatus())) ), ( lsWithConfirm, leftLpl, () => leaguePlayerLinksRepo.Received().AddAsync(Arg.Is <LeaguePlayerLink>(x => x.InJoinedStatus())) ), ( lsWithConfirm, LeaguePlayerLink.CreateWaitingLink(leagueId, playerId), () => leaguePlayerLinksRepo.Received().RemoveAsync(Arg.Is <LeaguePlayerLink>(x => x.InWaitingStatus())) ), ( lsWithConfirm, LeaguePlayerLink.CreateJoinedLink(leagueId, playerId), () => leaguePlayerLinksRepo.Received().UpdateAsync(Arg.Is <LeaguePlayerLink>(x => x.InLeftStatus())) ), }; foreach (var(lsItem, lplItem, checkAction) in hs) { leagueSettingsRepo.SingleAsync().ReturnsForAnyArgs(lsItem); leaguePlayerLinksRepo.SingleAsync().ReturnsForAnyArgs(lplItem); uow.GetRepositoryAsync <LeagueSettings>().ReturnsForAnyArgs(leagueSettingsRepo); uow.GetRepositoryAsync <LeaguePlayerLink>().ReturnsForAnyArgs(leaguePlayerLinksRepo); var commandHandler = new JoinLeagueHandler(uow, playersService); await commandHandler.HandleAsync(command, new CorrelationContext()); checkAction(); } }
public LeaguesContext GetInitiatedLeaguesContext() { LeaguesContext context = CreateContextAndMigrateDb(); var leagueForDelete = League.Create("for delete", "descr", LeagueType.Football); var leagueForUpdate = League.Create("for update", "descr", LeagueType.Football); var leagueForJoin1 = League.Create("for join 1", "descr", LeagueType.Football); var leagueForJoin2 = League.Create("for join 2", "descr", LeagueType.Hockey); var leagueForJoin3 = League.Create("for join 3", "descr", LeagueType.Hockey); var entityForDelete = context.Leagues.Add(leagueForDelete); var entityForUpdate = context.Leagues.Add(leagueForUpdate); var entityForJoin1 = context.Leagues.Add(leagueForJoin1); var entityForJoin2 = context.Leagues.Add(leagueForJoin2); var entityForJoin3 = context.Leagues.Add(leagueForJoin3); context.SaveChanges(); MockedDataInstance = new MockedData { LeagueForDeleteId = entityForDelete.Entity.Id, LeagueForUpdateId = entityForUpdate.Entity.Id, LeagueForJoinId1 = entityForJoin1.Entity.Id, LeagueForJoinId2 = entityForJoin2.Entity.Id, LeagueForJoinId3 = entityForJoin3.Entity.Id, PlayersInTeam = 10, TeamsInTour = 50, Player1 = 100, Player2 = 101, Player3 = 102, Player4 = 103, Player5 = 104 }; IEnumerable <LeaguePlayerLink> links = new[] { // 1 league LeaguePlayerLink.CreateJoinedLink(MockedDataInstance.LeagueForJoinId1, MockedDataInstance.Player1), LeaguePlayerLink.CreateJoinedLink(MockedDataInstance.LeagueForJoinId1, MockedDataInstance.Player2), LeaguePlayerLink.CreateWaitingLink(MockedDataInstance.LeagueForJoinId1, MockedDataInstance.Player3), // 2 league LeaguePlayerLink.CreateJoinedLink(MockedDataInstance.LeagueForJoinId2, MockedDataInstance.Player2), LeaguePlayerLink.CreateWaitingLink(MockedDataInstance.LeagueForJoinId2, MockedDataInstance.Player3), LeaguePlayerLink.CreateWaitingLink(MockedDataInstance.LeagueForJoinId2, MockedDataInstance.Player4), LeaguePlayerLink.CreateWaitingLink(MockedDataInstance.LeagueForJoinId2, MockedDataInstance.Player5), // 3 league LeaguePlayerLink.CreateJoinedLink(MockedDataInstance.LeagueForJoinId3, MockedDataInstance.Player1), LeaguePlayerLink.CreateJoinedLink(MockedDataInstance.LeagueForJoinId3, MockedDataInstance.Player2), LeaguePlayerLink.CreateJoinedLink(MockedDataInstance.LeagueForJoinId3, MockedDataInstance.Player3), LeaguePlayerLink.CreateJoinedLink(MockedDataInstance.LeagueForJoinId3, MockedDataInstance.Player4), LeaguePlayerLink.CreateJoinedLink(MockedDataInstance.LeagueForJoinId3, MockedDataInstance.Player5) }; MockedDataInstance.League1JoinedPlayersCount = 2; MockedDataInstance.League2JoinedPlayersCount = 1; MockedDataInstance.League3JoinedPlayersCount = 5; MockedDataInstance.League1WaitingPlayersCount = 1; MockedDataInstance.League1WaitingPlayersCount = 3; MockedDataInstance.League1WaitingPlayersCount = 0; context.LeaguePlayerLinks.AddRange(links); var leagueSettingsForUpdate = LeagueSettings.Create( false, false, leagueId: entityForUpdate.Entity.Id, MockedDataInstance.PlayersInTeam, MockedDataInstance.TeamsInTour); context.LeagueSettings.Add(leagueSettingsForUpdate); context.SaveChanges(); return(context); }