Skip to content

Commit

Permalink
Bugfix/improve dbcontext handling (#211)
Browse files Browse the repository at this point in the history
* rework stuff

* remove pomelo

* Rework stuff
  • Loading branch information
idormenco authored Jul 5, 2024
1 parent 0083c11 commit 1d94580
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 19 deletions.
1 change: 0 additions & 1 deletion src/ElectionResults.API/ElectionResults.API.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
<PackageReference Include="CSharpFunctionalExtensions" Version="2.42.0" />
<PackageReference Include="Microsoft.Web.LibraryManager.Build" Version="2.1.175" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.6.2" />
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="8.0.2" />
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="8.0.6" />
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="8.0.6" />
<PackageReference Include="Microsoft.AspNetCore.Identity.UI" Version="8.0.6" />
Expand Down
20 changes: 10 additions & 10 deletions src/ElectionResults.API/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public void ConfigureServices(IServiceCollection services)

var connectionString = Configuration["ConnectionStrings:DefaultConnection"]!;

services.AddDbContextPool<ApplicationDbContext>(options =>
services.AddDbContext<ApplicationDbContext>(options =>
{
options.UseMySQL(connectionString);
});
Expand All @@ -83,17 +83,17 @@ public void ConfigureServices(IServiceCollection services)

private static void RegisterDependencies(IServiceCollection services, IConfiguration configuration)
{
services.AddTransient<IResultsAggregator, ResultsAggregator>();
services.AddScoped<IResultsAggregator, ResultsAggregator>();

services.AddTransient<IAuthorsRepository, AuthorsRepository>();
services.AddTransient<IWinnersAggregator, WinnersAggregator>();
services.AddScoped<IAuthorsRepository, AuthorsRepository>();
services.AddScoped<IWinnersAggregator, WinnersAggregator>();

services.AddTransient<IArticleRepository, ArticleRepository>();
services.AddTransient<IElectionsRepository, ElectionsRepository>();
services.AddTransient<ITerritoryRepository, TerritoryRepository>();
services.AddTransient<IPicturesRepository, PicturesRepository>();
services.AddTransient<IBallotsRepository, BallotsRepository>();
services.AddTransient<IPartiesRepository, PartiesRepository>();
services.AddScoped<IArticleRepository, ArticleRepository>();
services.AddScoped<IElectionsRepository, ElectionsRepository>();
services.AddScoped<ITerritoryRepository, TerritoryRepository>();
services.AddScoped<IPicturesRepository, PicturesRepository>();
services.AddScoped<IBallotsRepository, BallotsRepository>();
services.AddScoped<IPartiesRepository, PartiesRepository>();
services.Configure<AWSS3Settings>(configuration.GetSection(AWSS3Settings.SectionKey));
services.Configure<MemoryCacheSettings>(configuration.GetSection(MemoryCacheSettings.SectionKey));
}
Expand Down
15 changes: 7 additions & 8 deletions src/ElectionResults.Core/Elections/ResultsAggregator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ public async Task<Result<ElectionResponse>> GetBallotResults(ElectionResultsQuer
electionResponse.Aggregated = electionInfo.Aggregated;
electionResponse.Results = results;
electionResponse.Observation = await _dbContext.Observations.FirstOrDefaultAsync(o => o.BallotId == ballot.BallotId);

if (divisionTurnout != null)
{
electionResponse.Turnout = new ElectionTurnout
Expand Down Expand Up @@ -219,7 +220,7 @@ public async Task<Turnout> GetDivisionTurnout(ElectionResultsQuery query, Ballot
{
if (ballot.Election.Category == ElectionCategory.Local && !ballot.AllowsDivision(query.Division, query.LocalityId.GetValueOrDefault()) && !ballot.Election.Live)
{
return await RetrieveAggregatedTurnoutForCityHalls(query, ballot, _dbContext);
return await RetrieveAggregatedTurnoutForCityHalls(query, ballot);
}

var turnouts = await _dbContext.Turnouts
Expand Down Expand Up @@ -258,10 +259,9 @@ public async Task<Turnout> GetDivisionTurnout(ElectionResultsQuery query, Ballot
return divisionTurnout;
}

private static async Task<Turnout> RetrieveAggregatedTurnoutForCityHalls(ElectionResultsQuery query,
Ballot ballot, ApplicationDbContext dbContext)
private async Task<Turnout> RetrieveAggregatedTurnoutForCityHalls(ElectionResultsQuery query, Ballot ballot)
{
IQueryable<Turnout> queryable = dbContext.Turnouts
IQueryable<Turnout> queryable = _dbContext.Turnouts
.Where(t => t.BallotId == ballot.BallotId);

if (ballot.BallotType == BallotType.CountyCouncilPresident || ballot.BallotType == BallotType.CountyCouncil)
Expand Down Expand Up @@ -306,7 +306,7 @@ private async Task<LiveElectionInfo> GetCandidatesFromDb(ElectionResultsQuery qu
{
if (!ballot.AllowsDivision(query.Division, query.LocalityId.GetValueOrDefault()) && !ballot.Election.Live)
{
var aggregatedVotes = await RetrieveAggregatedVotes(_dbContext, query, ballot);
var aggregatedVotes = await RetrieveAggregatedVotes(query, ballot);
liveElectionInfo.Candidates = aggregatedVotes;
liveElectionInfo.Aggregated = true;

Expand Down Expand Up @@ -345,8 +345,7 @@ private async Task<List<CandidateResult>> GetCandidateResultsFromQueryAndBallot(
return results;
}

private async Task<List<CandidateResult>> RetrieveAggregatedVotes(ApplicationDbContext dbContext,
ElectionResultsQuery query, Ballot ballot)
private async Task<List<CandidateResult>> RetrieveAggregatedVotes(ElectionResultsQuery query, Ballot ballot)
{
switch (query.Division)
{
Expand Down Expand Up @@ -374,7 +373,7 @@ private async Task<List<CandidateResult>> RetrieveAggregatedVotes(ApplicationDbC
}
else
{
var resultsForElection = await dbContext.CandidateResults
var resultsForElection = await _dbContext.CandidateResults
.Include(c => c.Party)
.Where(c => c.BallotId == query.BallotId && c.Division == ElectionDivision.Locality)
.ToListAsync();
Expand Down

0 comments on commit 1d94580

Please sign in to comment.