sqlite functions are now (mostly) async

This commit is contained in:
Jeddunk 2021-03-21 16:55:58 +01:00
parent 3bee1a5508
commit c823aa15fb

View File

@ -1,7 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq;
using System.Net.Http; using System.Net.Http;
using System.Text.Json; using System.Text.Json;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
@ -86,7 +85,7 @@ namespace GoldbergGUI.Core.Services
private IMvxLog _log; private IMvxLog _log;
private SQLiteConnection _db; private SQLiteAsyncConnection _db;
public async Task Initialize(IMvxLog log) public async Task Initialize(IMvxLog log)
{ {
@ -98,10 +97,14 @@ namespace GoldbergGUI.Core.Services
} }
_log = log; _log = log;
_db = new SQLiteConnection(Database); _db = new SQLiteAsyncConnection(Database);
_db.CreateTable<SteamApp>(); //_db.CreateTable<SteamApp>();
await _db.CreateTableAsync<SteamApp>()
//.ContinueWith(x => _log.Debug("Table success!"))
.ConfigureAwait(false);
if (DateTime.Now.Subtract(File.GetLastWriteTimeUtc(Database)).TotalDays >= 1 || !_db.Table<SteamApp>().Any()) var countAsync = await _db.Table<SteamApp>().CountAsync().ConfigureAwait(false);
if (DateTime.Now.Subtract(File.GetLastWriteTimeUtc(Database)).TotalDays >= 1 || countAsync == 0)
{ {
foreach (var (appType, steamCache) in _caches) foreach (var (appType, steamCache) in _caches)
{ {
@ -131,15 +134,16 @@ namespace GoldbergGUI.Core.Services
cache.Add(steamApp); cache.Add(steamApp);
} }
_db.InsertAll(cache); await _db.InsertAllAsync(cache).ConfigureAwait(false);
} }
} }
} }
public IEnumerable<SteamApp> GetListOfAppsByName(string name) public IEnumerable<SteamApp> GetListOfAppsByName(string name)
{ {
var listOfAppsByName = _db.Table<SteamApp>() var query = _db.Table<SteamApp>()
.Where(x => x.type == AppTypeGame).Search(x => x.Name) .Where(x => x.type == AppTypeGame).ToListAsync().Result;
var listOfAppsByName = query.Search(x => x.Name)
.SetCulture(StringComparison.OrdinalIgnoreCase) .SetCulture(StringComparison.OrdinalIgnoreCase)
.ContainingAll(name.Split(' ')); .ContainingAll(name.Split(' '));
return listOfAppsByName; return listOfAppsByName;
@ -150,7 +154,7 @@ namespace GoldbergGUI.Core.Services
_log.Info($"Trying to get app {name}"); _log.Info($"Trying to get app {name}");
var comparableName = PrepareStringToCompare(name); var comparableName = PrepareStringToCompare(name);
var app = _db.Table<SteamApp>() var app = _db.Table<SteamApp>()
.FirstOrDefault(x => x.type == AppTypeGame && x.ComparableName.Equals(comparableName)); .FirstOrDefaultAsync(x => x.type == AppTypeGame && x.ComparableName.Equals(comparableName)).Result;
if (app != null) _log.Info($"Successfully got app {app}"); if (app != null) _log.Info($"Successfully got app {app}");
return app; return app;
} }
@ -159,7 +163,7 @@ namespace GoldbergGUI.Core.Services
{ {
_log.Info($"Trying to get app with ID {appid}"); _log.Info($"Trying to get app with ID {appid}");
var app = _db.Table<SteamApp>().Where(x => x.type == AppTypeGame) var app = _db.Table<SteamApp>().Where(x => x.type == AppTypeGame)
.FirstOrDefault(x => x.AppId.Equals(appid)); .FirstOrDefaultAsync(x => x.AppId.Equals(appid)).Result;
if (app != null) _log.Info($"Successfully got app {app}"); if (app != null) _log.Info($"Successfully got app {app}");
return app; return app;
} }
@ -174,19 +178,19 @@ namespace GoldbergGUI.Core.Services
var steamAppDetails = await task.ConfigureAwait(true); var steamAppDetails = await task.ConfigureAwait(true);
if (steamAppDetails.Type == AppTypeGame) if (steamAppDetails.Type == AppTypeGame)
{ {
steamAppDetails.DLC.ForEach(x => steamAppDetails.DLC.ForEach(async x =>
{ {
var result = _db.Table<SteamApp>().Where(z => z.type == AppTypeDlc) var result = await _db.Table<SteamApp>().Where(z => z.type == AppTypeDlc)
.FirstOrDefault(y => y.AppId.Equals(x)) .FirstOrDefaultAsync(y => y.AppId.Equals(x)).ConfigureAwait(true)
?? new SteamApp {AppId = x, Name = $"Unknown DLC {x}"}; ?? new SteamApp {AppId = x, Name = $"Unknown DLC {x}"};
dlcList.Add(result); dlcList.Add(result);
_log.Debug($"{result.AppId}={result.Name}");
}); });
dlcList.ForEach(x => _log.Debug($"{x.AppId}={x.Name}"));
_log.Info("Got DLC successfully..."); _log.Info("Got DLC successfully...");
// Get DLC from SteamDB // Get DLC from SteamDB
// Get Cloudflare cookie // Get Cloudflare cookie (not implemented)
// Scrape and parse HTML page // Scrape and parse HTML page
// Add missing to DLC list // Add missing to DLC list