mirror of
https://github.com/hmaxnl/SharpRSS.git
synced 2025-01-18 21:04:21 +01:00
Reworking DbAccess
This commit is contained in:
parent
5a1f350caf
commit
cb13ef54c1
|
@ -15,6 +15,8 @@ namespace SharpRss
|
||||||
{
|
{
|
||||||
public static class DbAccess
|
public static class DbAccess
|
||||||
{
|
{
|
||||||
|
//TODO: Rename group => category.
|
||||||
|
//TODO: Reworking feed => model/db implementation.
|
||||||
private static bool _isInitialized;
|
private static bool _isInitialized;
|
||||||
private static readonly string ConnectionString = $"Data Source={Path.Combine(Environment.CurrentDirectory, "sharp_rss.sqlite")};";
|
private static readonly string ConnectionString = $"Data Source={Path.Combine(Environment.CurrentDirectory, "sharp_rss.sqlite")};";
|
||||||
private const string GroupTable = "group_data";
|
private const string GroupTable = "group_data";
|
||||||
|
@ -22,19 +24,19 @@ namespace SharpRss
|
||||||
private const string FeedItemTable = "feed_item_data";
|
private const string FeedItemTable = "feed_item_data";
|
||||||
|
|
||||||
// Groups
|
// 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);
|
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(categoryId != null ? $"SELECT * FROM {GroupTable} WHERE id=@gId;" : $"SELECT * FROM {GroupTable}", dbc)
|
||||||
{
|
{
|
||||||
Parameters =
|
Parameters =
|
||||||
{
|
{
|
||||||
new SqliteParameter("gId", groupId)
|
new SqliteParameter("gId", categoryId)
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
await using SqliteDataReader reader = await cmd.ExecuteReaderAsync();
|
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);
|
await using SqliteCommand cmdFeedCount = new SqliteCommand($"SELECT COUNT(*) FROM {FeedTable} WHERE group_id=@groupId", dbc);
|
||||||
while (reader.Read())
|
while (reader.Read())
|
||||||
{
|
{
|
||||||
|
@ -43,7 +45,7 @@ namespace SharpRss
|
||||||
using SqliteDataReader countReader = await cmdFeedCount.ExecuteReaderAsync();
|
using SqliteDataReader countReader = await cmdFeedCount.ExecuteReaderAsync();
|
||||||
int count = countReader.Read() ? countReader.GetInt32(0) : 0;
|
int count = countReader.Read() ? countReader.GetInt32(0) : 0;
|
||||||
|
|
||||||
groups.Add(new CategoryModel()
|
categories.Add(new CategoryModel()
|
||||||
{
|
{
|
||||||
Name = reader["name"].ToString(),
|
Name = reader["name"].ToString(),
|
||||||
FeedCount = count,
|
FeedCount = count,
|
||||||
|
@ -52,9 +54,9 @@ namespace SharpRss
|
||||||
Id = reader["id"].ToString()
|
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;
|
bool result = false;
|
||||||
await using SqliteConnection dbc = new SqliteConnection(ConnectionString);
|
await using SqliteConnection dbc = new SqliteConnection(ConnectionString);
|
||||||
|
@ -63,10 +65,10 @@ namespace SharpRss
|
||||||
{
|
{
|
||||||
Parameters =
|
Parameters =
|
||||||
{
|
{
|
||||||
new SqliteParameter("id", groupModel.Id),
|
new SqliteParameter("id", categoryModel.Id),
|
||||||
new SqliteParameter("hexColor", groupModel.HexColor),
|
new SqliteParameter("hexColor", categoryModel.HexColor),
|
||||||
new SqliteParameter("icon", groupModel.Icon),
|
new SqliteParameter("icon", categoryModel.Icon),
|
||||||
new SqliteParameter("name", groupModel.Name)
|
new SqliteParameter("name", categoryModel.Name)
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
int affected = await cmd.ExecuteNonQueryAsync();
|
int affected = await cmd.ExecuteNonQueryAsync();
|
||||||
|
@ -74,7 +76,7 @@ namespace SharpRss
|
||||||
result = true;
|
result = true;
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
public static async Task<bool> RemoveGroupAsync(CategoryModel groupModel)
|
public static async Task<bool> RemoveCategoryAsync(CategoryModel categoryModel)
|
||||||
{
|
{
|
||||||
bool result = false;
|
bool result = false;
|
||||||
await using SqliteConnection dbc = new SqliteConnection(ConnectionString);
|
await using SqliteConnection dbc = new SqliteConnection(ConnectionString);
|
||||||
|
@ -84,7 +86,7 @@ namespace SharpRss
|
||||||
{
|
{
|
||||||
Parameters =
|
Parameters =
|
||||||
{
|
{
|
||||||
new SqliteParameter("id", groupModel.Id)
|
new SqliteParameter("id", categoryModel.Id)
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
int affected = await cmd.ExecuteNonQueryAsync();
|
int affected = await cmd.ExecuteNonQueryAsync();
|
||||||
|
@ -361,7 +363,7 @@ namespace SharpRss
|
||||||
await using SqliteDataReader reader = await cmd.ExecuteReaderAsync();
|
await using SqliteDataReader reader = await cmd.ExecuteReaderAsync();
|
||||||
HashSet<CategoryModel>? groups = null;
|
HashSet<CategoryModel>? groups = null;
|
||||||
if (reader.Read())
|
if (reader.Read())
|
||||||
groups = await GetGroupsAsync(reader["group_id"].ToString());
|
groups = await GetCategoriesAsync(reader["group_id"].ToString());
|
||||||
if (groups != null && groups.Any())
|
if (groups != null && groups.Any())
|
||||||
result = groups.FirstOrDefault();
|
result = groups.FirstOrDefault();
|
||||||
return result;
|
return result;
|
||||||
|
|
|
@ -24,12 +24,12 @@ namespace SharpRss.Services
|
||||||
public async Task<HashSet<object>> GetGroupsFeedsAsync()
|
public async Task<HashSet<object>> GetGroupsFeedsAsync()
|
||||||
{
|
{
|
||||||
HashSet<object> items = new HashSet<object>();
|
HashSet<object> items = new HashSet<object>();
|
||||||
items.UnionWith(await GetGroupsAsync());
|
items.UnionWith(await GetCategoriesAsync());
|
||||||
items.UnionWith(await GetUngroupedFeedsAsync());
|
items.UnionWith(await GetUngroupedFeedsAsync());
|
||||||
return items;
|
return items;
|
||||||
}
|
}
|
||||||
public async Task<bool> CreateGroupAsync(CategoryModel group) => await DbAccess.SetGroupAsync(group);
|
public async Task<bool> CreateGroupAsync(CategoryModel group) => await DbAccess.SetCategoryAsync(group);
|
||||||
public async Task<HashSet<CategoryModel>> GetGroupsAsync() => await DbAccess.GetGroupsAsync();
|
public async Task<HashSet<CategoryModel>> GetCategoriesAsync() => await DbAccess.GetCategoriesAsync();
|
||||||
|
|
||||||
//TODO: Rework this!
|
//TODO: Rework this!
|
||||||
// Subscribe to a feed.
|
// Subscribe to a feed.
|
||||||
|
|
Loading…
Reference in New Issue
Block a user