-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add DbContextFactory for improved scalability in HouseScoutContext
- Loading branch information
Showing
4 changed files
with
117 additions
and
107 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,46 @@ | ||
using SharedDependencies.Model; | ||
using Microsoft.EntityFrameworkCore; | ||
using SharedDependencies.Services; | ||
|
||
namespace DiscordBot.Filters; | ||
|
||
public class DataFilter | ||
namespace DiscordBot.Filters | ||
{ | ||
private HouseScoutContext _context; | ||
|
||
public DataFilter(HouseScoutContext context) | ||
{ | ||
_context = context; | ||
} | ||
|
||
public List<Estate> SurfacePriceFilter( | ||
int priceMin, | ||
int priceMax, | ||
int surfaceMin, | ||
int surfaceMax, | ||
bool isNewUser | ||
) | ||
public class DataFilter | ||
{ | ||
var query = _context.Estates.AsQueryable(); | ||
private readonly IDbContextFactory<HouseScoutContext> _contextFactory; | ||
|
||
// we want to process only new data if user is not new, | ||
// and all data if user is new | ||
if (!isNewUser) | ||
public DataFilter(IDbContextFactory<HouseScoutContext> contextFactory) | ||
{ | ||
query = query.Where(e => e.IsNew); | ||
_contextFactory = contextFactory; | ||
} | ||
query = query.Where(e => | ||
e.Price >= priceMin | ||
&& e.Price <= priceMax | ||
&& e.Surface >= surfaceMin | ||
&& e.Surface <= surfaceMax | ||
); | ||
|
||
return query.ToList(); | ||
public List<Estate> SurfacePriceFilter( | ||
int priceMin, | ||
int priceMax, | ||
int surfaceMin, | ||
int surfaceMax, | ||
bool isNewUser | ||
) | ||
{ | ||
using (var context = _contextFactory.CreateDbContext()) | ||
{ | ||
var query = context.Estates.AsQueryable(); | ||
|
||
// Process only new data if the user is not new, | ||
// and all data if the user is new | ||
if (!isNewUser) | ||
{ | ||
query = query.Where(e => e.IsNew); | ||
} | ||
|
||
query = query.Where(e => | ||
e.Price >= priceMin | ||
&& e.Price <= priceMax | ||
&& e.Surface >= surfaceMin | ||
&& e.Surface <= surfaceMax | ||
); | ||
|
||
return query.ToList(); | ||
} | ||
} | ||
} | ||
} | ||
} |
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