-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Switched recent public games to direct SQL query instead of a nasty L…
…INQ query
- Loading branch information
1 parent
5f417cb
commit aecb554
Showing
6 changed files
with
108 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
Source/BusinessLogic/DataAccess/Repositories/IPlayedGameRepository.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using System.Collections.Generic; | ||
using BusinessLogic.Models.Games; | ||
using BusinessLogic.Models.PlayedGames; | ||
|
||
namespace BusinessLogic.DataAccess.Repositories | ||
{ | ||
public interface IPlayedGameRepository | ||
{ | ||
List<PublicGameSummary> GetRecentPublicGames(RecentlyPlayedGamesFilter filter); | ||
} | ||
} |
87 changes: 87 additions & 0 deletions
87
Source/BusinessLogic/DataAccess/Repositories/PlayedGameRepository.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Data.SqlClient; | ||
using System.Linq; | ||
using BusinessLogic.Models.Games; | ||
using BusinessLogic.Models.PlayedGames; | ||
|
||
namespace BusinessLogic.DataAccess.Repositories | ||
{ | ||
public class PlayedGameRepository : IPlayedGameRepository | ||
{ | ||
private readonly IDataContext _dataContext; | ||
|
||
public PlayedGameRepository(IDataContext dataContext) | ||
{ | ||
_dataContext = dataContext; | ||
} | ||
|
||
private const string RAW_SQL = @"WITH GamingGroupsWithGamesInLastTwoDays AS ( | ||
SELECT DISTINCT PlayedGame.GamingGroupId, MAX(PlayedGame.ID) AS PlayedGameId | ||
FROM PlayedGame | ||
{0} | ||
WHERE PlayedGame.DatePlayed BETWEEN @MinDate AND @MaxDate | ||
{1} | ||
GROUP BY PlayedGame.GamingGroupId | ||
), | ||
WinningPlayers AS ( | ||
SELECT MAX(PlayerGameResult.PlayerId) AS WinningPlayerId, PlayerGameResult.PlayedGameId | ||
FROM PlayerGameResult INNER JOIN GamingGroupsWithGamesInLastTwoDays | ||
ON PlayerGameResult.PlayedGameId = GamingGroupsWithGamesInLastTwoDays.PlayedGameId | ||
WHERE PlayerGameREsult.GameRank = 1 | ||
GROUP BY PlayerGameResult.PlayedGameId) | ||
SELECT TOP {2} PlayedGame.Id AS PlayedGameId, | ||
PlayedGame.GamingGroupId, | ||
PlayedGame.DatePlayed, | ||
PlayedGame.WinnerType, | ||
GameDefinition.Id AS GameDefinitionId, | ||
WinningPlayers.WinningPlayerId, | ||
Player.Name AS WinningPlayerName, | ||
GamingGroup.Name AS GamingGroupName, | ||
BoardGameGeekGameDefinition.Thumbnail AS ThumbnailImageUrl, | ||
GameDefinition.Name AS GameDefinitionName | ||
FROM GamingGroupsWithGamesInLastTwoDays | ||
INNER JOIN PlayedGame ON PlayedGame.Id = GamingGroupsWithGamesInLastTwoDays.PlayedGameId | ||
INNER JOIN GamingGroup ON PlayedGame.GamingGroupId = GamingGroup.Id | ||
INNER JOIN GameDefinition ON PlayedGame.GameDefinitionId = GameDefinition.Id | ||
INNER JOIN BoardGameGeekGameDefinition ON GameDefinition.BoardGameGeekGameDefinitionId = BoardGameGeekGameDefinition.Id | ||
INNER JOIN WinningPlayers ON WinningPlayers.PlayedGameId = GamingGroupsWithGamesInLastTwoDays.PlayedGameId | ||
LEFT JOIN Player on WinningPlayers.WinningPlayerId = Player.Id | ||
ORDER BY PlayedGameId DESC"; | ||
|
||
public List<PublicGameSummary> GetRecentPublicGames(RecentlyPlayedGamesFilter filter) | ||
{ | ||
string boardGameGeekGameDefinitionInnerJoin = string.Empty; | ||
string boardGameGeekGameDefinitionIdPredicate = string.Empty; | ||
object[] sqlParams; | ||
if (filter.BoardGameGeekGameDefinitionId.HasValue) | ||
{ | ||
boardGameGeekGameDefinitionInnerJoin = | ||
"INNER JOIN BoardGameGeekGameDefinition ON BoardGameGeekGameDefinition.Id = GameDefinition.BoardGameGeekGameDefinitionId "; | ||
boardGameGeekGameDefinitionIdPredicate = | ||
"AND BoardGameGeekGameDefinition.Id = @BoardGameGeekGameDefinitionId "; | ||
|
||
sqlParams = new object[4]; | ||
sqlParams[3] = new SqlParameter("BoardGameGeekGameDefinitionId", filter.BoardGameGeekGameDefinitionId.Value); | ||
} | ||
else | ||
{ | ||
sqlParams = new object[3]; | ||
} | ||
sqlParams[0] = new SqlParameter("NumberOfResults", filter.NumberOfGamesToRetrieve); | ||
sqlParams[1] = new SqlParameter("MinDate", filter.MinDate ?? DateTime.UtcNow.AddYears(-10)); | ||
sqlParams[2] = new SqlParameter("MaxDate", filter.MaxDate); | ||
|
||
var formattedSql = string.Format(RAW_SQL, | ||
boardGameGeekGameDefinitionInnerJoin, | ||
boardGameGeekGameDefinitionIdPredicate, | ||
filter.NumberOfGamesToRetrieve); | ||
|
||
var data = _dataContext.MakeRawSqlQuery<PublicGameSummary>(formattedSql, sqlParams); | ||
|
||
var results = data.ToList(); | ||
return results; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters