Reworking DbAccess

This commit is contained in:
Max Holleman 2023-06-02 15:45:10 +02:00
parent 5a1f350caf
commit cb13ef54c1
2 changed files with 19 additions and 17 deletions

View File

@ -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<HashSet<CategoryModel>> GetGroupsAsync(string? groupId = null)
public static async Task<HashSet<CategoryModel>> 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<CategoryModel> groups = new HashSet<CategoryModel>();
HashSet<CategoryModel> categories = new HashSet<CategoryModel>();
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<bool> SetGroupAsync(CategoryModel groupModel)
public static async Task<bool> 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<bool> RemoveGroupAsync(CategoryModel groupModel)
public static async Task<bool> 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<CategoryModel>? 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;

View File

@ -24,12 +24,12 @@ namespace SharpRss.Services
public async Task<HashSet<object>> GetGroupsFeedsAsync()
{
HashSet<object> items = new HashSet<object>();
items.UnionWith(await GetGroupsAsync());
items.UnionWith(await GetCategoriesAsync());
items.UnionWith(await GetUngroupedFeedsAsync());
return items;
}
public async Task<bool> CreateGroupAsync(CategoryModel group) => await DbAccess.SetGroupAsync(group);
public async Task<HashSet<CategoryModel>> GetGroupsAsync() => await DbAccess.GetGroupsAsync();
public async Task<bool> CreateGroupAsync(CategoryModel group) => await DbAccess.SetCategoryAsync(group);
public async Task<HashSet<CategoryModel>> GetCategoriesAsync() => await DbAccess.GetCategoriesAsync();
//TODO: Rework this!
// Subscribe to a feed.