mirror of
https://github.com/Bubuni-Team/telegram-bot.git
synced 2026-07-31 00:29:24 +03:00
72 lines
2.7 KiB
C#
72 lines
2.7 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
|
||
|
|
{
|
||
|
|
public 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
|
||
|
|
|
||
|
|
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));
|
||
|
|
|
||
|
|
#endregion
|
||
|
|
}
|
||
|
|
}
|