Working on read page & database service

This commit is contained in:
Max Holleman 2023-05-24 15:51:03 +02:00
parent bd4d465089
commit d638e763d4
5 changed files with 87 additions and 5 deletions

View File

@ -31,4 +31,7 @@ Global
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE
EndGlobalSection EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {A31B46E9-37D1-4263-8681-617029BDBE66}
EndGlobalSection
EndGlobal EndGlobal

View File

@ -15,7 +15,7 @@ namespace SharpRss.Services
internal DatabaseService() internal DatabaseService()
{ {
_sqlConn = new SqliteConnection(_connectionString); _sqlConn = new SqliteConnection(_connectionString);
//InitializeDb(); InitializeDb();
} }
private readonly SqliteConnection _sqlConn; private readonly SqliteConnection _sqlConn;
private readonly string _connectionString = $"Data Source={Path.Combine(Environment.CurrentDirectory, "sharp_rss.sqlite")};"; private readonly string _connectionString = $"Data Source={Path.Combine(Environment.CurrentDirectory, "sharp_rss.sqlite")};";
@ -24,14 +24,14 @@ namespace SharpRss.Services
private readonly string _feedItemTable = "feed_item_data"; private readonly string _feedItemTable = "feed_item_data";
// Groups // Groups
public async Task<HashSet<GroupModel>> GetGroupsAsync(string? groupName = null) public async Task<HashSet<GroupModel>> GetGroupsAsync(string? groupId = null)
{ {
_sqlConn.Open(); _sqlConn.Open();
using SqliteCommand cmd = new SqliteCommand(groupName != null ? $"SELECT * FROM {_groupTable} WHERE name=@name;" : $"SELECT * FROM {_groupTable}", _sqlConn) using SqliteCommand cmd = new SqliteCommand(groupId != null ? $"SELECT * FROM {_groupTable} WHERE id=@gId;" : $"SELECT * FROM {_groupTable}", _sqlConn)
{ {
Parameters = Parameters =
{ {
new SqliteParameter("name", groupName) new SqliteParameter("gId", groupId)
} }
}; };
await using SqliteDataReader reader = await cmd.ExecuteReaderAsync(); await using SqliteDataReader reader = await cmd.ExecuteReaderAsync();
@ -255,6 +255,26 @@ namespace SharpRss.Services
_sqlConn.Close(); _sqlConn.Close();
return result; return result;
} }
public async Task<GroupModel?> GetGroupFromFeeditemAsync(FeedItemModel feedItem)
{
GroupModel? result = null;
_sqlConn.Open();
using SqliteCommand cmd = new SqliteCommand($"SELECT * FROM {_groupTable} WHERE id=(SELECT group_id FROM {_feedTable} WHERE id=@fId)", _sqlConn)
{
Parameters =
{
new SqliteParameter ("fId", feedItem.Id)
}
};
using SqliteDataReader reader = await cmd.ExecuteReaderAsync();
HashSet<GroupModel>? groups = null;
if (reader.Read())
groups = await GetGroupsAsync(reader["group_id"].ToString());
if (groups != null && groups.Any())
result = groups.FirstOrDefault();
_sqlConn.Close();
return result;
}
private async void InitializeDb() private async void InitializeDb()
{ {

View File

@ -5,7 +5,12 @@ namespace WebSharpRSS.Models
{ {
public class FeedItemData : FeedItemModel public class FeedItemData : FeedItemModel
{ {
public FeedItemData()
{
FaviconUrl = string.Format(Caretaker.Settings["Paths"].GetString("FaviconResolveUrl"), Link.Remove(Link.IndexOf("http", StringComparison.Ordinal), Link.IndexOf("://", StringComparison.Ordinal) + 3));
}
public static FeedItemData? FromModel(FeedItemModel model) => Utilities.ConvertFrom<FeedItemData, FeedItemModel>(model); public static FeedItemData? FromModel(FeedItemModel model) => Utilities.ConvertFrom<FeedItemData, FeedItemModel>(model);
public string HexColor { get; set; } public string HexColor { get; set; }
public string FaviconUrl { get; set; }
} }
} }

View File

@ -20,7 +20,7 @@ namespace WebSharpRSS.Models
{ {
FeedModel = feedModel; FeedModel = feedModel;
Title = feedModel.Title; Title = feedModel.Title;
FaviconUrl = string.Format(Caretaker.Settings["Paths"].GetString("FaviconResolveUrl"), feedModel.Url.Remove(feedModel.Url.IndexOf("http", StringComparison.Ordinal), feedModel.Url.IndexOf("://", StringComparison.Ordinal) + 3)); ; FaviconUrl = string.Format(Caretaker.Settings["Paths"].GetString("FaviconResolveUrl"), feedModel.Url.Remove(feedModel.Url.IndexOf("http", StringComparison.Ordinal), feedModel.Url.IndexOf("://", StringComparison.Ordinal) + 3));
} }
public readonly GroupModel? GroupModel; public readonly GroupModel? GroupModel;
public readonly FeedModel? FeedModel; public readonly FeedModel? FeedModel;

View File

@ -0,0 +1,54 @@
@page "/read/{Id}"
@using WebSharpRSS.Models;
<MudStack Spacing="2" Class="ml-2 mr-2">
@if (isLoading)
{
<div class="justify-self: center">
<MudProgressCircular Color="Color.Primary" Indeterminate="true" />
<MudText>Loading...</MudText>
</div>
}
else if (failed)
{
<MudAlert Severity="Severity.Error" Variant="Variant.Filled">Loading failed!</MudAlert>
}
else
{
foreach (var feedItemData in items)
{
<MudItem>
<MudCard>
<MudCardContent>
<div style="justify-self: start;" class="d-flex align-center">
@*@if (feedItemData.Icon != null)
{
<MudIcon Icon="@feedItemData.Icon" Style="@($"color:{feedItemData.CategoryColorHex}")" />
}
@if (feedItemData.FaviconUrl != null)
{
<MudImage Src="@feedItemData" ObjectFit="ObjectFit.Contain" />
}*@
<MudText Class="d-inline pa-2 align-center">@feedItemData.Title</MudText>
</div>
<MudText Typo="Typo.body2">@feedItemData.Description</MudText>
<MudText Typo="Typo.overline">@feedItemData.PublishingDate.ToString()</MudText>
</MudCardContent>
</MudCard>
</MudItem>
}
}
</MudStack>
@code {
[Parameter]
public string Id { get; set; }
HashSet<FeedItemData> items = new HashSet<FeedItemData>();
bool isLoading = true;
bool failed = false;
protected override void OnInitialized()
{
}
}