Skip to content

Commit

Permalink
#5 Refactor a bit in the players service.
Browse files Browse the repository at this point in the history
  • Loading branch information
Adrian Fürschuß committed Nov 23, 2016
1 parent 1ccdbaa commit 952f778
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/Wuzlstats/Services/PlayersService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,21 @@ public PlayersService(Db db)
_db = db;
}

public async Task<IEnumerable<PlayerViewModel>> FindPlayersOfLeague(int leagueId, int? daysForStatistics)

private IQueryable<Game> FetchGames(int leagueId, int? daysForStatistics)
{
var gamesQuery = _db.Games.AsNoTracking().Where(x => x.LeagueId == leagueId);
if (daysForStatistics.HasValue)
if (!daysForStatistics.HasValue)
{
var date = DateTime.UtcNow.Date.AddDays(-daysForStatistics.Value);
gamesQuery = gamesQuery.Where(x => x.Date >= date);
return gamesQuery;
}
var date = DateTime.UtcNow.Date.AddDays(-daysForStatistics.Value);
return gamesQuery.Where(x => x.Date >= date);
}

public async Task<IEnumerable<PlayerViewModel>> FindPlayersOfLeague(int leagueId, int? daysForStatistics)
{
var gamesQuery = FetchGames(leagueId, daysForStatistics);

// EF7 beta4 does not support navigation properties in queries yet
// this complicates the code a lot, because we need joins :(
Expand Down Expand Up @@ -95,5 +102,6 @@ join player in _db.Players.AsNoTracking() on position.PlayerId equals player.Id
}
return players.OrderByDescending(x => x.LastGamePlayedOn);
}

}
}

0 comments on commit 952f778

Please sign in to comment.