Initial work on getting minimal API to work
This commit is contained in:
72
AS1024.GeoFeed.Core.SqliteGeoFeedCache/GeoFeedSqliteCache.cs
Normal file
72
AS1024.GeoFeed.Core.SqliteGeoFeedCache/GeoFeedSqliteCache.cs
Normal file
@@ -0,0 +1,72 @@
|
||||
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();
|
||||
}
|
||||
|
||||
private async Task DBContextMigrate()
|
||||
{
|
||||
if (dbContext.Database.GetPendingMigrations().Any())
|
||||
{
|
||||
await dbContext.Database.MigrateAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user