Moved db acces to static class.

This commit is contained in:
Max 2023-05-29 16:14:26 +02:00
parent 2355da58b2
commit 323486f36d
5 changed files with 159 additions and 143 deletions

View File

@ -9,25 +9,23 @@ using Microsoft.Data.Sqlite;
using Serilog; using Serilog;
using SharpRss.Models; using SharpRss.Models;
namespace SharpRss.Services namespace SharpRss
{ {
internal class DatabaseService : IDisposable public static class DbAccess
{ {
internal DatabaseService() private static bool _isInitialized;
{ private static readonly string ConnectionString = $"Data Source={Path.Combine(Environment.CurrentDirectory, "sharp_rss.sqlite")};";
InitializeDb(); private const string GroupTable = "group_data";
} private const string FeedTable = "feed_data";
private readonly string _connectionString = $"Data Source={Path.Combine(Environment.CurrentDirectory, "sharp_rss.sqlite")};"; private const string FeedItemTable = "feed_item_data";
private readonly string _groupTable = "group_data";
private readonly string _feedTable = "feed_data";
private readonly string _feedItemTable = "feed_item_data";
// Groups // Groups
public async Task<HashSet<GroupModel>> GetGroupsAsync(string? groupId = null) public static async Task<HashSet<GroupModel>> GetGroupsAsync(string? groupId = null)
{ {
await using SqliteConnection dbc = new SqliteConnection(_connectionString); CheckInitialized();
await using SqliteConnection dbc = new SqliteConnection(ConnectionString);
dbc.Open(); dbc.Open();
await using SqliteCommand cmd = new SqliteCommand(groupId != null ? $"SELECT * FROM {_groupTable} WHERE id=@gId;" : $"SELECT * FROM {_groupTable}", dbc) await using SqliteCommand cmd = new SqliteCommand(groupId != null ? $"SELECT * FROM {GroupTable} WHERE id=@gId;" : $"SELECT * FROM {GroupTable}", dbc)
{ {
Parameters = Parameters =
{ {
@ -36,7 +34,7 @@ namespace SharpRss.Services
}; };
await using SqliteDataReader reader = await cmd.ExecuteReaderAsync(); await using SqliteDataReader reader = await cmd.ExecuteReaderAsync();
HashSet<GroupModel> groups = new HashSet<GroupModel>(); HashSet<GroupModel> groups = new HashSet<GroupModel>();
await using SqliteCommand cmdFeedCount = new SqliteCommand($"SELECT COUNT(*) FROM {_feedTable} WHERE group_id=@groupId", dbc); await using SqliteCommand cmdFeedCount = new SqliteCommand($"SELECT COUNT(*) FROM {FeedTable} WHERE group_id=@groupId", dbc);
while (reader.Read()) while (reader.Read())
{ {
cmdFeedCount.Parameters.Clear(); cmdFeedCount.Parameters.Clear();
@ -55,12 +53,12 @@ namespace SharpRss.Services
} }
return groups; return groups;
} }
public async Task<bool> SetGroupAsync(GroupModel groupModel) public static async Task<bool> SetGroupAsync(GroupModel groupModel)
{ {
bool result = false; bool result = false;
await using SqliteConnection dbc = new SqliteConnection(_connectionString); await using SqliteConnection dbc = new SqliteConnection(ConnectionString);
dbc.Open(); dbc.Open();
await using SqliteCommand cmd = new SqliteCommand($"INSERT OR REPLACE INTO {_groupTable} (id, hex_color, icon, name) VALUES (IFNULL((SELECT id FROM {_groupTable} WHERE name=@name), @id), @hexColor, @icon, @name)", dbc) await using SqliteCommand cmd = new SqliteCommand($"INSERT OR REPLACE INTO {GroupTable} (id, hex_color, icon, name) VALUES (IFNULL((SELECT id FROM {GroupTable} WHERE name=@name), @id), @hexColor, @icon, @name)", dbc)
{ {
Parameters = Parameters =
{ {
@ -75,13 +73,13 @@ namespace SharpRss.Services
result = true; result = true;
return result; return result;
} }
public async Task<bool> RemoveGroupAsync(GroupModel groupModel) public static async Task<bool> RemoveGroupAsync(GroupModel groupModel)
{ {
bool result = false; bool result = false;
await using SqliteConnection dbc = new SqliteConnection(_connectionString); await using SqliteConnection dbc = new SqliteConnection(ConnectionString);
dbc.Open(); dbc.Open();
// Remove the group and remove the feeds that were part of the group. // Remove the group and remove the feeds that were part of the group.
await using SqliteCommand cmd = new SqliteCommand($"DELETE FROM {_groupTable} WHERE id=@id; UPDATE {_feedTable} SET group_id=NULL WHERE group_id=@id", dbc) await using SqliteCommand cmd = new SqliteCommand($"DELETE FROM {GroupTable} WHERE id=@id; UPDATE {FeedTable} SET group_id=NULL WHERE group_id=@id", dbc)
{ {
Parameters = Parameters =
{ {
@ -99,12 +97,12 @@ namespace SharpRss.Services
/// </summary> /// </summary>
/// <param name="groupId">Empty = ungrouped feeds | null = all feeds | id = grouped feeds</param> /// <param name="groupId">Empty = ungrouped feeds | null = all feeds | id = grouped feeds</param>
/// <returns></returns> /// <returns></returns>
public async Task<HashSet<FeedModel>> GetFeedsAsync(string? groupId = null) public static async Task<HashSet<FeedModel>> GetFeedsAsync(string? groupId = null)
{ {
HashSet<FeedModel> feeds = new HashSet<FeedModel>(); HashSet<FeedModel> feeds = new HashSet<FeedModel>();
await using SqliteConnection dbc = new SqliteConnection(_connectionString); await using SqliteConnection dbc = new SqliteConnection(ConnectionString);
dbc.Open(); dbc.Open();
await using SqliteCommand cmd = new SqliteCommand(groupId != null ? $"SELECT * FROM {_feedTable} WHERE group_id=@groupId" : $"SELECT * FROM {_feedTable}", dbc) await using SqliteCommand cmd = new SqliteCommand(groupId != null ? $"SELECT * FROM {FeedTable} WHERE group_id=@groupId" : $"SELECT * FROM {FeedTable}", dbc)
{ {
Parameters = Parameters =
{ {
@ -116,12 +114,12 @@ namespace SharpRss.Services
feeds.Add(await ReaderToFeedModel(reader)); feeds.Add(await ReaderToFeedModel(reader));
return feeds; return feeds;
} }
public async Task<FeedModel?> GetFeedAsync(string feedId) public static async Task<FeedModel?> GetFeedAsync(string feedId)
{ {
await using SqliteConnection dbc = new SqliteConnection(_connectionString); await using SqliteConnection dbc = new SqliteConnection(ConnectionString);
dbc.Open(); dbc.Open();
FeedModel? feed = null; FeedModel? feed = null;
await using SqliteCommand cmd = new SqliteCommand($"SELECT * FROM {_feedTable} WHERE id=@id", dbc) await using SqliteCommand cmd = new SqliteCommand($"SELECT * FROM {FeedTable} WHERE id=@id", dbc)
{ {
Parameters = { new SqliteParameter("id", feedId) } Parameters = { new SqliteParameter("id", feedId) }
}; };
@ -130,40 +128,48 @@ namespace SharpRss.Services
feed = await ReaderToFeedModel(reader); feed = await ReaderToFeedModel(reader);
return feed; return feed;
} }
public async Task<FeedModel?> SetFeedAsync(FeedModel feedModel) public static async Task<FeedModel?> SetFeedsAsync(IEnumerable<FeedModel> feedModels)
{ {
FeedModel? resultModel = null; FeedModel? resultModel = null;
await using SqliteConnection dbc = new SqliteConnection(_connectionString); await using SqliteConnection dbc = new SqliteConnection(ConnectionString);
dbc.Open(); dbc.Open();
await using SqliteCommand cmd = new SqliteCommand($"INSERT OR REPLACE INTO {_feedTable} (id, url, title, group_id, feed_type, description, language, copyright, date_added, last_updated, image_url, original_document) VALUES (IFNULL((SELECT id FROM {_feedTable} WHERE url=@url), @id), @url, @title, @groupId, @feedType, @description, @language, @copyright, IFNULL((SELECT date_added FROM {_feedTable} WHERE id=@id), @dateAdded), @lastUpdated, @imageUrl, @originalDoc); SELECT * FROM {_feedTable} WHERE url=@url", dbc) await using SqliteTransaction transaction = dbc.BeginTransaction();
foreach (var feedModel in feedModels)
{ {
Parameters = //await using SqliteCommand cmd = new SqliteCommand($"INSERT OR REPLACE INTO {_feedTable} (id, url, title, group_id, feed_type, description, language, copyright, date_added, last_updated, image_url, original_document) VALUES (IFNULL((SELECT id FROM {_feedTable} WHERE url=@url), @id), @url, @title, @groupId, @feedType, @description, @language, @copyright, IFNULL((SELECT date_added FROM {_feedTable} WHERE id=@id), @dateAdded), @lastUpdated, @imageUrl, @originalDoc); SELECT * FROM {_feedTable} WHERE url=@url", dbc)
await using SqliteCommand cmd = new SqliteCommand($"INSERT OR REPLACE INTO {FeedTable} (id, url, title, group_id, feed_type, description, language, copyright, date_added, last_updated, image_url, original_document) VALUES (IFNULL((SELECT id FROM {FeedTable} WHERE url=@url), @id), @url, @title, @groupId, @feedType, @description, @language, @copyright, IFNULL((SELECT date_added FROM {FeedTable} WHERE id=@id), @dateAdded), @lastUpdated, @imageUrl, @originalDoc)", dbc)
{ {
new SqliteParameter("id", feedModel.Id ?? string.Empty), Parameters =
new SqliteParameter("url", feedModel.Url ?? string.Empty), {
new SqliteParameter("title", feedModel.Title ?? string.Empty), new SqliteParameter("id", feedModel.Id ?? string.Empty),
new SqliteParameter("groupId", feedModel.GroupId ?? string.Empty), new SqliteParameter("url", feedModel.Url ?? string.Empty),
new SqliteParameter("feedType", feedModel.FeedType ?? string.Empty), new SqliteParameter("title", feedModel.Title ?? string.Empty),
new SqliteParameter("description", feedModel.Description ?? string.Empty), new SqliteParameter("groupId", feedModel.GroupId ?? string.Empty),
new SqliteParameter("language", feedModel.Language ?? string.Empty), new SqliteParameter("feedType", feedModel.FeedType ?? string.Empty),
new SqliteParameter("copyright", feedModel.Copyright ?? string.Empty), new SqliteParameter("description", feedModel.Description ?? string.Empty),
new SqliteParameter("dateAdded", feedModel.DateAdded?.ToUnixTimeMilliseconds()), new SqliteParameter("language", feedModel.Language ?? string.Empty),
new SqliteParameter("lastUpdated", feedModel.LastUpdated?.ToUnixTimeMilliseconds()), new SqliteParameter("copyright", feedModel.Copyright ?? string.Empty),
new SqliteParameter("imageUrl", feedModel.ImageUrl ?? string.Empty), new SqliteParameter("dateAdded", feedModel.DateAdded?.ToUnixTimeMilliseconds()),
new SqliteParameter("originalDoc", feedModel.OriginalDocument ?? string.Empty) new SqliteParameter("lastUpdated", feedModel.LastUpdated?.ToUnixTimeMilliseconds()),
} new SqliteParameter("imageUrl", feedModel.ImageUrl ?? string.Empty),
}; new SqliteParameter("originalDoc", feedModel.OriginalDocument ?? string.Empty)
await using SqliteDataReader reader = await cmd.ExecuteReaderAsync(); }
if (reader.Read()) };
resultModel = await ReaderToFeedModel(reader); /*await using SqliteDataReader reader = await cmd.ExecuteReaderAsync();*/
await cmd.ExecuteNonQueryAsync();
}
/*if (reader.Read())
resultModel = await ReaderToFeedModel(reader);*/
await transaction.CommitAsync();
return resultModel; return resultModel;
} }
public async Task<bool> RemoveFeedAsync(FeedModel feedModel) public static async Task<bool> RemoveFeedAsync(FeedModel feedModel)
{ {
bool result = false; bool result = false;
await using SqliteConnection dbc = new SqliteConnection(_connectionString); await using SqliteConnection dbc = new SqliteConnection(ConnectionString);
dbc.Open(); dbc.Open();
await using SqliteCommand cmd = new SqliteCommand($"DELETE FROM {_feedTable} WHERE id=@id; UPDATE {_feedItemTable} SET feed_id=NULL WHERE feed_id=@id", dbc) await using SqliteCommand cmd = new SqliteCommand($"DELETE FROM {FeedTable} WHERE id=@id; UPDATE {FeedItemTable} SET feed_id=NULL WHERE feed_id=@id", dbc)
{ {
Parameters = Parameters =
{ {
@ -176,16 +182,16 @@ namespace SharpRss.Services
return result; return result;
} }
// Feed items // Feed items
public async Task<HashSet<FeedItemModel>> GetFeedItemsAsync(string[]? feedIds = null) public static async Task<HashSet<FeedItemModel>> GetFeedItemsAsync(string[]? feedIds = null)
{ {
List<string>? formattedIds = feedIds?.Select(s => $"'{s}'").ToList(); List<string>? formattedIds = feedIds?.Select(s => $"'{s}'").ToList();
HashSet<FeedItemModel> feedItems = new HashSet<FeedItemModel>(); HashSet<FeedItemModel> feedItems = new HashSet<FeedItemModel>();
await using SqliteConnection dbc = new SqliteConnection(_connectionString); await using SqliteConnection dbc = new SqliteConnection(ConnectionString);
dbc.Open(); dbc.Open();
await using SqliteCommand cmd = new SqliteCommand( await using SqliteCommand cmd = new SqliteCommand(
formattedIds != null formattedIds != null
? $"SELECT * FROM {_feedItemTable} WHERE feed_id IN ({string.Join(", ", formattedIds)})" ? $"SELECT * FROM {FeedItemTable} WHERE feed_id IN ({string.Join(", ", formattedIds)})"
: $"SELECT * FROM {_feedItemTable}", dbc); : $"SELECT * FROM {FeedItemTable}", dbc);
await using SqliteDataReader reader = await cmd.ExecuteReaderAsync(); await using SqliteDataReader reader = await cmd.ExecuteReaderAsync();
while (reader.Read()) while (reader.Read())
@ -215,14 +221,14 @@ namespace SharpRss.Services
} }
return feedItems; return feedItems;
} }
public async Task<int> SetFeedItemsAsync(HashSet<FeedItemModel> items) public static async Task<int> SetFeedItemsAsync(HashSet<FeedItemModel> items)
{ {
int result = 0; int result = 0;
await using SqliteConnection dbc = new SqliteConnection(_connectionString); await using SqliteConnection dbc = new SqliteConnection(ConnectionString);
dbc.Open(); dbc.Open();
await using SqliteTransaction transaction = dbc.BeginTransaction(); await using SqliteTransaction transaction = dbc.BeginTransaction();
await using SqliteCommand cmd = new SqliteCommand($"INSERT OR REPLACE INTO {_feedItemTable} (id, feed_id, read, title, description, link, last_updated, publishing_date, author, categories, content)" + await using SqliteCommand cmd = new SqliteCommand($"INSERT OR REPLACE INTO {FeedItemTable} (id, feed_id, read, title, description, link, last_updated, publishing_date, author, categories, content)" +
$"VALUES (IFNULL((SELECT id FROM {_feedItemTable} WHERE link=@link), @id), @feedId, @read, @title, @description, @link, @lastUpdated, @publishingDate, @author, @categories, @content)", dbc); $"VALUES (IFNULL((SELECT id FROM {FeedItemTable} WHERE link=@link), @id), @feedId, @read, @title, @description, @link, @lastUpdated, @publishingDate, @author, @categories, @content)", dbc);
foreach (FeedItemModel item in items) foreach (FeedItemModel item in items)
{ {
cmd.Parameters.Clear(); cmd.Parameters.Clear();
@ -247,14 +253,14 @@ namespace SharpRss.Services
result += affected; result += affected;
} }
transaction.Commit(); transaction.Commit();
return result; // Return the amount affected rows. return result;
} }
public async Task<bool> RemoveFeedItemAsync(FeedItemModel itemModel) public static async Task<bool> RemoveFeedItemAsync(FeedItemModel itemModel)
{ {
bool result = false; bool result = false;
await using SqliteConnection dbc = new SqliteConnection(_connectionString); await using SqliteConnection dbc = new SqliteConnection(ConnectionString);
dbc.Open(); dbc.Open();
await using SqliteCommand cmd = new SqliteCommand($"DELETE FROM {_feedItemTable} WHERE id=@id", dbc) await using SqliteCommand cmd = new SqliteCommand($"DELETE FROM {FeedItemTable} WHERE id=@id", dbc)
{ {
Parameters = Parameters =
{ {
@ -266,12 +272,12 @@ namespace SharpRss.Services
result = true; result = true;
return result; return result;
} }
public async Task<GroupModel?> GetGroupFromFeedItemAsync(FeedItemModel feedItem) public static async Task<GroupModel?> GetGroupFromFeedItemAsync(FeedItemModel feedItem)
{ {
GroupModel? result = null; GroupModel? result = null;
await using SqliteConnection dbc = new SqliteConnection(_connectionString); await using SqliteConnection dbc = new SqliteConnection(ConnectionString);
dbc.Open(); dbc.Open();
await using SqliteCommand cmd = new SqliteCommand($"SELECT * FROM {_groupTable} WHERE id=(SELECT group_id FROM {_feedTable} WHERE id=@fId)", dbc) await using SqliteCommand cmd = new SqliteCommand($"SELECT * FROM {GroupTable} WHERE id=(SELECT group_id FROM {FeedTable} WHERE id=@fId)", dbc)
{ {
Parameters = Parameters =
{ {
@ -286,7 +292,7 @@ namespace SharpRss.Services
result = groups.FirstOrDefault(); result = groups.FirstOrDefault();
return result; return result;
} }
private async Task<FeedModel> ReaderToFeedModel(SqliteDataReader reader) private static async Task<FeedModel> ReaderToFeedModel(SqliteDataReader reader)
{ {
FeedModel fetchedFeed = new FeedModel(reader["url"].ToString()) FeedModel fetchedFeed = new FeedModel(reader["url"].ToString())
{ {
@ -309,22 +315,29 @@ namespace SharpRss.Services
Log.Warning("Could not get group from feed: {FeedId}", fetchedFeed.Id); Log.Warning("Could not get group from feed: {FeedId}", fetchedFeed.Id);
return fetchedFeed; return fetchedFeed;
} }
private async void InitializeDb()
//===
private static void CheckInitialized()
{ {
if (!_isInitialized) throw new TypeInitializationException(nameof(DbAccess), null);
}
public static async void Initialize()
{
if (_isInitialized) return;
Log.Verbose("Checking database..."); Log.Verbose("Checking database...");
HashSet<string> failed = new HashSet<string>(); HashSet<string> failed = new HashSet<string>();
await using SqliteConnection dbc = new SqliteConnection(_connectionString); await using SqliteConnection dbc = new SqliteConnection(ConnectionString);
dbc.Open(); dbc.Open();
Log.Verbose("Checking table: {Table}", _groupTable); Log.Verbose("Checking table: {Table}", GroupTable);
var queryResponse = await dbc.QueryAsync($"CREATE TABLE IF NOT EXISTS {_groupTable} (name STRING NOT NULL, hex_color STRING NOT NULL, icon STRING, id STRING PRIMARY KEY)"); var queryResponse = await dbc.QueryAsync($"CREATE TABLE IF NOT EXISTS {GroupTable} (name STRING NOT NULL, hex_color STRING NOT NULL, icon STRING, id STRING PRIMARY KEY)");
if (queryResponse.Any()) failed.Add("category_data"); if (queryResponse.Any()) failed.Add("category_data");
Log.Verbose("Checking table: {Table}", _feedTable); Log.Verbose("Checking table: {Table}", FeedTable);
queryResponse = await dbc.QueryAsync($"CREATE TABLE IF NOT EXISTS {_feedTable} (id STRING PRIMARY KEY, url STRING NOT NULL, title STRING, group_id STRING, feed_type STRING, description STRING, language STRING, copyright STRING, date_added INT, last_updated INT, image_url STRING, original_document STRING)"); queryResponse = await dbc.QueryAsync($"CREATE TABLE IF NOT EXISTS {FeedTable} (id STRING PRIMARY KEY, url STRING NOT NULL, title STRING, group_id STRING, feed_type STRING, description STRING, language STRING, copyright STRING, date_added INT, last_updated INT, image_url STRING, original_document STRING)");
if (queryResponse.Any()) failed.Add("feed_data"); if (queryResponse.Any()) failed.Add("feed_data");
Log.Verbose("Checking table: {Table}", _feedItemTable); Log.Verbose("Checking table: {Table}", FeedItemTable);
queryResponse = await dbc.QueryAsync($"CREATE TABLE IF NOT EXISTS {_feedItemTable} (id STRING PRIMARY KEY, feed_id STRING, read INT, title STRING, description STRING, link STRING, last_updated INT, publishing_date INT, author STRING, categories STRING, content STRING)"); queryResponse = await dbc.QueryAsync($"CREATE TABLE IF NOT EXISTS {FeedItemTable} (id STRING PRIMARY KEY, feed_id STRING, read INT, title STRING, description STRING, link STRING, last_updated INT, publishing_date INT, author STRING, categories STRING, content STRING)");
if (queryResponse.Any()) failed.Add("feed_item_data"); if (queryResponse.Any()) failed.Add("feed_item_data");
if (failed.Any()) if (failed.Any())
@ -334,9 +347,7 @@ namespace SharpRss.Services
} }
else else
Log.Verbose("Checking database done!"); Log.Verbose("Checking database done!");
} _isInitialized = true;
public void Dispose()
{
} }
} }
} }

View File

@ -1,43 +0,0 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using CodeHollow.FeedReader;
using Serilog;
using ToolQit.Extensions;
namespace SharpRss
{
/// <summary>
/// Global memory feed cache.
/// </summary>
public static class FeedCache
{
private static readonly Dictionary<string, Feed> CachedFeeds = new Dictionary<string, Feed>();
public static async Task<Feed> GetFeed(string urlKey)
{
Log.Verbose("Fetching feed: {UrlKey}", urlKey);
if (urlKey.IsNullEmptyWhiteSpace())
{
Log.Error("RSS Url is empty!");
return new Feed();
}
if (CachedFeeds.TryGetValue(urlKey, out Feed? fModel))
return fModel;
string feedUrl;
var urls = await FeedReader.GetFeedUrlsFromUrlAsync(urlKey);
if (!urls.Any())
feedUrl = urlKey;
else
feedUrl = urls.First().Url;
Feed feed = await FeedReader.ReadAsync(feedUrl);
if (feed != null)
{
Log.Warning("Could not get feed: {FeedUrl}", feedUrl);
CachedFeeds[feedUrl] = feed;
}
return feed;
}
}
}

View File

@ -17,7 +17,6 @@ namespace SharpRss.Services
{ {
SetupTestGroupsAndFeedsAsync(); SetupTestGroupsAndFeedsAsync();
} }
private readonly DatabaseService _dbService = new DatabaseService();
public async Task<HashSet<object>> GetGroupsFeedsAsync() public async Task<HashSet<object>> GetGroupsFeedsAsync()
{ {
@ -26,20 +25,39 @@ namespace SharpRss.Services
items.UnionWith(await GetUngroupedFeedsAsync()); items.UnionWith(await GetUngroupedFeedsAsync());
return items; return items;
} }
public async Task<bool> CreateGroupAsync(GroupModel group) => await _dbService.SetGroupAsync(group); public async Task<bool> CreateGroupAsync(GroupModel group) => await DbAccess.SetGroupAsync(group);
public async Task<HashSet<GroupModel>> GetGroupsAsync() => await _dbService.GetGroupsAsync(); public async Task<HashSet<GroupModel>> GetGroupsAsync() => await DbAccess.GetGroupsAsync();
//TODO: Need to rework this implementation!!! //TODO: Need to rework this implementation!!!
/*public async Task<bool> FetchFeeds(string[]? rssUrls, GroupModel? group = null) public async Task<bool> AddFeedsAsync(string[]? rssUrls, GroupModel? group = null)
{ {
bool result = false; bool result = false;
if (rssUrls == null) return result; if (rssUrls == null) return result;
HashSet<Feed> fetchedFeeds = new HashSet<Feed>(); HashSet<Feed> fetchedFeeds = new HashSet<Feed>();
foreach (var rssUrl in rssUrls) foreach (var rssUrl in rssUrls)
{
fetchedFeeds.Add(await FetchFeed(rssUrl)); fetchedFeeds.Add(await FetchFeed(rssUrl));
HashSet<FeedModel> feeds = new HashSet<FeedModel>();
HashSet<FeedItemModel> itemModels = new HashSet<FeedItemModel>();
foreach (var feed in fetchedFeeds)
{
FeedModel feedModel = new FeedModel(feed.Link)
{
Title = feed.Title,
GroupId = group?.Id ?? string.Empty,
FeedType = feed.Type.ToString(),
Description = feed.Description,
Language = feed.Language,
Copyright = feed.Copyright,
DateAdded = DateTimeOffset.Now,
LastUpdated = DateTimeOffset.Now,
ImageUrl = feed.ImageUrl,
OriginalDocument = feed.OriginalDocument
};
feeds.Add(feedModel);
} }
Feed fetched = await FetchFeed(rssUrl); /*Feed fetched = await FetchFeed(rssUrl);
if (fetched == null) if (fetched == null)
return result; return result;
@ -59,18 +77,19 @@ namespace SharpRss.Services
FeedModel? dbFeed = await _dbService.SetFeedAsync(feedModel); FeedModel? dbFeed = await _dbService.SetFeedAsync(feedModel);
result = dbFeed != null; result = dbFeed != null;
if (dbFeed == null) if (dbFeed == null)
return result; return result;*/
if (await AddFeedItems(fetched.Items, dbFeed) == 0) //itemModels.Add(ConstructFeedItems());
Log.Warning("No feed items added to feed: {FeedUrl}", dbFeed.Url); /*if (await AddFeedItems(fetched.Items, dbFeed) == 0)
Log.Warning("No feed items added to feed: {FeedUrl}", dbFeed.Url);*/
return result; return result;
}*/ }
public async Task<HashSet<FeedModel>> GetFeedsAsync(string? groupId = null) => await _dbService.GetFeedsAsync(groupId); public async Task<HashSet<FeedModel>> GetFeedsAsync(string? groupId = null) => await DbAccess.GetFeedsAsync(groupId);
public async Task<HashSet<FeedModel>> GetUngroupedFeedsAsync() => await _dbService.GetFeedsAsync(""); public async Task<HashSet<FeedModel>> GetUngroupedFeedsAsync() => await DbAccess.GetFeedsAsync("");
public async Task<HashSet<FeedItemModel>> GetFeedItemsAsync(string feedId, string? groupId = null) => await GetFeedItemsFromFeedsAsync(new[] { feedId }, groupId); public async Task<HashSet<FeedItemModel>> GetFeedItemsAsync(string feedId, string? groupId = null) => await GetFeedItemsFromFeedsAsync(new[] { feedId }, groupId);
public async Task<HashSet<FeedItemModel>> GetFeedItemsFromFeedsAsync(string[] feedIds, string? groupId = null) public async Task<HashSet<FeedItemModel>> GetFeedItemsFromFeedsAsync(string[] feedIds, string? groupId = null)
{ {
var items = await _dbService.GetFeedItemsAsync(feedIds); var items = await DbAccess.GetFeedItemsAsync(feedIds);
return items; return items;
} }
@ -80,12 +99,22 @@ namespace SharpRss.Services
if (!items.Any()) if (!items.Any())
return result; return result;
HashSet<FeedItemModel> itemModels = new HashSet<FeedItemModel>(); HashSet<FeedItemModel> itemModels = new HashSet<FeedItemModel>();
// TODO: implement!!!
result = await DbAccess.SetFeedItemsAsync(itemModels);
return result;
}
private IList<FeedItemModel> ConstructFeedItems(IList<FeedItem> items, FeedModel feed)
{
IList<FeedItemModel> itemModels = new List<FeedItemModel>();
if (!items.Any())
return itemModels;
foreach (FeedItem item in items) foreach (FeedItem item in items)
{ {
itemModels.Add(new FeedItemModel() itemModels.Add(new FeedItemModel()
{ {
Id = item.Id, Id = item.Id,
FeedId = feedModel.Id, FeedId = feed.Id,
Title = item.Title, Title = item.Title,
Description = item.Description, Description = item.Description,
Link = item.Link, Link = item.Link,
@ -96,10 +125,10 @@ namespace SharpRss.Services
Content = item.Content Content = item.Content
}); });
} }
result = await _dbService.SetFeedItemsAsync(itemModels); return itemModels;
return result; }
}
private async Task<Feed> FetchFeed(string url) private async Task<Feed> FetchFeed(string url)
{ {
Log.Verbose("Fetching feed: {FeedUrl}", url); Log.Verbose("Fetching feed: {FeedUrl}", url);
var urls = await FeedReader.ParseFeedUrlsAsStringAsync(url); var urls = await FeedReader.ParseFeedUrlsAsStringAsync(url);
@ -133,9 +162,8 @@ namespace SharpRss.Services
}*/ }*/
} }
public void Dispose() public void Dispose()
{ {
_dbService.Dispose(); }
} }
}
} }

View File

@ -4,6 +4,7 @@ using Microsoft.Extensions.Hosting;
using MudBlazor; using MudBlazor;
using MudBlazor.Services; using MudBlazor.Services;
using Serilog; using Serilog;
using SharpRss;
using SharpRss.Services; using SharpRss.Services;
using ToolQit; using ToolQit;
using WebSharpRSS; using WebSharpRSS;
@ -12,6 +13,7 @@ using WebSharpRSS.Models;
Caretaker.Settings.SetAppDefaultSettings(); Caretaker.Settings.SetAppDefaultSettings();
Bootstrapper.SetupLogging(); Bootstrapper.SetupLogging();
Log.Information("Starting..."); Log.Information("Starting...");
DbAccess.Initialize();
var builder = WebApplication.CreateBuilder(args); var builder = WebApplication.CreateBuilder(args);
builder.Services.AddRazorPages(); builder.Services.AddRazorPages();

View File

@ -23,4 +23,22 @@
<ProjectReference Include="..\SharpRss\SharpRss.csproj" /> <ProjectReference Include="..\SharpRss\SharpRss.csproj" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<_ContentIncludedByDefault Remove="logs\log_20230507.json" />
<_ContentIncludedByDefault Remove="logs\log_20230509.json" />
<_ContentIncludedByDefault Remove="logs\log_20230512.json" />
<_ContentIncludedByDefault Remove="logs\log_20230513.json" />
<_ContentIncludedByDefault Remove="logs\log_20230515.json" />
<_ContentIncludedByDefault Remove="logs\log_20230517.json" />
<_ContentIncludedByDefault Remove="logs\log_20230518.json" />
<_ContentIncludedByDefault Remove="logs\log_20230519.json" />
<_ContentIncludedByDefault Remove="logs\log_20230521.json" />
<_ContentIncludedByDefault Remove="logs\log_20230522.json" />
<_ContentIncludedByDefault Remove="logs\log_20230523.json" />
<_ContentIncludedByDefault Remove="logs\log_20230524.json" />
<_ContentIncludedByDefault Remove="logs\log_20230525.json" />
<_ContentIncludedByDefault Remove="logs\log_20230526.json" />
<_ContentIncludedByDefault Remove="logs\log_20230527.json" />
</ItemGroup>
</Project> </Project>