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
///
/// Searchs the , who reads a .
///
///
///
/// The array with all subscribers.
public static async Task ByFeed(this DbSet repository, Feed feed)
{
return await repository.Where(subscriber => subscriber.Feed == feed)
.ToArrayAsync();
}
public static IQueryable AllSubscriptions(this DbSet repository, Chat chat)
{
return repository.Where(entity => entity.SubscriberId == chat.Id);
}
public static async Task HasSubscription(this DbSet repository, Chat chat, Feed feed)
{
return (await repository.Where(subscriber => subscriber.Feed == feed && subscriber.SubscriberId == chat.Id)
.CountAsync()) != 0;
}
#endregion
#region Feed
///
/// Searchs the for fetching.
///
///
/// The period (in seconds) from last fetching.
/// Max results count
///
public static async Task ForFetching(this DbSet repository, UInt16 period, UInt16 count) =>
await repository.Where(feed => feed.UpdatedAt < (DateTime.Now - TimeSpan.FromSeconds(period)))
.OrderBy(feed => feed.UpdatedAt)
.Take(count)
.ToArrayAsync();
///
/// Searchs the for fetching.
///
///
/// The period (in seconds) from last fetching.
///
public static async Task ForFetching(this DbSet repository, UInt16 period) =>
await repository.ForFetching(period, 25);
#endregion
#region Post
public static async Task ByFeedAndUrl(this DbSet repository, string url, Feed feed = null) =>
await repository.FirstOrDefaultAsync(post => post.Url == url && (feed == null || post.Feed == feed));
#endregion
}