ok
This commit is contained in:
@@ -0,0 +1,11 @@
|
||||
using PepperBot.Domain;
|
||||
|
||||
namespace PepperBot.Application.Interfaces;
|
||||
|
||||
public interface IDealRepository
|
||||
{
|
||||
Task InitializeAsync(CancellationToken cancellationToken);
|
||||
Task<bool> ExistsAsync(long id, CancellationToken cancellationToken);
|
||||
Task AddAsync(Deal deal, CancellationToken cancellationToken);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
using PepperBot.Domain;
|
||||
|
||||
namespace PepperBot.Application.Interfaces;
|
||||
|
||||
public interface IPepperClient
|
||||
{
|
||||
Task<IReadOnlyList<Deal>> GetLatestDealsAsync(CancellationToken cancellationToken);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
using PepperBot.Domain;
|
||||
|
||||
namespace PepperBot.Application.Interfaces;
|
||||
|
||||
public interface ITelegramNotifier
|
||||
{
|
||||
Task NotifyNewDealAsync(Deal deal, CancellationToken cancellationToken);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using PepperBot.Application.Interfaces;
|
||||
using PepperBot.Domain;
|
||||
|
||||
namespace PepperBot.Application.Services;
|
||||
|
||||
public class DealService
|
||||
{
|
||||
private readonly IPepperClient _pepperClient;
|
||||
private readonly IDealRepository _repository;
|
||||
private readonly ITelegramNotifier _notifier;
|
||||
private readonly ILogger<DealService> _logger;
|
||||
|
||||
public DealService(
|
||||
IPepperClient pepperClient,
|
||||
IDealRepository repository,
|
||||
ITelegramNotifier notifier,
|
||||
ILogger<DealService> logger)
|
||||
{
|
||||
_pepperClient = pepperClient;
|
||||
_repository = repository;
|
||||
_notifier = notifier;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public async Task CheckAndNotifyAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
var deals = await _pepperClient.GetLatestDealsAsync(cancellationToken);
|
||||
|
||||
foreach (var deal in deals)
|
||||
{
|
||||
if (await _repository.ExistsAsync(deal.Id, cancellationToken))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
_logger.LogInformation("Новая скидка: {Title} ({Id})", deal.Title, deal.Id);
|
||||
|
||||
await _repository.AddAsync(deal, cancellationToken);
|
||||
await _notifier.NotifyNewDealAsync(deal, cancellationToken);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user