73 lines
2.2 KiB
C#
73 lines
2.2 KiB
C#
using AS1024.GeoFeed.Core.Interfaces;
|
|
using AS1024.GeoFeed.Models;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.Hosting;
|
|
using AS1024.GeoFeed.Core.Tools;
|
|
|
|
namespace AS1024.GeoFeed.Core.GeoFeedSqliteLocalCache
|
|
{
|
|
public class GeoFeedSqliteCache : IGeoFeedPersistentCacheProvider
|
|
{
|
|
protected readonly GeoFeedCacheDbContext dbContext;
|
|
private readonly IGeoFeedProvider feedProvider;
|
|
|
|
public GeoFeedSqliteCache(GeoFeedCacheDbContext geoFeedCacheDb,
|
|
IHost host,
|
|
IGeoFeedProvider provider)
|
|
{
|
|
dbContext = geoFeedCacheDb;
|
|
feedProvider = provider;
|
|
}
|
|
|
|
public string ProviderName => "sqlite";
|
|
|
|
public async Task<bool> CacheGeoFeed(IList<IPGeoFeed> pGeoFeeds)
|
|
{
|
|
await DBContextMigrate();
|
|
List<GeoFeedCacheEntry> geoFeedCacheEntry = [];
|
|
|
|
var results = pGeoFeeds.ToList();
|
|
|
|
results.ForEach(x =>
|
|
{
|
|
geoFeedCacheEntry.Add(new()
|
|
{
|
|
Prefix = x.Prefix,
|
|
GeolocCity = x.GeolocCity,
|
|
GeolocCountry = x.GeolocCountry,
|
|
GeolocHasLocation = x.GeolocHasLocation,
|
|
GeolocPostalCode = x.GeolocPostalCode,
|
|
GeolocRegion = x.GeolocRegion
|
|
});
|
|
});
|
|
|
|
if (dbContext.GeoFeedCacheEntries.Any())
|
|
{
|
|
dbContext.GeoFeedCacheEntries.RemoveRange(dbContext.GeoFeedCacheEntries.ToArray());
|
|
}
|
|
await dbContext.AddRangeAsync(geoFeedCacheEntry);
|
|
await dbContext.SaveChangesAsync();
|
|
|
|
return true;
|
|
}
|
|
|
|
public string GetGeoFeed()
|
|
{
|
|
var results =
|
|
dbContext.GeoFeedCacheEntries.ToList();
|
|
List<IPGeoFeed> cachedData = [];
|
|
results.ForEach(cachedData.Add);
|
|
|
|
return cachedData.ToGeoFeedCsv(true, true);
|
|
}
|
|
|
|
private async Task DBContextMigrate()
|
|
{
|
|
if (dbContext.Database.GetPendingMigrations().Any())
|
|
{
|
|
await dbContext.Database.MigrateAsync();
|
|
}
|
|
}
|
|
}
|
|
}
|