From dfc9db794435df32b341c8988aa516f6b2d8d33a Mon Sep 17 00:00:00 2001 From: Max <51083570+DRdrProfessor@users.noreply.github.com> Date: Sat, 24 Jun 2023 00:15:54 +0200 Subject: [PATCH] Broken, rework DbAccess to only get and set db data. --- SharpRss/Core/Cache/TempCache.cs | 12 ++++++------ SharpRss/DbAccess.cs | 4 +++- SharpRss/Models/CategoryModel.cs | 3 ++- WebSharpRSS/Models/TreeItemData.cs | 6 ++++-- WebSharpRSS/Shared/ReadDialog.razor | 15 ++++++++++++++- WebSharpRSS/Shared/SideGuide.razor | 3 ++- WebSharpRSS/Shared/SyndicationListItem.razor | 13 +++++++++---- 7 files changed, 40 insertions(+), 16 deletions(-) diff --git a/SharpRss/Core/Cache/TempCache.cs b/SharpRss/Core/Cache/TempCache.cs index c3170cf..4c18eda 100644 --- a/SharpRss/Core/Cache/TempCache.cs +++ b/SharpRss/Core/Cache/TempCache.cs @@ -13,8 +13,8 @@ namespace SharpRss.Core.Cache internal static class TempCache { - private static Dictionary _categoryCache = new Dictionary(); - private static Dictionary _syndicationCache = new Dictionary(); + public static Dictionary CategoryCache { get; private set; } = new Dictionary(); + public static Dictionary SyndicationCache { get; private set; } = new Dictionary(); public static async Task UpdateCache(CacheFetch cf) { @@ -22,16 +22,16 @@ namespace SharpRss.Core.Cache { case CacheFetch.Category: var cats = await DbAccess.GetCategoriesAsync(); - _categoryCache = cats.ToDictionary(x => x.Id); + CategoryCache = cats.ToDictionary(x => x.Id); break; case CacheFetch.Syndication: var syndications = await DbAccess.GetSyndicationsAsync(); - _syndicationCache = syndications.ToDictionary(x => x.EncodedUrl); + SyndicationCache = syndications.ToDictionary(x => x.EncodedUrl); break; } } - public static CategoryModel? GetCategory(string catId) => _categoryCache.TryGetValue(catId, out CategoryModel catModel) ? catModel : null; - public static SyndicationModel? GetSyndication(string encodedUrl) => _syndicationCache.TryGetValue(encodedUrl, out SyndicationModel synModel) ? synModel : null; + public static CategoryModel? GetCategory(string catId) => CategoryCache.TryGetValue(catId, out CategoryModel catModel) ? catModel : null; + public static SyndicationModel? GetSyndication(string encodedUrl) => SyndicationCache.TryGetValue(encodedUrl, out SyndicationModel synModel) ? synModel : null; } } \ No newline at end of file diff --git a/SharpRss/DbAccess.cs b/SharpRss/DbAccess.cs index 322fb04..0d9060f 100644 --- a/SharpRss/DbAccess.cs +++ b/SharpRss/DbAccess.cs @@ -12,6 +12,7 @@ using SharpRss.Models; namespace SharpRss { + //TODO: Need rework to only get from db syndication service will handle extra data. internal static class DbAccess { private static readonly string ConnectionString = $"Data Source={Path.Combine(Environment.CurrentDirectory, "sharp_rss.sqlite")};"; @@ -47,7 +48,8 @@ namespace SharpRss Icon = reader["icon"].ToString(), LastUpdated = DateTimeOffset.FromUnixTimeMilliseconds(long.Parse(reader["last_updated"].ToString())) }; - categoryModel.SyndicationCount = await dbc.ExecuteScalarAsync($"SELECT COUNT(*) FROM syndication WHERE category_id=@CatId", new { CatId = categoryModel.Id }); + categoryModel.Syndications = await GetSyndicationsAsync(new []{ categoryModel.Id }); + //categoryModel.SyndicationCount = await dbc.ExecuteScalarAsync($"SELECT COUNT(*) FROM syndication WHERE category_id=@CatId", new { CatId = categoryModel.Id }); categories.Add(categoryModel); } return categories; diff --git a/SharpRss/Models/CategoryModel.cs b/SharpRss/Models/CategoryModel.cs index caf2131..87a85ae 100644 --- a/SharpRss/Models/CategoryModel.cs +++ b/SharpRss/Models/CategoryModel.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using ToolQit; using ToolQit.Extensions; @@ -26,6 +27,6 @@ namespace SharpRss.Models } public string Icon { get; set; } = string.Empty; public DateTimeOffset LastUpdated { get; set; } = DateTimeOffset.Now; - public int SyndicationCount { get; set; } + public HashSet Syndications { get; set; } = new HashSet(); } } diff --git a/WebSharpRSS/Models/TreeItemData.cs b/WebSharpRSS/Models/TreeItemData.cs index fe3dd8b..535e661 100644 --- a/WebSharpRSS/Models/TreeItemData.cs +++ b/WebSharpRSS/Models/TreeItemData.cs @@ -16,8 +16,10 @@ namespace WebSharpRSS.Models CategoryModel = categoryModel; Title = categoryModel.Name; Icon = categoryModel.Icon == string.Empty ? Icons.Material.Filled.RssFeed : categoryModel.Icon; - HasChildren = CategoryModel.SyndicationCount > 0; - } + HasChildren = CategoryModel.Syndications.Count > 0; + if (HasChildren) + CategoryModel.Syndications.Select(x => x.ItemCount).ToList().ForEach(i => TotalItems += i); + } public TreeItemData(SyndicationModel syndicationModel) { diff --git a/WebSharpRSS/Shared/ReadDialog.razor b/WebSharpRSS/Shared/ReadDialog.razor index 1982e2a..b8b4544 100644 --- a/WebSharpRSS/Shared/ReadDialog.razor +++ b/WebSharpRSS/Shared/ReadDialog.razor @@ -1,8 +1,21 @@ @using WebSharpRSS.Models +@using ToolQit.Extensions - +
+ @if (Data?.SyndicationParent.ImageUrl != null) + { + + } +
+ @((MarkupString)(Data?.Title ?? "This item doesn't contains a title!")) +
+
+ +
+ @((MarkupString)((Data?.Content == null || Data.Content.IsNullEmptyWhiteSpace() ? Data?.Description : Data.Content) ?? "This item doesn't contain any content!")) +
diff --git a/WebSharpRSS/Shared/SideGuide.razor b/WebSharpRSS/Shared/SideGuide.razor index e3d35e8..a521677 100644 --- a/WebSharpRSS/Shared/SideGuide.razor +++ b/WebSharpRSS/Shared/SideGuide.razor @@ -14,7 +14,7 @@
-
+
@if (context.FaviconUrl == null && context.Icon != null) { @@ -26,6 +26,7 @@ } @context.Title
+ @context.TotalItems
diff --git a/WebSharpRSS/Shared/SyndicationListItem.razor b/WebSharpRSS/Shared/SyndicationListItem.razor index abe69ef..ae684c5 100644 --- a/WebSharpRSS/Shared/SyndicationListItem.razor +++ b/WebSharpRSS/Shared/SyndicationListItem.razor @@ -14,20 +14,25 @@
@if (feedItemData.SyndicationParent.Category != null && !feedItemData.SyndicationParent.Category.Icon.IsNullEmptyWhiteSpace()) { - + + + } @if (feedItemData.SyndicationParent.ImageUrl != null) { - + + + } -
+
@if (feedItemData.Title != null) { @((MarkupString)feedItemData.Title) }
+ - @feedItemData.SyndicationParent.Title
-
+
@if (feedItemData.Description != null && !feedItemData.Description.IsNullEmptyWhiteSpace()) { @((MarkupString)feedItemData.Description)