Skip to content

Commit

Permalink
Ensure that soft deleted players are not returned in queries #555
Browse files Browse the repository at this point in the history
  • Loading branch information
sussexrick committed Oct 21, 2023
1 parent 65adfe3 commit 0e19295
Show file tree
Hide file tree
Showing 11 changed files with 76 additions and 50 deletions.
Binary file not shown.
Binary file not shown.
10 changes: 10 additions & 0 deletions Stoolball.Data.SqlServer/SqlConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,14 @@ public static class Tables
public const string UmbracoNode = "umbracoNode";
public const string UmbracoMember = "cmsMember";
}

public static class Views
{
internal const string _viewPrefix = "vw_Stoolball_";

/// <summary>
/// Player identities with player information, excluding those which have been soft deleted.
/// </summary>
public const string PlayerIdentity = _viewPrefix + "PlayerIdentity";
}
}
30 changes: 12 additions & 18 deletions Stoolball.Data.SqlServer/SqlServerMatchDataSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,17 +124,14 @@ WHERE LOWER(m.MatchRoute) = @Route
$@"SELECT i.MatchInningsId, i.Byes, i.Wides, i.NoBalls, i.BonusOrPenaltyRuns, i.Runs, i.Wickets, i.InningsOrderInMatch,
i.BattingMatchTeamId, i.BowlingMatchTeamId,
pi.PlayerInningsId, pi.BattingPosition, pi.DismissalType, pi.RunsScored, pi.BallsFaced,
bat.PlayerIdentityId, bat.PlayerIdentityName, bat2.PlayerId, bat2.PlayerRoute,
field.PlayerIdentityId, field.PlayerIdentityName, field2.PlayerId, field2.PlayerRoute,
bowl.PlayerIdentityId, bowl.PlayerIdentityName, bowl2.PlayerId, bowl2.PlayerRoute
bat.PlayerIdentityId, bat.PlayerIdentityName, bat.PlayerId, bat.PlayerRoute,
field.PlayerIdentityId, field.PlayerIdentityName, field.PlayerId, field.PlayerRoute,
bowl.PlayerIdentityId, bowl.PlayerIdentityName, bowl.PlayerId, bowl.PlayerRoute
FROM {Tables.MatchInnings} i
LEFT JOIN {Tables.PlayerInnings} pi ON i.MatchInningsId = pi.MatchInningsId
LEFT JOIN {Tables.PlayerIdentity} bat ON pi.BatterPlayerIdentityId = bat.PlayerIdentityId
LEFT JOIN {Tables.Player} bat2 ON bat.PlayerId = bat2.PlayerId
LEFT JOIN {Tables.PlayerIdentity} field ON pi.DismissedByPlayerIdentityId = field.PlayerIdentityId
LEFT JOIN {Tables.Player} field2 ON field.PlayerId = field2.PlayerId
LEFT JOIN {Tables.PlayerIdentity} bowl ON pi.BowlerPlayerIdentityId = bowl.PlayerIdentityId
LEFT JOIN {Tables.Player} bowl2 ON bowl.PlayerId = bowl2.PlayerId
LEFT JOIN {Views.PlayerIdentity} bat ON pi.BatterPlayerIdentityId = bat.PlayerIdentityId
LEFT JOIN {Views.PlayerIdentity} field ON pi.DismissedByPlayerIdentityId = field.PlayerIdentityId
LEFT JOIN {Views.PlayerIdentity} bowl ON pi.BowlerPlayerIdentityId = bowl.PlayerIdentityId
WHERE i.MatchId = @MatchId
ORDER BY i.InningsOrderInMatch, pi.BattingPosition",
(innings, matchTeamIds, batting, batter, dismissedBy, bowledBy) =>
Expand Down Expand Up @@ -215,13 +212,12 @@ WHERE LOWER(m.MatchRoute) = @Route
os.OverSetId, os.OverSetNumber, os.Overs, os.BallsPerOver,
o.OverNumber, o.BallsBowled, o.NoBalls, o.Wides, o.RunsConceded,
pi.PlayerIdentityId, pi.PlayerIdentityName,
p.PlayerId, p.PlayerRoute,
pi.PlayerId, pi.PlayerRoute,
pi.TeamId
FROM {Tables.MatchInnings} i
INNER JOIN {Tables.OverSet} os ON i.MatchInningsId = os.MatchInningsId
LEFT JOIN {Tables.Over} o ON os.OverSetId = o.OverSetId
LEFT JOIN {Tables.PlayerIdentity} pi ON o.BowlerPlayerIdentityId = pi.PlayerIdentityId
LEFT JOIN {Tables.Player} p ON pi.PlayerId = p.PlayerId
LEFT JOIN {Views.PlayerIdentity} pi ON o.BowlerPlayerIdentityId = pi.PlayerIdentityId
WHERE i.MatchId = @MatchId
ORDER BY i.InningsOrderInMatch, o.OverNumber",
(innings, overSet, over, bowlerPlayerIdentity, bowlerPlayer, team) =>
Expand Down Expand Up @@ -274,11 +270,10 @@ WHERE LOWER(m.MatchRoute) = @Route
($@"SELECT bf.BowlingFiguresId, bf.Overs, bf.Maidens, bf.RunsConceded, bf.Wickets,
bf.MatchInningsId,
pi.PlayerIdentityId, pi.PlayerIdentityName,
p.PlayerId, p.PlayerRoute,
pi.PlayerId, pi.PlayerRoute,
pi.TeamId
FROM {Tables.BowlingFigures} bf
INNER JOIN {Tables.PlayerIdentity} pi ON bf.BowlerPlayerIdentityId = pi.PlayerIdentityId
INNER JOIN {Tables.Player} p ON pi.PlayerId = p.PlayerId
INNER JOIN {Views.PlayerIdentity} pi ON bf.BowlerPlayerIdentityId = pi.PlayerIdentityId
WHERE bf.MatchInningsId IN @MatchInningsIds
ORDER BY bf.MatchInningsId, bf.BowlingOrder",
(bowling, innings, bowlerPlayerIdentity, bowlerPlayer, team) =>
Expand All @@ -301,12 +296,11 @@ WHERE bf.MatchInningsId IN @MatchInningsIds
matchToReturn.Awards = (await connection.QueryAsync<MatchAward, Award, PlayerIdentity, Player, Team, MatchAward>(
$@"SELECT ma.AwardedToId, ma.Reason, a.AwardName,
pi.PlayerIdentityId, pi.PlayerIdentityName,
p.PlayerId, p.PlayerRoute,
pi.PlayerId, pi.PlayerRoute,
pi.TeamId
FROM {Tables.AwardedTo} ma
INNER JOIN {Tables.Award} a ON ma.AwardId = a.AwardId
INNER JOIN {Tables.PlayerIdentity} pi ON ma.PlayerIdentityId = pi.PlayerIdentityId
INNER JOIN {Tables.Player} p ON pi.PlayerId = p.PlayerId
INNER JOIN {Views.PlayerIdentity} pi ON ma.PlayerIdentityId = pi.PlayerIdentityId
WHERE ma.MatchId = @MatchId
ORDER BY a.AwardName",
(matchAward, award, playerIdentity, player, team) =>
Expand Down
10 changes: 5 additions & 5 deletions Stoolball.Data.SqlServer/SqlServerMatchRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -803,9 +803,9 @@ public async Task<MatchInnings> UpdateBattingScorecard(Match match, Guid matchIn
field.PlayerIdentityId, field.PlayerIdentityName,
bowl.PlayerIdentityId, bowl.PlayerIdentityName
FROM {Tables.PlayerInnings} i
INNER JOIN {Tables.PlayerIdentity} bat ON i.BatterPlayerIdentityId = bat.PlayerIdentityId
LEFT JOIN {Tables.PlayerIdentity} field ON i.DismissedByPlayerIdentityId = field.PlayerIdentityId
LEFT JOIN {Tables.PlayerIdentity} bowl ON i.BowlerPlayerIdentityId = bowl.PlayerIdentityId
INNER JOIN {Views.PlayerIdentity} bat ON i.BatterPlayerIdentityId = bat.PlayerIdentityId
LEFT JOIN {Views.PlayerIdentity} field ON i.DismissedByPlayerIdentityId = field.PlayerIdentityId
LEFT JOIN {Views.PlayerIdentity} bowl ON i.BowlerPlayerIdentityId = bowl.PlayerIdentityId
WHERE i.MatchInningsId = @MatchInningsId",
(playerInnings, batter, fielder, bowler) =>
{
Expand Down Expand Up @@ -1070,7 +1070,7 @@ public async Task<MatchInnings> UpdateBowlingScorecard(Match match, Guid matchIn
$@"SELECT o.OverId, o.OverNumber, o.BallsBowled, o.NoBalls, o.Wides, o.RunsConceded,
o.OverSetId,
p.PlayerIdentityId, p.PlayerIdentityName
FROM {Tables.Over} o INNER JOIN {Tables.PlayerIdentity} p ON o.BowlerPlayerIdentityId = p.PlayerIdentityId
FROM {Tables.Over} o INNER JOIN {Views.PlayerIdentity} p ON o.BowlerPlayerIdentityId = p.PlayerIdentityId
WHERE o.MatchInningsId = @MatchInningsId",
(over, overSet, playerIdentity) =>
{
Expand Down Expand Up @@ -1346,7 +1346,7 @@ await _dapperWrapper.ExecuteAsync($@"UPDATE {Tables.Match} SET

var awardsBefore = await _dapperWrapper.QueryAsync<(Guid playerIdentityId, string playerIdentityName, Guid teamId)>(
$@"SELECT pi.PlayerIdentityId, pi.PlayerIdentityName, pi.TeamId
FROM {Tables.AwardedTo} a INNER JOIN {Tables.PlayerIdentity} pi ON a.PlayerIdentityId = pi.PlayerIdentityId
FROM {Tables.AwardedTo} a INNER JOIN {Views.PlayerIdentity} pi ON a.PlayerIdentityId = pi.PlayerIdentityId
WHERE a.MatchId = @MatchId",
new { auditableMatch.MatchId },
transaction).ConfigureAwait(false);
Expand Down
13 changes: 7 additions & 6 deletions Stoolball.Data.SqlServer/SqlServerPlayerDataSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public async Task<List<PlayerIdentity>> ReadPlayerIdentities(PlayerFilter? filte
var sql = $@"SELECT stats.PlayerIdentityId, pi.PlayerIdentityName, pi.RouteSegment, {PROBABILITY_CALCULATION} AS Probability,
COUNT(DISTINCT MatchId) AS TotalMatches, MIN(MatchStartTime) AS FirstPlayed, MAX(MatchStartTime) AS LastPlayed,
stats.PlayerId, stats.PlayerRoute, stats.TeamId, stats.TeamName
FROM {Tables.PlayerIdentity} pi INNER JOIN {Tables.PlayerInMatchStatistics} AS stats ON pi.PlayerIdentityId = stats.PlayerIdentityId
FROM {Views.PlayerIdentity} pi INNER JOIN {Tables.PlayerInMatchStatistics} AS stats ON pi.PlayerIdentityId = stats.PlayerIdentityId
<<WHERE>>
GROUP BY stats.PlayerId, stats.PlayerRoute, stats.PlayerIdentityId, pi.PlayerIdentityName, pi.RouteSegment, stats.TeamId, stats.TeamName
ORDER BY stats.TeamId ASC, {PROBABILITY_CALCULATION} DESC, pi.PlayerIdentityName ASC";
Expand Down Expand Up @@ -201,13 +201,13 @@ public async Task<List<PlayerIdentity>> ReadPlayerIdentities(PlayerFilter? filte
// Updates to PlayerInMatchStatistics are done asynchronously and the data will not be updated by the time this is called again.

var playerData = await connection.QueryAsync<Player, PlayerIdentity, Team, Player>(
$@"SELECT p.PlayerId, p.PlayerRoute, p.MemberKey,
$@"SELECT pi.PlayerId, pi.PlayerRoute, pi.MemberKey,
pi.PlayerIdentityId, pi.PlayerIdentityName,
(SELECT COUNT(DISTINCT MatchId) AS TotalMatches FROM {Tables.PlayerInMatchStatistics} WHERE PlayerIdentityId = pi.PlayerIdentityId {where}) AS TotalMatches,
(SELECT MIN(MatchStartTime) AS FirstPlayed FROM {Tables.PlayerInMatchStatistics} WHERE PlayerIdentityId = pi.PlayerIdentityId {where}) AS FirstPlayed,
(SELECT MAX(MatchStartTime) AS LastPlayed FROM {Tables.PlayerInMatchStatistics} WHERE PlayerIdentityId = pi.PlayerIdentityId {where}) AS LastPlayed,
t.TeamId, tv.TeamName, t.TeamRoute
FROM {Tables.Player} p INNER JOIN {Tables.PlayerIdentity} pi ON p.PlayerId = pi.PlayerId
FROM {Views.PlayerIdentity} pi
INNER JOIN {Tables.Team} t ON pi.TeamId = t.TeamId
INNER JOIN {Tables.TeamVersion} tv ON t.TeamId = tv.TeamId
WHERE LOWER(PlayerRoute) = @Route
Expand Down Expand Up @@ -241,7 +241,8 @@ WHERE LOWER(PlayerRoute) = @Route
return await connection.QuerySingleOrDefaultAsync<Player>(
$@"SELECT TOP 1 PlayerRoute
FROM {Tables.Player}
WHERE MemberKey = @MemberKey",
WHERE MemberKey = @MemberKey
AND Deleted = 0",
new { MemberKey = key });
}
}
Expand Down Expand Up @@ -280,12 +281,12 @@ WHERE LOWER(PlayerRoute) = @Route
pi.PlayerId,
t.TeamId, tv.TeamName, t.TeamRoute,
c.ClubId, cv.ClubName, c.ClubRoute
FROM {Tables.PlayerIdentity} pi INNER JOIN {Tables.Team} t ON pi.TeamId = t.TeamId
FROM {Views.PlayerIdentity} pi INNER JOIN {Tables.Team} t ON pi.TeamId = t.TeamId
INNER JOIN {Tables.TeamVersion} tv ON t.TeamId = tv.TeamId
LEFT JOIN {Tables.Club} c ON t.ClubId = c.ClubId
LEFT JOIN {Tables.ClubVersion} cv ON c.ClubId = cv.ClubId
WHERE LOWER(pi.RouteSegment) = @RouteSegment AND LOWER(t.TeamRoute) = @TeamRoute
AND tv.TeamVersionId = (SELECT TOP 1 TeamVersionId FROM { Tables.TeamVersion } WHERE TeamId = t.TeamId ORDER BY ISNULL(UntilDate, '{SqlDateTime.MaxValue.Value.Date.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture)}') DESC)
AND tv.TeamVersionId = (SELECT TOP 1 TeamVersionId FROM {Tables.TeamVersion} WHERE TeamId = t.TeamId ORDER BY ISNULL(UntilDate, '{SqlDateTime.MaxValue.Value.Date.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture)}') DESC)
AND (cv.ClubVersionId = (SELECT TOP 1 ClubVersionId FROM {Tables.ClubVersion} WHERE ClubId = c.ClubId ORDER BY ISNULL(UntilDate, '{SqlDateTime.MaxValue.Value.Date.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture)}') DESC) OR cv.ClubVersionId IS NULL)",
(identity, player, team, club) =>
{
Expand Down
Loading

0 comments on commit 0e19295

Please sign in to comment.