public async Task GetTeamsQuery_Can_Return_Teams()
        {
            var dbContext = GetDbContext("GetTeamsQuery_Can_Return_Teams");

            using (dbContext)
            {
                var fakeRepo   = new TeamRepository(dbContext);
                var fakeLogger = new Mock <ILogger <GetTeamsQueryHandler> >();
                var handler    = new GetTeamsQueryHandler(fakeRepo, GetMapper(), fakeLogger.Object);

                var TeamToAdd = new Team
                {
                    Name      = "TeamName",
                    Longitude = 8.11,
                    Latitude  = 9.43,
                };

                await fakeRepo.AddAsync(TeamToAdd);

                var result = await handler.Handle(new GetTeamsQuery(), default);

                Assert.False(result.Notifications.HasErrors());

                Assert.Equal(1, result.Teams.Count);
                Assert.Equal(TeamToAdd.Name, result.Teams[0].Name);
                Assert.Equal(TeamToAdd.Longitude, result.Teams[0].Longitude);
                Assert.Equal(TeamToAdd.Longitude, result.Teams[0].Longitude);
            }
        }
        public async Task DeleteTeamCommand_Can_Delete_Team()
        {
            using (var dbContext = GetDbContext("DeleteTeamCommand_Can_Delete_Team"))
            {
                var fakeRepo = new TeamRepository(dbContext);
                await fakeRepo.AddAsync(new Domain.Entities.Team
                {
                    Name = "TestTeamName"
                });
            }

            using (var dbContext = GetDbContext("DeleteTeamCommand_Can_Delete_Team"))
            {
                var fakeRepo   = new TeamRepository(dbContext);
                var fakeLogger = new Mock <ILogger <DeleteTeamCommandHandler> >();
                var handler    = new DeleteTeamCommandHandler(fakeRepo, GetMapper(), fakeLogger.Object);

                var command = new DeleteTeamCommand
                {
                    Id = 1,
                };

                var result = await handler.Handle(command, default);

                Assert.False(result.Notifications.HasErrors());

                Assert.Equal(command.Id, result.TeamDeleteModel.Id);
                Assert.Equal(Domain.Enumerations.EnumBag.DataState.Inactive, result.TeamDeleteModel.DataState);
            }
        }
        public override void Handle(BrokeredMessage message)
        {
            var teamRepository = new TeamRepository(new MyCompanyContext());
            var dto            = message.GetBody <TeamDTO>();
            var team           = Mapper.Map <Team>(dto);

            teamRepository.AddAsync(team).Wait();
        }
        public async Task GetStadiumsQuery_Can_Return_Stadiums()
        {
            var dbContext = GetDbContext("GetStadiumsQuery_Can_Return_Stadiums");

            using (dbContext)
            {
                var fakeStadiumRepo = new StadiumRepository(dbContext);
                var fakeTeamRepo    = new TeamRepository(dbContext);
                var fakeLogger      = new Mock <ILogger <GetStadiumsQueryHandler> >();
                var handler         = new GetStadiumsQueryHandler(fakeStadiumRepo, GetMapper(), fakeLogger.Object);

                var TeamsToAdd = new List <Team>
                {
                    new Team
                    {
                        Name      = "TeamOne",
                        Longitude = 1.11,
                        Latitude  = 1.43,
                    },
                    new Team
                    {
                        Name      = "TeamTwo",
                        Longitude = 2.11,
                        Latitude  = 2.43,
                    },
                    new Team
                    {
                        Name      = "TeamThree",
                        Longitude = 3.11,
                        Latitude  = 3.43,
                    },
                };

                foreach (var TeamToAdd in TeamsToAdd)
                {
                    await fakeTeamRepo.AddAsync(TeamToAdd);
                }

                var StadiumToAdd = new Stadium
                {
                    Name   = "StadiumName",
                    TeamId = 2
                };

                await fakeStadiumRepo.AddAsync(StadiumToAdd);

                var result = await handler.Handle(new GetStadiumsQuery(), default);

                Assert.False(result.Notifications.HasErrors());

                Assert.Equal(1, result.Stadiums.Count);
                Assert.Equal(StadiumToAdd.Name, result.Stadiums[0].Name);
                Assert.Equal(StadiumToAdd.TeamId, result.Stadiums[0].Team.Id);
            }
        }
Example #5
0
        public async Task AddStadiumCommand_Can_Add_Stadium()
        {
            using (var dbContext = GetDbContext("AddStadiumCommand_Can_Add_Stadium"))
            {
                var fakePlayerRepo = new PlayerRepository(dbContext);
                await fakePlayerRepo.AddAsync(new Domain.Entities.Player
                {
                    Name         = "FirstPlayer",
                    Surname      = "LastName",
                    Height       = 1.98,
                    EmailAddress = "*****@*****.**"
                });

                await fakePlayerRepo.AddAsync(new Domain.Entities.Player
                {
                    Name         = "SecondPlayer",
                    Surname      = "LastName",
                    Height       = 1.98,
                    EmailAddress = "*****@*****.**"
                });

                var fakeTeamRepo = new TeamRepository(dbContext);

                var players = fakePlayerRepo.GetAll();
                await fakeTeamRepo.AddAsync(new Domain.Entities.Team
                {
                    Name    = "FirstTeam",
                    Players = players.ToList()
                });
            }

            using (var dbContext = GetDbContext("AddStadiumCommand_Can_Add_Stadium"))
            {
                var fakeStadiumRepo = new StadiumRepository(dbContext);
                var fakeTeamRepo    = new TeamRepository(dbContext);
                var fakeLogger      = new Mock <ILogger <AddStadiumCommandHandler> >();
                var handler         = new AddStadiumCommandHandler(fakeStadiumRepo, fakeTeamRepo, GetMapper(), fakeLogger.Object);

                var command = new AddStadiumCommand
                {
                    Name   = "TestStadiumName",
                    TeamId = 1
                };

                var result = await handler.Handle(command, default);

                Assert.False(result.Notifications.HasErrors());

                Assert.Equal(command.Name, result.StadiumLookupModel.Name);
                Assert.Equal(command.TeamId, result.StadiumLookupModel.Team.Id);
            }
        }
        public override void Handle(BrokeredMessage message)
        {
            var teamRepository = new TeamRepository(new MyCompanyContext());
            var dto            = message.GetBody <TeamDTO>();
            var team           = Mapper.Map <Team>(dto);

            var current = teamRepository.GetAsync(team.TeamId).Result;

            if (current == null)
            {
                teamRepository.AddAsync(team).Wait();
            }
        }
Example #7
0
        public async Task GetTeamByIdQuery_Can_Return_Team()
        {
            var dbContext = GetDbContext("GetTeamByIdQuery_Can_Return_Team");

            using (dbContext)
            {
                var fakeRepo   = new TeamRepository(dbContext);
                var fakeLogger = new Mock <ILogger <GetTeamByIdQueryHandler> >();
                var handler    = new GetTeamByIdQueryHandler(fakeRepo, GetMapper(), fakeLogger.Object);

                var TeamsToAdd = new List <Team>
                {
                    new Team
                    {
                        Name      = "TeamOne",
                        Longitude = 1.11,
                        Latitude  = 1.43,
                    },
                    new Team
                    {
                        Name      = "TeamTwo",
                        Longitude = 2.11,
                        Latitude  = 2.43,
                    },
                    new Team
                    {
                        Name      = "TeamThree",
                        Longitude = 3.11,
                        Latitude  = 3.43,
                    },
                };

                foreach (var TeamToAdd in TeamsToAdd)
                {
                    await fakeRepo.AddAsync(TeamToAdd);
                }

                var result = await handler.Handle(new GetTeamByIdQuery()
                {
                    Id = 2
                }, default);

                Assert.False(result.Notifications.HasErrors());

                var secondTeam = result.TeamLookupModel;

                Assert.Equal(TeamsToAdd[1].Name, secondTeam.Name);
                Assert.Equal(TeamsToAdd[1].Longitude, secondTeam.Longitude);
                Assert.Equal(TeamsToAdd[1].Latitude, secondTeam.Latitude);
            }
        }
        public async Task TeamRepository_AddAsync_AddsValueToDatabase()
        {
            var expectedCount = TeamsDbSet.Get().ToList().Count + 1;
            var entityToAdd   = TeamsDbSet.TeamBuilder().Create();

            await using var context = new TimeTrackingDbContext(_dbOptions);
            var teamRepository = new TeamRepository(context);

            await teamRepository.AddAsync(entityToAdd);

            context.Teams.Should().HaveCount(expectedCount);
            var entityFound = await context.Teams.FindAsync(entityToAdd.Id);

            Assert.That(entityFound, Is.EqualTo(entityToAdd).Using(EqualityComparers.TeamComparer));
        }
        public async Task TeamRepository_AddTeam_Added_NotFail_Test()
        {
            var context  = new MyCompanyContext();
            int expected = context.Teams.Count() + 1;

            var target     = new TeamRepository(new MyCompanyContext());
            var employeeId = context.Employees.FirstOrDefault().EmployeeId;
            var teamId     = context.Teams.Select(e => e.TeamId).Max() + 1;
            var Team       = new Team()
            {
                TeamId    = teamId,
                ManagerId = employeeId,
            };
            await target.AddAsync(Team);

            int actual = context.Teams.Count();

            Assert.AreEqual(expected, actual);
        }
Example #10
0
        public async Task UpdateTeamCommand_Can_Update_Team()
        {
            using (var dbContext = GetDbContext("UpdateTeamCommand_Can_Update_Team"))
            {
                var fakeRepo = new TeamRepository(dbContext);
                await fakeRepo.AddAsync(new Domain.Entities.Team
                {
                    Name      = "TeamName",
                    Longitude = 8.11,
                    Latitude  = 9.43,
                });
            }

            using (var dbContext = GetDbContext("UpdateTeamCommand_Can_Update_Team"))
            {
                var fakeRepo   = new TeamRepository(dbContext);
                var fakeLogger = new Mock <ILogger <UpdateTeamCommandHandler> >();
                var handler    = new UpdateTeamCommandHandler(fakeRepo, GetMapper(), fakeLogger.Object);

                var command = new UpdateTeamCommand
                {
                    Id        = 1,
                    Name      = "NewTeamName",
                    Longitude = 2.11,
                    Latitude  = 6.43,
                };

                var result = await handler.Handle(command, default);

                Assert.False(result.Notifications.HasErrors());

                Assert.Equal(command.Name, result.TeamLookupModel.Name);
                Assert.Equal(command.Longitude, result.TeamLookupModel.Longitude);
                Assert.Equal(command.Latitude, result.TeamLookupModel.Latitude);
            }
        }
        public async Task GetStadiumByIdQuery_Can_Return_Stadium()
        {
            var dbContext = GetDbContext("GetStadiumByIdQuery_Can_Return_Stadium");

            using (dbContext)
            {
                var fakeStadiumRepo = new StadiumRepository(dbContext);
                var fakeTeamRepo    = new TeamRepository(dbContext);
                var fakeLogger      = new Mock <ILogger <GetStadiumByIdQueryHandler> >();
                var handler         = new GetStadiumByIdQueryHandler(fakeStadiumRepo, GetMapper(), fakeLogger.Object);

                var fakePlayerRepo = new PlayerRepository(dbContext);
                await fakePlayerRepo.AddAsync(new Domain.Entities.Player
                {
                    Name         = "FirstPlayer",
                    Surname      = "LastName",
                    Height       = 1.98,
                    EmailAddress = "*****@*****.**"
                });

                await fakePlayerRepo.AddAsync(new Domain.Entities.Player
                {
                    Name         = "SecondPlayer",
                    Surname      = "LastName",
                    Height       = 1.98,
                    EmailAddress = "*****@*****.**"
                }); await fakePlayerRepo.AddAsync(new Domain.Entities.Player
                {
                    Name         = "ThirdPlayer",
                    Surname      = "LastName",
                    Height       = 1.98,
                    EmailAddress = "*****@*****.**"
                });

                var TeamsToAdd = new List <Team>
                {
                    new Team
                    {
                        Name      = "TeamOne",
                        Longitude = 1.11,
                        Latitude  = 1.43,
                        Players   = new List <Player> {
                            await fakePlayerRepo.GetPlayerByIdAsync(1)
                        }
                    },
                    new Team
                    {
                        Name      = "TeamTwo",
                        Longitude = 2.11,
                        Latitude  = 2.43,
                        Players   = new List <Player> {
                            await fakePlayerRepo.GetPlayerByIdAsync(2)
                        }
                    },
                    new Team
                    {
                        Name      = "TeamThree",
                        Longitude = 3.11,
                        Latitude  = 3.43,
                        Players   = new List <Player> {
                            await fakePlayerRepo.GetPlayerByIdAsync(3)
                        }
                    },
                };

                foreach (var TeamToAdd in TeamsToAdd)
                {
                    await fakeTeamRepo.AddAsync(TeamToAdd);
                }

                var StadiumsToAdd = new List <Stadium>
                {
                    new Stadium
                    {
                        Name   = "StadiumOne",
                        TeamId = 1
                    },
                    new Stadium
                    {
                        Name   = "StadiumTwo",
                        TeamId = 2
                    },
                    new Stadium
                    {
                        Name   = "StadiumThree",
                        TeamId = 3
                    }
                };

                foreach (var StadiumToAdd in StadiumsToAdd)
                {
                    await fakeStadiumRepo.AddAsync(StadiumToAdd);
                }

                var result = await handler.Handle(new GetStadiumByIdQuery()
                {
                    Id = 2
                }, default);

                Assert.False(result.Notifications.HasErrors());

                var secondStadium = result.StadiumLookupModel;

                Assert.Equal(StadiumsToAdd[1].Name, secondStadium.Name);
                Assert.Equal(StadiumsToAdd[1].Team.Id, secondStadium.Team.Id);
            }
        }
Example #12
0
        public async Task UpdateTeamPlayersCommand_Can_Add_Team()
        {
            using (var dbContext = GetDbContext("UpdateTeamPlayersCommand_Can_Add_Team"))
            {
                var fakePlayerRepo = new PlayerRepository(dbContext);
                await fakePlayerRepo.AddAsync(new Domain.Entities.Player
                {
                    Name         = "FirstPlayer",
                    Surname      = "LastName",
                    Height       = 1.98,
                    EmailAddress = "*****@*****.**"
                });

                await fakePlayerRepo.AddAsync(new Domain.Entities.Player
                {
                    Name         = "SecondPlayer",
                    Surname      = "LastName",
                    Height       = 1.98,
                    EmailAddress = "*****@*****.**"
                });

                var fakeTeamRepo = new TeamRepository(dbContext);

                await fakeTeamRepo.AddAsync(new Domain.Entities.Team
                {
                    Name      = "TeamName",
                    Longitude = 1.11,
                    Latitude  = 1.43,
                });

                var fakeTeam = fakeTeamRepo.GetTeamByIdAsync(1).Result;

                var fakePlayerOne = fakePlayerRepo.GetPlayerByIdAsync(1).Result;

                fakePlayerOne.Team = fakeTeam;
                fakeTeam.Players   = new List <Player> {
                    fakePlayerOne
                };

                await fakePlayerRepo.UpdateAsync(fakePlayerOne);

                await fakeTeamRepo.UpdateAsync(fakeTeam);

                var fakeLogger = new Mock <ILogger <UpdateTeamPlayersCommandHandler> >();
                var handler    = new UpdateTeamPlayersCommandHandler(fakeTeamRepo, fakePlayerRepo, GetMapper(), fakeLogger.Object);

                var command = new UpdateTeamPlayersCommand
                {
                    Id        = 1,
                    PlayerIds = new List <int> {
                        2
                    }
                };

                var result = await handler.Handle(command, default);

                Assert.False(result.Notifications.HasErrors());

                Assert.Equal(command.Id, result.TeamLookupModel.Id);
                Assert.Equal(1, result.TeamLookupModel.Players.ToList()[0].Id);
                Assert.Equal(command.PlayerIds.ToList()[0], result.TeamLookupModel.Players.ToList()[1].Id);
            }
        }
Example #13
0
        public async Task LoadTeamsAndResults()
        {
            _dataBaseTools.Reset();

            var teams = GetTeams();

            foreach (var team in teams)
            {
                FillTeamDetails(team);

                await _teamRepository.AddAsync(team);

                await _teamRepository.SaveChangesAsync();
            }

            foreach (var team in teams)
            {
                var results = GetTeamResults(team);

                foreach (var result in results)
                {
                    var versusTeam = await _teamRepository.GetTeamByNameAsync(result.VersusTeam);

                    var thisTeam = await _teamRepository.GetTeamByNameAsync(team.Name);

                    var hasGameAlreadyBeenPlayed = result.Result.Split('-').Length == 2;

                    if (!hasGameAlreadyBeenPlayed)
                    {
                        return;
                    }

                    var homeTeamPoints   = Convert.ToInt32(result.Result.Split('-')[0].Trim());
                    var abroadTeamPoints = Convert.ToInt32(result.Result.Split('-')[1].Trim());

                    var day = await _dayRepository.GetDayByOrderAsync(result.Day);

                    if (day == null)
                    {
                        day = new Day(result.Day);
                        await _dayRepository.AddDayAsync(day);
                    }

                    var homeTeam = result.VersusIndicator == "vs"
                        ? thisTeam
                        : versusTeam;

                    var abroadTeam = result.VersusIndicator == "vs"
                        ? versusTeam
                        : thisTeam;

                    var game = new Game(homeTeam, abroadTeam, homeTeamPoints, abroadTeamPoints);
                    var isGameAlreadySaved = day.Games.Any(g =>
                                                           g.HomeTeamId == thisTeam.Id && g.AbroadTeamId == versusTeam.Id);

                    if (isGameAlreadySaved)
                    {
                        return;
                    }

                    day.AddGame(game);
                    await _dayRepository.SaveChangesAsync();
                }
            }
        }
        public async Task UpdateStadiumCommand_Can_Update_Stadium()
        {
            using (var dbContext = GetDbContext("UpdateStadiumCommand_Can_Update_Stadium"))
            {
                var fakeStadiumRepo = new StadiumRepository(dbContext);
                var fakeTeamRepo    = new TeamRepository(dbContext);

                var fakePlayerRepo = new PlayerRepository(dbContext);
                await fakePlayerRepo.AddAsync(new Domain.Entities.Player
                {
                    Name         = "FirstPlayer",
                    Surname      = "LastName",
                    Height       = 1.98,
                    EmailAddress = "*****@*****.**"
                });

                await fakePlayerRepo.AddAsync(new Domain.Entities.Player
                {
                    Name         = "SecondPlayer",
                    Surname      = "LastName",
                    Height       = 1.98,
                    EmailAddress = "*****@*****.**"
                }); await fakePlayerRepo.AddAsync(new Domain.Entities.Player
                {
                    Name         = "ThirdPlayer",
                    Surname      = "LastName",
                    Height       = 1.98,
                    EmailAddress = "*****@*****.**"
                });

                var TeamsToAdd = new List <Team>
                {
                    new Team
                    {
                        Name      = "TeamOne",
                        Longitude = 1.11,
                        Latitude  = 1.43,
                        Players   = new List <Player> {
                            await fakePlayerRepo.GetPlayerByIdAsync(1)
                        }
                    },
                    new Team
                    {
                        Name      = "TeamTwo",
                        Longitude = 2.11,
                        Latitude  = 2.43,
                        Players   = new List <Player> {
                            await fakePlayerRepo.GetPlayerByIdAsync(2)
                        }
                    },
                    new Team
                    {
                        Name      = "TeamThree",
                        Longitude = 3.11,
                        Latitude  = 3.43,
                        Players   = new List <Player> {
                            await fakePlayerRepo.GetPlayerByIdAsync(3)
                        }
                    },
                };

                foreach (var TeamToAdd in TeamsToAdd)
                {
                    await fakeTeamRepo.AddAsync(TeamToAdd);
                }

                await fakeStadiumRepo.AddAsync(new Domain.Entities.Stadium
                {
                    Name   = "StadiumName",
                    TeamId = 2
                });

                var fakeLogger = new Mock <ILogger <UpdateStadiumCommandHandler> >();
                var handler    = new UpdateStadiumCommandHandler(fakeStadiumRepo, GetMapper(), fakeLogger.Object);

                var command = new UpdateStadiumCommand
                {
                    Id     = 1,
                    Name   = "NewStadiumName",
                    TeamId = 1
                };

                var result = await handler.Handle(command, default);

                Assert.False(result.Notifications.HasErrors());

                Assert.Equal(command.Name, result.StadiumLookupModel.Name);
                Assert.Equal(command.TeamId, result.StadiumLookupModel.Team.Id);
            }
        }