Files
telegram-bot/modules/RichSiteSummary/RichSiteSummaryRepositoryExtension.cs

75 lines
2.8 KiB
C#

using System;
using System.Linq;
using System.Threading.Tasks;
using Kruzya.TelegramBot.RichSiteSummary.Data;
using Microsoft.EntityFrameworkCore;
using Telegram.Bot.Types;
namespace Kruzya.TelegramBot.RichSiteSummary;
internal static class RichSiteSummaryRepositoryExtension
{
#region Subscriber
/// <summary>
/// Searchs the <see cref="Subscriber"/>, who reads a <see cref="Feed"/>.
/// </summary>
/// <param name="repository"></param>
/// <param name="feed"></param>
/// <returns>The array with all subscribers.</returns>
public static async Task<Subscriber[]> ByFeed(this DbSet<Subscriber> repository, Feed feed)
{
return await repository.Where(subscriber => subscriber.Feed == feed)
.ToArrayAsync();
}
public static IQueryable<Subscriber> AllSubscriptions(this DbSet<Subscriber> repository, Chat chat)
{
return repository.Where(entity => entity.SubscriberId == chat.Id);
}
public static async Task<bool> HasSubscription(this DbSet<Subscriber> repository, Chat chat, Feed feed)
{
return (await repository.Where(subscriber => subscriber.Feed == feed && subscriber.SubscriberId == chat.Id)
.CountAsync()) != 0;
}
#endregion
#region Feed
/// <summary>
/// Searchs the <see cref="Feed"/> for fetching.
/// </summary>
/// <param name="repository"></param>
/// <param name="period">The period (in seconds) from last fetching.</param>
/// <param name="count">Max results count</param>
/// <returns></returns>
public static async Task<Feed[]> ForFetching(this DbSet<Feed> repository, UInt16 period, UInt16 count) =>
await repository.Where(feed => feed.UpdatedAt < (DateTime.Now - TimeSpan.FromSeconds(period)))
.OrderBy(feed => feed.UpdatedAt)
.Take(count)
.ToArrayAsync();
/// <summary>
/// Searchs the <see cref="Feed"/> for fetching.
/// </summary>
/// <param name="repository"></param>
/// <param name="period">The period (in seconds) from last fetching.</param>
/// <returns></returns>
public static async Task<Feed[]> ForFetching(this DbSet<Feed> repository, UInt16 period) =>
await repository.ForFetching(period, 25);
#endregion
#region Post
[Obsolete("Use ByFeedAndUrlHash()")]
public static async Task<Post> ByFeedAndUrl(this DbSet<Post> repository, string url, Feed feed = null) =>
await repository.FirstOrDefaultAsync(post => post.Url == url && (feed == null || post.Feed == feed));
public static async Task<Post> ByFeedAndUrlHash(this DbSet<Post> repository, string urlHash, Feed feed = null) =>
await repository.FirstOrDefaultAsync(post => post.UrlHash == urlHash && (feed == null || post.Feed == feed));
#endregion
}