From cb13ef54c17ddd7b429f91f6fa27ad431d033769 Mon Sep 17 00:00:00 2001 From: Max Holleman Date: Fri, 2 Jun 2023 15:45:10 +0200 Subject: [PATCH] Reworking DbAccess --- SharpRss/DbAccess.cs | 30 ++++++++++++++++-------------- SharpRss/Services/RssService.cs | 6 +++--- 2 files changed, 19 insertions(+), 17 deletions(-) diff --git a/SharpRss/DbAccess.cs b/SharpRss/DbAccess.cs index f30516c..b6d78a0 100644 --- a/SharpRss/DbAccess.cs +++ b/SharpRss/DbAccess.cs @@ -15,6 +15,8 @@ namespace SharpRss { public static class DbAccess { + //TODO: Rename group => category. + //TODO: Reworking feed => model/db implementation. private static bool _isInitialized; private static readonly string ConnectionString = $"Data Source={Path.Combine(Environment.CurrentDirectory, "sharp_rss.sqlite")};"; private const string GroupTable = "group_data"; @@ -22,19 +24,19 @@ namespace SharpRss private const string FeedItemTable = "feed_item_data"; // Groups - public static async Task> GetGroupsAsync(string? groupId = null) + public static async Task> GetCategoriesAsync(string? categoryId = null) { await using SqliteConnection dbc = new SqliteConnection(ConnectionString); 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(categoryId != null ? $"SELECT * FROM {GroupTable} WHERE id=@gId;" : $"SELECT * FROM {GroupTable}", dbc) { Parameters = { - new SqliteParameter("gId", groupId) + new SqliteParameter("gId", categoryId) } }; await using SqliteDataReader reader = await cmd.ExecuteReaderAsync(); - HashSet groups = new HashSet(); + HashSet categories = new HashSet(); await using SqliteCommand cmdFeedCount = new SqliteCommand($"SELECT COUNT(*) FROM {FeedTable} WHERE group_id=@groupId", dbc); while (reader.Read()) { @@ -43,7 +45,7 @@ namespace SharpRss using SqliteDataReader countReader = await cmdFeedCount.ExecuteReaderAsync(); int count = countReader.Read() ? countReader.GetInt32(0) : 0; - groups.Add(new CategoryModel() + categories.Add(new CategoryModel() { Name = reader["name"].ToString(), FeedCount = count, @@ -52,9 +54,9 @@ namespace SharpRss Id = reader["id"].ToString() }); } - return groups; + return categories; } - public static async Task SetGroupAsync(CategoryModel groupModel) + public static async Task SetCategoryAsync(CategoryModel categoryModel) { bool result = false; await using SqliteConnection dbc = new SqliteConnection(ConnectionString); @@ -63,10 +65,10 @@ namespace SharpRss { Parameters = { - new SqliteParameter("id", groupModel.Id), - new SqliteParameter("hexColor", groupModel.HexColor), - new SqliteParameter("icon", groupModel.Icon), - new SqliteParameter("name", groupModel.Name) + new SqliteParameter("id", categoryModel.Id), + new SqliteParameter("hexColor", categoryModel.HexColor), + new SqliteParameter("icon", categoryModel.Icon), + new SqliteParameter("name", categoryModel.Name) } }; int affected = await cmd.ExecuteNonQueryAsync(); @@ -74,7 +76,7 @@ namespace SharpRss result = true; return result; } - public static async Task RemoveGroupAsync(CategoryModel groupModel) + public static async Task RemoveCategoryAsync(CategoryModel categoryModel) { bool result = false; await using SqliteConnection dbc = new SqliteConnection(ConnectionString); @@ -84,7 +86,7 @@ namespace SharpRss { Parameters = { - new SqliteParameter("id", groupModel.Id) + new SqliteParameter("id", categoryModel.Id) } }; int affected = await cmd.ExecuteNonQueryAsync(); @@ -361,7 +363,7 @@ namespace SharpRss await using SqliteDataReader reader = await cmd.ExecuteReaderAsync(); HashSet? groups = null; if (reader.Read()) - groups = await GetGroupsAsync(reader["group_id"].ToString()); + groups = await GetCategoriesAsync(reader["group_id"].ToString()); if (groups != null && groups.Any()) result = groups.FirstOrDefault(); return result; diff --git a/SharpRss/Services/RssService.cs b/SharpRss/Services/RssService.cs index 72a2edc..b11c520 100644 --- a/SharpRss/Services/RssService.cs +++ b/SharpRss/Services/RssService.cs @@ -24,12 +24,12 @@ namespace SharpRss.Services public async Task> GetGroupsFeedsAsync() { HashSet items = new HashSet(); - items.UnionWith(await GetGroupsAsync()); + items.UnionWith(await GetCategoriesAsync()); items.UnionWith(await GetUngroupedFeedsAsync()); return items; } - public async Task CreateGroupAsync(CategoryModel group) => await DbAccess.SetGroupAsync(group); - public async Task> GetGroupsAsync() => await DbAccess.GetGroupsAsync(); + public async Task CreateGroupAsync(CategoryModel group) => await DbAccess.SetCategoryAsync(group); + public async Task> GetCategoriesAsync() => await DbAccess.GetCategoriesAsync(); //TODO: Rework this! // Subscribe to a feed.