2023-05-18 01:27:11 +02:00
|
|
|
using System.Collections.Generic;
|
2023-05-18 20:15:31 +02:00
|
|
|
using System.Linq;
|
2023-05-18 01:27:11 +02:00
|
|
|
using System.Threading.Tasks;
|
2023-05-18 20:15:31 +02:00
|
|
|
using CodeHollow.FeedReader;
|
2023-05-18 01:27:11 +02:00
|
|
|
using SharpRss.Models;
|
2023-05-18 20:15:31 +02:00
|
|
|
using ToolQit.Extensions;
|
2023-05-18 01:27:11 +02:00
|
|
|
|
|
|
|
namespace SharpRss.Services
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// Managing RSS feeds and categories.
|
|
|
|
/// </summary>
|
2023-05-19 14:40:57 +02:00
|
|
|
public class RssService : IDisposable
|
2023-05-18 01:27:11 +02:00
|
|
|
{
|
|
|
|
public RssService()
|
|
|
|
{
|
2023-05-18 20:15:31 +02:00
|
|
|
//SetupTestCategoriesAndFeedsAsync();
|
2023-05-18 01:27:11 +02:00
|
|
|
}
|
2023-05-18 20:15:31 +02:00
|
|
|
private readonly DatabaseService _dbService = new DatabaseService();
|
2023-05-18 01:27:11 +02:00
|
|
|
|
2023-05-18 20:15:31 +02:00
|
|
|
private async void SetupTestCategoriesAndFeedsAsync()
|
2023-05-18 01:27:11 +02:00
|
|
|
{
|
2023-05-18 20:15:31 +02:00
|
|
|
await _dbService.AddCategoriesAsync(new HashSet<CategoryModel>()
|
|
|
|
{
|
|
|
|
new CategoryModel() { Name = "All" },
|
|
|
|
new CategoryModel() { Name = "RSS" },
|
|
|
|
new CategoryModel() { Name = "Tech" },
|
|
|
|
new CategoryModel() { Name = "News" }
|
|
|
|
});
|
|
|
|
await _dbService.AddFeedsAsync(new HashSet<FeedModel>()
|
|
|
|
{
|
|
|
|
new FeedModel("http://fedoramagazine.org/feed/"),
|
|
|
|
new FeedModel("https://www.nasa.gov/rss/dyn/breaking_news.rss"),
|
|
|
|
new FeedModel("https://journals.plos.org/plosone/feed/atom"),
|
|
|
|
new FeedModel("https://itsfoss.com/feed")
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public async Task<Feed> GetFeedAsync(string rssUrl)
|
|
|
|
{
|
|
|
|
return await FeedCache.GetFeed(rssUrl);
|
2023-05-18 01:27:11 +02:00
|
|
|
}
|
|
|
|
|
2023-05-18 20:15:31 +02:00
|
|
|
public async Task<HashSet<object>> GetAllUnsortedAsync()
|
2023-05-18 01:27:11 +02:00
|
|
|
{
|
|
|
|
HashSet<object> items = new HashSet<object>();
|
2023-05-18 20:15:31 +02:00
|
|
|
var categories = await _dbService.GetCategoriesAsync();
|
|
|
|
var feeds = await _dbService.GetFeedsAsync(string.Empty);
|
|
|
|
items.UnionWith(categories);
|
|
|
|
items.UnionWith(feeds);
|
2023-05-18 01:27:11 +02:00
|
|
|
return items;
|
|
|
|
}
|
|
|
|
|
|
|
|
public async Task<HashSet<CategoryModel>> GetCategoriesAsync()
|
|
|
|
{
|
2023-05-18 20:15:31 +02:00
|
|
|
var result = await _dbService.GetCategoriesAsync();
|
|
|
|
return result.OrderBy(x => x.Name).ToHashSet();
|
2023-05-18 01:27:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public async Task<HashSet<FeedModel>> GetFeedsAsync(CategoryModel? categoryModel = null)
|
|
|
|
{
|
2023-05-18 20:15:31 +02:00
|
|
|
HashSet<FeedModel> feeds;
|
2023-05-18 01:27:11 +02:00
|
|
|
if (categoryModel != null)
|
2023-05-18 20:15:31 +02:00
|
|
|
feeds = await _dbService.GetFeedsAsync(categoryModel.CategoryId);
|
2023-05-18 01:27:11 +02:00
|
|
|
else
|
2023-05-18 20:15:31 +02:00
|
|
|
feeds = await _dbService.GetFeedsAsync();
|
2023-05-18 01:27:11 +02:00
|
|
|
return feeds;
|
|
|
|
}
|
|
|
|
|
2023-05-18 20:15:31 +02:00
|
|
|
public async void AddCategoryAsync(string name, string? icon, string hexColor)
|
2023-05-18 01:27:11 +02:00
|
|
|
{
|
2023-05-18 20:15:31 +02:00
|
|
|
CategoryModel categoryModel = new CategoryModel()
|
|
|
|
{
|
|
|
|
Name = name
|
|
|
|
};
|
|
|
|
if (icon != null)
|
|
|
|
categoryModel.PathIcon = icon;
|
|
|
|
if (!hexColor.IsNullEmptyWhiteSpace())
|
|
|
|
categoryModel.HexColor = hexColor;
|
|
|
|
await _dbService.AddCategoriesAsync(new HashSet<CategoryModel>() { categoryModel });
|
2023-05-18 01:27:11 +02:00
|
|
|
}
|
|
|
|
|
2023-05-18 20:15:31 +02:00
|
|
|
public async void AddFeedAsync(string rssUrl, CategoryModel? category = null)
|
2023-05-18 01:27:11 +02:00
|
|
|
{
|
2023-05-18 20:15:31 +02:00
|
|
|
FeedModel feedModel = new FeedModel(rssUrl, category);
|
|
|
|
await _dbService.AddFeedsAsync(new HashSet<FeedModel>() { feedModel });
|
2023-05-18 01:27:11 +02:00
|
|
|
}
|
2023-05-19 14:40:57 +02:00
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
{
|
|
|
|
_dbService.Dispose();
|
|
|
|
}
|
|
|
|
}
|
2023-05-18 01:27:11 +02:00
|
|
|
}
|