// Testing search criteria. Player with ID 2 should be at number 1 in the list because he match both criterias public void SearchForPlayersWithLeaugeAndPosition() { //Arrange var playerRepos = new Mock <IPlayerRepository <Player> >(); //Setup search criterias SearchCriteriaForPlayer sc = new SearchCriteriaForPlayer { PrimaryPosition = "Playmaker", League = "Second League" }; playerRepos.Setup(x => x.GetBySearchCriteria(" p.isAvailable = 1 and p.league = 'Second League' and p.PrimaryPosition = 'Playmaker'")) .Returns(new List <Player> { new Player { Id = 1, Country = "Denmark", League = "First League", PrimaryPosition = "Left back" }, new Player { Id = 2, Country = "Sweden", League = "Second League", PrimaryPosition = "Playmaker" }, new Player { Id = 3, Country = "Sweden", League = "Third League", PrimaryPosition = "Pivot" } }); PlayerLogic pl = new PlayerLogic(playerRepos.Object); //Act var list = pl.HandleSearchAlgorithm(sc); Assert.Equal("Playmaker", list[0].PrimaryPosition); }
public void SearchForPlayersWithNoCriteria() { //Arrange var playerRepos = new Mock <IPlayerRepository <Player> >(); SearchCriteriaForPlayer sc = new SearchCriteriaForPlayer(); playerRepos.Setup(x => x.GetAll()) .Returns(new List <Player> { new Player { Id = 1, Country = "Denmark" }, new Player { Id = 2, Country = "Sweden" }, new Player { Id = 3, Country = "Sweden" } }); PlayerLogic pl = new PlayerLogic(playerRepos.Object); var list = pl.HandleSearchAlgorithm(sc); Assert.Equal(3, list.Count); }
public void SearchForPlayersWithOnlyStrength() { //Arrange var playerRepos = new Mock <IPlayerRepository <Player> >(); //Setup search criterias List <string> strengthList = new List <string>() { "speedy", "Tactical", "Social" }; List <string> strengthList2 = new List <string>() { "speedy", "Tactical" }; SearchCriteriaForPlayer sc = new SearchCriteriaForPlayer { StrengthsList = strengthList }; playerRepos.Setup(x => x.GetBySearchCriteria(" s.name = 'speedy' and p.isAvailable = 1 or s.name = 'Tactical' and p.isAvailable = 1 or s.name = 'Social' and p.isAvailable = 1")) .Returns(new List <Player> { new Player { Id = 1, InjuryStatus = "Healthy", Height = 180 }, new Player { Id = 2, InjuryStatus = "Healthy", Height = 170 }, new Player { Id = 3, InjuryStatus = "Healthy", Height = 190 }, new Player { Id = 4, InjuryStatus = "Healthy", Height = 170, StrengthList = strengthList2 }, new Player { Id = 5, InjuryStatus = "Healthy", Height = 170 }, new Player { Id = 6, InjuryStatus = "Healthy", Height = 170 } }); PlayerLogic pl = new PlayerLogic(playerRepos.Object); //Act var list = pl.HandleSearchAlgorithm(sc); Assert.Equal(66, list[0].SearchPercentage); }
// Testing search criteria. Player with ID 6 should be at number 1 in the list because he match // on 3/5 criteria with would be the highest percentage match and he will be number 1 in the list public void SearchForPlayersWithCalculatePercentage() { //Arrange var playerRepos = new Mock <IPlayerRepository <Player> >(); //Setup search criterias SearchCriteriaForPlayer sc = new SearchCriteriaForPlayer { Country = "Sweden", League = "Second League", PrimaryPosition = "Pivot", InjuryStatus = "Healthy", MinimumHeight = 180 }; playerRepos.Setup(x => x.GetBySearchCriteria(" p.isAvailable = 1 and p.league = 'Second League' and p.PrimaryPosition = 'Pivot'")) .Returns(new List <Player> { new Player { Id = 1, Country = "Denmark", League = "First League", PrimaryPosition = "Left back", InjuryStatus = "Healthy" }, new Player { Id = 2, Country = "Sweden", League = "Second League", PrimaryPosition = "Playmaker", InjuryStatus = "Healthy" }, new Player { Id = 3, Country = "Sweden", League = "Third League", PrimaryPosition = "Pivot", InjuryStatus = "Healthy" }, new Player { Id = 4, Country = "Norway", League = "Second League", PrimaryPosition = "Pivot", InjuryStatus = "Healthy" }, new Player { Id = 5, Country = "Norway", League = "Second League", PrimaryPosition = "Pivot", InjuryStatus = "Healthy" }, new Player { Id = 6, Country = "Sweden", League = "First League", PrimaryPosition = "Pivot", InjuryStatus = "Healthy", Height = 170 } }); PlayerLogic pl = new PlayerLogic(playerRepos.Object); //Act var list = pl.HandleSearchAlgorithm(sc); Assert.Equal(60, list[0].SearchPercentage); }
public List <Player> HandleSearchAlgorithm(SearchCriteriaForPlayer request) { string sqlSelectStatement = ""; List <Player> playerList = new List <Player>(); //If no search criteria were selected, we select all player in DB and return them. if (request.Country == null && request.League == null && request.ContractStatus == null && request.MinimumAge == null && request.MaximumAge == null && request.PrimaryPosition == null && request.SecondaryPosition == null && request.InjuryStatus == null && request.HandPreference == null && request.MinimumHeight == null && request.MaximumWeight == null && request.StrengthsList.Count == 0) { return((List <Player>)_playerRepos.GetAll()); } //First we check if any League or PrimaryPosition is selected as search criteria. If true we selected after those 2 or 1 of them if (request.League != null || request.PrimaryPosition != null) { sqlSelectStatement += " p.isAvailable = 1"; if (request.League != null) { sqlSelectStatement += " and p.league = '" + request.League + "'"; } if (request.PrimaryPosition != null) { sqlSelectStatement += " and p.PrimaryPosition = '" + request.PrimaryPosition + "'"; } } //If no league or primaryPosition was selected we try to selected after country if they specified any country in their search. else if (request.Country != null) { sqlSelectStatement = " p.isAvailable = 1 and p.country = '" + request.Country + "'"; } // If no country, league or primary position was selected, we put togehter a selected statement for the rest of the search criteria // that are less important else if (request.ContractStatus != null || request.MinimumAge != null || request.MaximumAge != null || request.SecondaryPosition != null || request.InjuryStatus != null || request.HandPreference != null || request.MinimumHeight != null || request.MaximumWeight != null || request.StrengthsList.Count != 0) { if (request.ContractStatus != null) { sqlSelectStatement = " p.contractstatus = '" + request.ContractStatus + "'" + " and p.isAvailable = 1"; } if (request.MinimumAge != null) { int timeNow = DateTime.Now.Year; int?year = timeNow - request.MinimumAge; if (sqlSelectStatement == "") { sqlSelectStatement = " p.year <= " + year + " and p.isAvailable = 1"; } else { sqlSelectStatement += " or p.year <= " + year + " and p.isAvailable = 1"; } } if (request.MaximumAge != null) { int timeNow = DateTime.Now.Year; int?year = timeNow - request.MaximumAge; if (sqlSelectStatement == "") { sqlSelectStatement = " p.year >= " + year + " and p.isAvailable = 1"; } else { sqlSelectStatement += " or p.year >= " + year + " and p.isAvailable = 1"; } } if (request.SecondaryPosition != null) { if (sqlSelectStatement == "") { sqlSelectStatement = " p.SecondaryPosition = '" + request.SecondaryPosition + "'" + " and p.isAvailable = 1"; } else { sqlSelectStatement += " or p.SecondaryPosition = '" + request.SecondaryPosition + "'" + " and p.isAvailable = 1"; } } if (request.HandPreference != null) { if (sqlSelectStatement == "") { sqlSelectStatement = " p.preferredhand = '" + request.HandPreference + "'" + " and p.isAvailable = 1"; } else { sqlSelectStatement += " or p.preferredhand = '" + request.HandPreference + "'" + " and p.isAvailable = 1"; } } if (request.InjuryStatus != null) { if (sqlSelectStatement == "") { sqlSelectStatement = " p.Injurystatus = '" + request.InjuryStatus + "'" + " and p.isAvailable = 1"; } else { sqlSelectStatement += " or p.Injurystatus = '" + request.InjuryStatus + "'" + " and p.isAvailable = 1"; } } if (request.MinimumHeight != null) { if (sqlSelectStatement == "") { sqlSelectStatement = " p.height >= " + request.MinimumHeight + " and p.isAvailable = 1"; } else { sqlSelectStatement += " or p.height >= " + request.MinimumHeight + " and p.isAvailable = 1"; } } if (request.MaximumWeight != null) { if (sqlSelectStatement == "") { sqlSelectStatement = " p.height >= " + request.MinimumHeight + " and p.isAvailable = 1"; } else { sqlSelectStatement += " or p.weight <= " + request.MaximumWeight + " and p.isAvailable = 1"; } } if (request.StrengthsList.Count != 0) { foreach (string item in request.StrengthsList) { if (sqlSelectStatement == "") { sqlSelectStatement += " s.name = '" + item + "'" + " and p.isAvailable = 1"; } else { sqlSelectStatement += " or s.name = '" + item + "'" + " and p.isAvailable = 1"; } } } } playerList = _playerRepos.GetBySearchCriteria(sqlSelectStatement).ToList(); // Now we check for which values match the search criterias and calculate the percentage they match with of total search criteria // selected and order the list by the percentage and return the finished list playerList = CheckWhichCriteriaMatchAndCalculatePercentage(playerList, request); SortListByPercentage sort = new SortListByPercentage(); playerList.Sort(sort.Compare); return(playerList); }
// Method that check which criteria match and how many criterias were selected and calculate percentage of match on each player. private List <Player> CheckWhichCriteriaMatchAndCalculatePercentage(List <Player> playerList, SearchCriteriaForPlayer request) { foreach (Player player in playerList) { int criteriaNumber = 0; int matchedCriteriaNumber = 0; if (request.Country != null) { criteriaNumber += 1; if (request.Country == player.Country) { matchedCriteriaNumber += 1; } } if (request.League != null) { criteriaNumber += 1; if (request.League == player.League) { matchedCriteriaNumber += 1; } } if (request.PrimaryPosition != null) { criteriaNumber += 1; if (request.PrimaryPosition == player.PrimaryPosition) { matchedCriteriaNumber += 1; } } if (request.SecondaryPosition != null) { criteriaNumber += 1; if (request.SecondaryPosition == player.SecondaryPosition) { matchedCriteriaNumber += 1; } } if (request.ContractStatus != null) { criteriaNumber += 1; if (request.ContractStatus == player.ContractStatus) { matchedCriteriaNumber += 1; } } if (request.MinimumAge != null) { int timeNow = DateTime.Now.Year; int minYear = timeNow - player.Year; criteriaNumber += 1; if (request.MinimumAge <= minYear) { matchedCriteriaNumber += 1; } } if (request.MaximumAge != null) { int timeNow = DateTime.Now.Year; int maxYear = timeNow - player.Year; criteriaNumber += 1; if (request.MaximumAge >= maxYear) { matchedCriteriaNumber += 1; } } if (request.InjuryStatus != null) { criteriaNumber += 1; if (request.InjuryStatus == player.InjuryStatus) { matchedCriteriaNumber += 1; } } if (request.HandPreference != null) { criteriaNumber += 1; if (request.HandPreference == player.PreferredHand) { matchedCriteriaNumber += 1; } } if (request.MinimumHeight != null) { criteriaNumber += 1; if (request.MinimumHeight <= player.Height) { matchedCriteriaNumber += 1; } } if (request.MaximumWeight != null) { criteriaNumber += 1; if (request.MaximumWeight >= player.Weight) { matchedCriteriaNumber += 1; } } if (request.StrengthsList.Count > 0) { foreach (string rList in request.StrengthsList) { criteriaNumber += 1; foreach (string pList in player.StrengthList) { if (rList == pList) { matchedCriteriaNumber += 1; } } } } player.CalculatePercentage(matchedCriteriaNumber, criteriaNumber); } return(playerList); }
public IActionResult SearchPlayers([FromQuery] SearchCriteriaForPlayer request) { return(Ok(_playerLogic.HandleSearchAlgorithm(request))); }